<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Popup Example</title> <style> /* Style for the overlay (popup background) */ #popupOverlay { display: none; /* Hidden by default */ position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 1000; } /* Style for the popup itself */ #popup { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: white; padding: 20px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); z-index: 1001; width: 300px; text-align: center; } /* Button to close the popup */ #closeBtn { margin-top: 10px; padding: 5px 10px; background-color: red; color: white; border: none; cursor: pointer; } </style> </head> <body> <!-- Button to trigger popup --> <button id="showPopupBtn">Show Popup</button> <!-- Overlay for popup --> <div id="popupOverlay"> <!-- Popup content --> <div id="popup"> <h2>Popup Message</h2> <p>This is a simple popup example.</p> <button id="closeBtn">Close</button> </div> </div> <script> // Get elements const showPopupBtn = document.getElementById('showPopupBtn'); const popupOverlay = document.getElementById('popupOverlay'); const closeBtn = document.getElementById('closeBtn'); // Show the popup when button is clicked showPopupBtn.addEventListener('click', function() { popupOverlay.style.display = 'block'; }); // Close the popup when close button is clicked closeBtn.addEventListener('click', function() { popupOverlay.style.display = 'none'; }); // Optionally close the popup if the user clicks outside of it window.addEventListener('click', function(event) { if (event.target === popupOverlay) { popupOverlay.style.display = 'none'; } }); </script> </body> </html>