function openModal(){
document.getElementById("overlay").style.display='block';
document.getElementById("modal").style.display='block';
}
function closeModal(){
document.getElementById("overlay").style.display='none';
document.getElementById("modal").style.display='none';
}
.closex{
border: none;
background-color: transparent;
font-weight: bold;
}
.open{
border: none;
background-color: dodgerblue;
border-radius: 14px;
width: 150px;
height: 40px;
font-size: 20px;
}
body{
background-color: whitesmoke;
}
.modal{
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
transition: 200ms ease-in-out;
border-radius: 10px;
z-index: 10;
background-color: white;
width: 500px;
max-width: 80%;
font-family: Arial;
border: 1px solid white;
box-shadow: 0 60px 120px rgba(0, 0, 0, 0.14), 0 40px 160px rgba(0, 0, 0, 0.24);
-webkit-animation: animatezoom 0.6s;
animation: animatezoom 0.6s;
}
@-webkit-keyframes animatezoom {
from {-webkit-transform: translate(-50%,-50%) scale(0)}
to {-webkit-transform: translate(-50%,-50%) scale(1)}
}
@keyframes animatezoom {
from {transform: translate(-50%,-50%) scale(0)}
to {transform: translate(-50%,-50%) scale(1)}
}
.mhead{
border-bottom: 1px solid rgba(0,0,0,0.5);
padding: 15px 15px 15px;
font-size: 22px;
}
.mbody{
padding: 15px 15px 15px 15px;
font-size: 19px
}
.close{
margin-left: 330px;
}
#overlay{
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
pointer-events: none;
}
<body>
<div id="overlay"></div>
<div class="modal" id="modal">
<div class="mhead">Welcome!!!<span class="close"><button class="closex" onclick="closeModal()">×</button></span></div>
<div class="mbody">
Welcome to this site! We are very happy that you are using this site. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla nec odio et eros accumsan pulvinar. Integer tristique nulla eu sapien consectetur, sed auctor est consequat. Proin ac lectus in nisi porta mollis at non mauris. Etiam tincidunt vestibulum risus, vel laoreet libero blandit eget.
</div>
</div>
<div align="center">
<button onclick="openModal()" class="open">Open Modal</button>
</div>
</body>
Having trouble making the modal disappear when clicking outside of it? Here's a code snippet to achieve that:
var modal = document.getElementById('modal');
// Close the modal when clicked outside of it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
If you've tried other solutions without success, give this code a shot!