<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.overlay {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.4);
}
.overlay-content {
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.closebtn {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.closebtn:hover,
.closebtn:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<h2>overlay Example</h2>
<!-- Trigger/Open The overlay -->
<button id="myBtn" onclick="openoverlay()">Open overlay</button>
<!-- The overlay -->
<div id="myoverlay" class="overlay">
<!-- overlay content -->
<div class="overlay-content">
<span class="closebtn" onclick="closebtn()">×</span>
<p>Some text in the overlay..</p>
</div>
</div>
<script>
var overlay = document.getElementById("myoverlay");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("closebtn")[0];
function openoverlay()
{
overlay.style.display = "block";
}
function closebtn()
{
overlay.style.display = "none";
}
window.onclick = function(event) //outside overlay box click also hide box.
{
if (event.target == overlay) {
overlay.style.display = "none";
}
}
</script>
</body>
</html>
in above example we have set two class one cover complete webpage, other is overlay box, one close button two hide open box.
simple display:none or block properties set from script and style. position must fixed to cover all page & background set rgba color.