<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML</title>
<link rel="stylesheet" href="style.css">
<style>
/* The Modal backdrop */
.modal {
display: none; /* Hidden by default */
position: fixed; /* position fixed at top */
z-index: 1; /* on top */
padding-top: 100px;
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgba(0,0,0,0.2)
}
/* Modal Content */
.modal-content {
background-color: white;
margin: auto;
padding: 20px;
box-shadow: 10px 5px 5px rgba(0,0,0,0.5);
width: 50%;
text-align: center;
}
/* The Close Button */
.close {
color: #dddddd;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000000;
text-decoration: none;
cursor: pointer;
}
@media only screen and (max-width: 600px){
.modal-content {
width: 80%;
margin: 15px auto;
padding: 20px;
}
}
</style>
</head>
<body>
<h2>Modal Example</h2>
<!-- Button to open modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal conent -->
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<h2> Modal Header </h2>
<img src="https://s3.ap-south-1.amazonaws.com/s3.studytonight.com/tutorials/uploads/pictures/1628065818-101156.png" alt="">
<p> The content in the modal can be added here </p>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>