CSS Portal
Run
Search
« Back to
HTML inert Global Attribute
<style> .modal { display: none; position: fixed; z-index: 1; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); padding-top: 60px; } .modal-content { background-color: #fefefe; margin: 5% auto; padding: 20px; border: 1px solid #888; width: 80%; } .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } </style> <!-- Main content --> <div id="mainContent"> <h2>Main Content of the Page</h2> <p>This content will become non-interactive when the modal is open.</p> <button id="openModalBtn">Open Modal</button> </div> <!-- The Modal --> <div id="myModal" class="modal"> <div class="modal-content"> <span class="close">×</span> <h2>Modal Dialog</h2> <p>This is a simple modal. You can interact with this content, but not with the main page behind it.</p> </div> </div> <script> // Get the modal var modal = document.getElementById("myModal"); // Get the button that opens the modal var btn = document.getElementById("openModalBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks the button, open the modal and make main content inert btn.onclick = function() { modal.style.display = "block"; document.getElementById("mainContent").setAttribute("inert", ""); } // When the user clicks on <span> (x), close the modal and remove inert from main content span.onclick = function() { modal.style.display = "none"; document.getElementById("mainContent").removeAttribute("inert"); } </script>
If this site has been useful, we’d love your support! Running this site takes time and resources, and every small contribution helps us keep creating valuable content. Consider buying us a coffee to keep things going strong!
☕ Buy a Coffee