Implement a Responsive Modal Dialog with Materialize CSS
Learn how to add a responsive Materialize CSS modal that centers, darkens the background, closes on outside click or ESC, and works on narrow screens.
26 Aug 2025, 23:54 UTC

Desired outcome
You want a modal window that appears centered, darkens the page background, can be dismissed by clicking outside the modal or pressing the Esc key, and adapts to narrow viewports.
Takeaway
After following the steps below you will have a functional modal that meets those criteria, with verification steps and a simple way to revert the changes if needed.
Prerequisites
- Basic knowledge of HTML and JavaScript.
- Access to edit the HTML file where the modal will appear.
- Materialize CSS and JavaScript files (version 1.0.0 or newer) available locally or via CDN.
Procedure
-
Add Materialize assets
In the
<head>of your page, include the CSS and JS files. If you use a CDN, the tags look like this:<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>No jQuery is required; Materialize uses vanilla JavaScript.
-
Create the trigger button
Place a button (or any element) that will open the modal. Give it the class
modal-triggerand adata-targetattribute whose value matches the modal’s ID.<a class="waves-effect waves-light btn modal-trigger" data-target="example-modal">Open modal</a> -
Define the modal markup
After the trigger (commonly just before the closing
</body>tag), add adivwith classmodal. Inside, includemodal-contentfor the body and optionallymodal-footerfor actions.<div id="example-modal" class="modal"> <div class="modal-content"> <h4>Modal Header</h4> <p>This is the modal body. It remains usable on screens narrower than 600px.</p> </div> <div class="modal-footer"> <a href="#!" class="modal-close waves-effect waves-green btn-flat">Agree</a> </div> </div> -
Initialize the modal with JavaScript
Materialize provides a vanilla JS API. Run the initialization after the DOM is fully loaded.
document.addEventListener('DOMContentLoaded', function () { var elems = document.querySelectorAll('.modal'); var instances = M.Modal.init(elems, { // optional: prevent scrolling behind the modal // dismissible: true, // opacity: 0.5 }); });Place this script either at the end of
<body>or in a separate JS file that loads after the DOM.
Expected checks
- Click the trigger button; the modal should appear centered with a semi‑transparent overlay that darkens the rest of the page.
- Clicking anywhere on the overlay or pressing
Escshould close the modal. - Resize the browser window to a width below 600px; the modal should expand to full width while remaining usable.
- Open DevTools, select the
<body>element, and verify that the classmodal-openis present while the modal is visible and absent when closed.
Limitations
- Initialization must occur after the DOM is ready; calling
M.Modal.inittoo early will have no effect. - Duplicate IDs break the selector; ensure the value of
data-targetmatches exactly one modal’sid. - Loading another CSS framework that overrides
.modalor.modal-contentmay alter layout or accessibility.
Recovery options (rollback)
If you need to revert the changes:
- Remove the CSS and JS link tags you added in step 1.
- Delete the trigger button markup.
- Delete the modal markup block.
- Remove the initialization script.
After removal, reload the page to confirm the modal no longer appears.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.