The Wayback Machine - https://web.archive.org/web/20240226194200/https://www.geeksforgeeks.org/how-to-create-popup-box-using-html-and-css/
Open In App
Related Articles

How to create popup box using HTML and CSS ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will see how to create a popup box using HTML and CSS. A Popup (or dialog box) is a modal window used to inform, warn, or receive input from the user. Pop-up windows should not be used as they prevent the user from accessing other aspects of the program until the pop-up window is closed.

Approach 1: In this approach, we will show how to create a popup box or window using HTML and CSS. We use some basic HTML tags like heading, paragraph, button, and anchor tags. We create a button and when clicked, the popup box will open as you can see in the output. The anchor element is attached to the popup box with the “modal” class by using the href attribute that is used to specify a link to the “popup-box” address.

Example 1: Implementation of the popup box

HTML

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <style>
        .box {
            background-color: black;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }
 
        p {
            font-size: 17px;
            align-items: center;
        }
 
        .box a {
            display: inline-block;
            background-color: #fff;
            padding: 15px;
            border-radius: 3px;
        }
 
        .modal {
            align-items: center;
            display: flex;
            justify-content: center;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(254, 126, 126, 0.7);
            transition: all 0.4s;
            visibility: hidden;
            opacity: 0;
        }
 
        .content {
            position: absolute;
            background: white;
            width: 400px;
            padding: 1em 2em;
            border-radius: 4px;
        }
 
        .modal:target {
            visibility: visible;
            opacity: 1;
        }
 
        .box-close {
            position: absolute;
            top: 0;
            right: 15px;
            color: #fe0606;
            text-decoration: none;
            font-size: 30px;
        }
    </style>
</head>
 
<body>
    <div class="box">
        <a href="#popup-box">
            Click to Open Popup Box !
        </a>
    </div>
    <div id="popup-box" class="modal">
        <div class="content">
            <h1 style="color: green;">
                Hello GeeksForGeeks !
            </h1>
            <b>
                <p>Never Give Up !</p>
            </b>
            <a href="#" class="box-close">
                ×
            </a>
        </div>
    </div>
</body>
 
</html>

                    

Output : 

Output

Approach 2: In this example, we created an automatic popup window using HTML and CSS with the help of some markup tags like button, heading, anchor, and div element and then used a few lines of JavaScript code to create the dynamic popup window, we just add loading of the popup window in screen automatically. We use internal CSS for styling using the class names. The addEventListener() function in JavaScript takes the click event to listen for showing and closing the popup box with the id “myPopup”.

Example 2: Implementation of the popup box

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width,
                   initial-scale=1.0">
    <style>
        .popup {
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0, 0, 0, 0.4);
            display: none;
        }
        .popup-content {
            background-color: white;
            margin: 10% auto;
            padding: 20px;
            border: 1px solid #888888;
            width: 30%;
            font-weight: bolder;
        }
        .popup-content button {
            display: block;
            margin: 0 auto;
        }
        .show {
            display: block;
        }
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h3>Create popup box using HTML and CSS</h3>
    <button id="myButton">
          Click me
      </button>
    <div id="myPopup" class="popup">
        <div class="popup-content">
            <h1 style="color:green;">
                  GeekforGeeks !
              </h1>
            <p>This is a popup box!</p>
            <button id="closePopup">
                  Close
              </button>
        </div>
    </div>
 
    <script>
        myButton.addEventListener("click", function () {
            myPopup.classList.add("show");
        });
        closePopup.addEventListener("click", function () {
            myPopup.classList.remove("show");
        });
        window.addEventListener("click", function (event) {
            if (event.target == myPopup) {
                myPopup.classList.remove("show");
            }
        });
    </script>
</body>
</html>

                    

Output:

Output



Last Updated : 11 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads