JSFiddle - React, Tailwind, and code Playground
HTML
<a href="https://www.youtube.com/embed/pbRX2ALuy3k" class="lightbox-link">Click me</a>
<br>
<a href="https://www.youtube.com/embed/5KNGDZhoRyA" class="lightbox-link">Click me</a>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<!--Close button-->
<span class="close"><span class="close">Ă—</span></span>
<!--Insert video here-->
<div id="modalcontent">
<iframe id="modaliframe" width="500" height="315" src="#" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>
</div>
CSS
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.4);
/* Black w/ opacity */
}
/* Modal Content/Box */
.modal-content {
background-color: #fefefe;
margin: 5% auto;
/* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 60%;
/* Could be more or less, depending on screen size */
}
/* The Close Button */
.close {
display: block;
color: #D13135;
float: right;
font-size: 50px;
font-weight: 900;
line-height: 1;
}
.close:hover,
.close:focus {
color: #6a6a6a;
text-decoration: none;
cursor: pointer;
}
/*Wistia specific CSS
.wistia_responsive_padding {
margin-top: 50px !important;
}
*/
#modaliframe {
max-width: 100%;
}
JavaScript
//Define variable for the modal, close button and innerHTML of videos
var modal = document.getElementById("myModal");
var span = document.getElementsByClassName("close")[0];
//Close if click on X
span.onclick = function() {
modal.style.display = "none";
}
//Close if click outside of modal
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.querySelectorAll(".lightbox-link").forEach(function(link) {
link.addEventListener("click", function(event) {
event.preventDefault();
document.getElementById("myModal").style.display = "block";
document.getElementById("modaliframe").src = link.href;
},
false);
});