Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a Modal Box with CSS and JavaScript?
A modal box is a popup window that displays on top of the current page content. It requires CSS for styling and positioning, and JavaScript for interactive functionality like opening and closing.
Syntax
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.4); /* Black with opacity */
}
Example
The following example demonstrates how to create a modal box with CSS and JavaScript −
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
.modal {
text-align: center;
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
}
.modalContent {
font-size: 20px;
font-weight: bold;
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
max-width: 500px;
border-radius: 5px;
}
.close {
color: rgb(255, 65, 65);
float: right;
font-size: 40px;
font-weight: bold;
}
.close:hover, .close:focus {
color: #ff1010;
cursor: pointer;
}
.openModal {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.openModal:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Modal Example</h1>
<button class="openModal">Open Modal</button>
<p>Click on the above button to open modal</p>
<div class="modal">
<div class="modalContent">
<span class="close">×</span>
<p>This is a sample modal box!</p>
<p>You can add any content here.</p>
</div>
</div>
<script>
var modal = document.querySelector(".modal");
var btn = document.querySelector(".openModal");
var span = document.querySelector(".close");
btn.addEventListener("click", () => {
modal.style.display = "block";
});
span.addEventListener("click", () => {
modal.style.display = "none";
});
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
</script>
</body>
</html>
A styled green "Open Modal" button appears on the page. When clicked, a modal overlay with a white content box appears containing text and a red "×" close button. The modal can be closed by clicking the close button or clicking outside the modal content area.
Key Points
| Property | Purpose |
|---|---|
display: none |
Hides the modal initially |
position: fixed |
Positions modal relative to viewport |
z-index: 1 |
Ensures modal appears above other content |
background-color: rgba(0,0,0,0.4) |
Creates semi-transparent overlay |
Conclusion
Modal boxes combine CSS positioning and JavaScript event handling to create interactive popup windows. The key is using position: fixed with full viewport coverage and JavaScript to toggle the display property.
Advertisements
