<!DOCTYPE html>
<html>
<head>
<title>Modal Example</title>
<style>
body {
font-family: sans-serif;
}
.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: rgba(0,0,0,0.4); /* Black w/ opacity */
}
.modal-content {
background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
max-width: 600px; /* Limit the maximum width */
border-radius: 5px; /* Rounded corners */
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2); /* Add a shadow */
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
cursor: pointer;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Modal Example</h2>
<button id="myBtn">Open Modal</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</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>
Explanation and Key Features:
-
HTML Structure:
- The modal is contained within a
div
with the classmodal
. - Inside the modal is another
div
with the classmodal-content
which holds the actual content of the modal. - A close button (a
span
with the classclose
) is included within themodal-content
. - A button (
myBtn
) is used to trigger the modal's opening.
- The modal is contained within a
-
CSS Styling:
- Centering: The
modal-content
is centered usingmargin: 15% auto;
.auto
for left and right margins centers the element horizontally.15%
top margin positions it vertically. - Translucency/Opacity: The
.modal
background is set torgba(0,0,0,0.4)
which creates a semi-transparent black overlay. - Full Screen Overlay: The
.modal
is positionedfixed
and takes up the full width and height of the screen. - Rounded Corners and Shadow:
border-radius
andbox-shadow
are used for visual enhancement.
- Centering: The
-
JavaScript Functionality:
- Opening the Modal: Clicking the button sets the modal's display to
block
. - Closing the Modal: Clicking the close button or clicking anywhere outside the modal sets the modal's display to
none
.
- Opening the Modal: Clicking the button sets the modal's display to
How to Use:
- Save the code as an HTML file (e.g.,
modal.html
). - Open the file in a web browser.