<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Floating Div Example</title> <style> body { font-family: Arial, sans-serif; } #floatingBox { display: none; /* Initially hidden */ position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; padding: 20px; background-color: #f8f9fa; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); border: 1px solid #ccc; border-radius: 8px; z-index: 1000; /* Ensures it floats above other elements */ } #overlay { display: none; /* Initially hidden */ position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); z-index: 999; /* Behind the floating box but above the rest */ } #openButton, #closeButton { padding: 10px 20px; font-size: 16px; cursor: pointer; border: none; border-radius: 5px; background-color: #007bff; color: white; } #closeButton { background-color: #dc3545; } </style> </head> <body> <button id="openButton">Open Floating Box</button> <div id="overlay"></div> <div id="floatingBox"> <p>This is a floating box. It doesn't affect the rest of the page!</p> <button id="closeButton">Close</button> </div> <script> const openButton = document.getElementById('openButton'); const closeButton = document.getElementById('closeButton'); const floatingBox = document.getElementById('floatingBox'); const overlay = document.getElementById('overlay'); openButton.addEventListener('click', () => { floatingBox.style.display = 'block'; overlay.style.display = 'block'; }); closeButton.addEventListener('click', () => { floatingBox.style.display = 'none'; overlay.style.display = 'none'; }); </script> </body> </html>
This is a floating box. It doesn't affect the rest of the page!