Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Custom Modal</title> <style> .modal { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); justify-content: center; align-items: center; } .modal-content { background-color: #fff; padding: 20px; border-radius: 5px; text-align: center; } .close { position: absolute; top: 10px; right: 10px; font-size: 20px; cursor: pointer; } </style> </head> <body> <button id="open-modal">Open Modal</button> <div id="modal" class="modal"> <div class="modal-content"> <span id="close-modal" class="close">×</span> <p>This is a custom modal!</p> </div> </div> <script> document.getElementById('open-modal').addEventListener('click', function() { document.getElementById('modal').style.display = 'flex'; }); document.getElementById('close-modal').addEventListener('click', function() { document.getElementById('modal').style.display = 'none'; }); window.addEventListener('click', function(event) { if (event.target === document.getElementById('modal')) { document.getElementById('modal').style.display = 'none'; } }); </script> </body> </html>