Skip to content
Shubham JangleResources
JavaScript

Explore topics

Modal

Watch the walkthrough
Open on Instagram

Exercise

Open a modal from a button and close it from its close button or overlay.

What the source implements

The source changes classes and ARIA attributes, but it does not trap focus, close on Escape, or restore focus to the trigger. Add those behaviors and an accessible dialog name before treating it as a complete modal.

Run and revise

Copy the entire fenced HTML into a file with the same name and open it in a browser. It includes the markup, styles, and JavaScript together. Check the console, keyboard behavior, and boundary cases. Remote APIs/assets and CDN scripts need network access.

Original repository source

The complete file below is preserved apart from line endings; the extensions above are not changes to this copied implementation.

8-modal.html

Original file on GitHub

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Accessible Modal</title>
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,700;1,100&display=swap"
      rel="stylesheet"
    />
    <style>
      body {
        color: white;
        background-color: rgb(71, 79, 87);
        font-family: "Poppins", sans-serif;
        font-weight: 400;
        font-style: normal;
      }
      input {
        flex-grow: 1;
        padding: 0.35rem 1rem;
        border: 1px solid gainsboro;
        border-radius: 5px;
      }
      button {
        padding: 0.5rem 1rem;
        min-width: 8rem;
        cursor: pointer;
        background-color: rgb(69, 119, 211);
        color: white;
        font-weight: 700;
        border: none;
        border-radius: 5px;
        transition: all 0.2s ease;
      }
      button:hover {
        background-color: rgb(55, 102, 191);
      }
      button:active {
        scale: 0.9;
      }
      button:disabled {
        cursor: not-allowed;
        background-color: lightgray;
      }

      #modal {
        display: none;
        z-index: 10;
        position: fixed;
        top: 50%;
        left: 50%;
        translate: -50% -50%;
        width: 400px;
        border-radius: 10px;
        color: black;
        background-color: white;
        padding: 20px;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
      }

      #overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9;
      }

      .show {
        display: block !important;
      }

      .center {
        display: grid;
        place-items: center;
        height: 100vh;
      }
    </style>
  </head>
  <body>
    <div class="center">
      <button id="toggle-modal-btn">Open Modal</button>
    </div>

    <!-- Overlay for the modal background -->
    <div id="overlay"></div>

    <!-- Modal container -->
    <div
      id="modal"
      role="dialog"
      aria-modal="true"
      aria-labelledby="modal-title"
    >
      <h2 id="modal-title">Modal Title</h2>
      <p>This is the modal content.</p>
      <button id="close-modal-btn">Close</button>
    </div>

    <script>
      const btn = document.getElementById("toggle-modal-btn");
      const modal = document.getElementById("modal");
      const closeModalBtn = document.getElementById("close-modal-btn");
      const overlay = document.getElementById("overlay");

      // Open modal
      function openModal() {
        modal.classList.add("show");
        overlay.classList.add("show");
        modal.setAttribute("aria-hidden", "false");
        btn.setAttribute("aria-expanded", "true");
        modal.focus();
      }

      // Close modal
      function closeModal() {
        modal.classList.remove("show");
        overlay.classList.remove("show");
        modal.setAttribute("aria-hidden", "true");
        btn.setAttribute("aria-expanded", "false");
      }

      // Toggle modal on button click
      btn.addEventListener("click", () => {
        if (modal.classList.contains("show")) {
          closeModal();
        } else {
          openModal();
        }
      });

      // Close modal on overlay click
      overlay.addEventListener("click", closeModal);

      // Close modal on close button click
      closeModalBtn.addEventListener("click", closeModal);

      // Close modal with Esc key
      document.addEventListener("keydown", (e) => {
        if (e.key === "Escape" && modal.classList.contains("show")) {
          closeModal();
        }
      });
    </script>
  </body>
</html>