
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to create a Modal Box with CSS and JavaScript?
To create a modal box with CSS and JavaScript, the code is as follows −
Example
<!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%; } .close { color: rgb(255, 65, 65); float: right; font-size: 40px; font-weight: bold; } .close:hover, .close:focus { color: #ff1010; cursor: pointer; } </style> </head> <body> <h1>Modal Example</h1> <button class="openModal">Open Modal</button> <h2>Click on the above button to open modal</h2> <div class="modal"> <div class="modalContent"> <span class="close">×</span> <p>Sample text inside modal</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>
output
The above code will produce the following output −
On clicking the Open Modal button −
- Related Articles
- How to create a modal image gallery with CSS and JavaScript?
- How to create a delete confirmation modal with CSS and JavaScript?
- How to create responsive Modal Images with CSS and JavaScript?
- How to create a modal popup using JavaScript and CSS?
- How to create a full screen search box with CSS and JavaScript?
- How to create a flip box with CSS?
- How to create and apply CSS to JavaScript Alert box?
- Create a transparent box with CSS
- How to Create a Comment Box with a Containing Text Using CSS
- How to create tabs with CSS and JavaScript?
- How to create popups with CSS and JavaScript?
- How to create a responsive slideshow with CSS and JavaScript?
- How to create a scroll indicator with CSS and JavaScript?
- How to create a collapsible section with CSS and JavaScript?
- How to create a snackbar / toast with CSS and JavaScript?

Advertisements