Failed to Build a Modal Popup in Interview?

Problem Statement:

Requirement:

HTML code for Modal Popup

 <!-- Title of the page -->
    <title>Modal PopUp</title>
 <!-- link external css -->
    <link rel="stylesheet" href="styles.css" />
 <!-- This button is responsible for open popup -->
      <button id="openmodal" class="open-modal">Open Modal</button>
<!-- Modal Overlay -->
    <div id="modalOverlay" class="modal-overlay">
      <!-- Modal Content -->
      <div class="modal">
        <!-- Close button -->
        <span id="closeModalBtn" class="close-btn">&times;</span>
        <h2>Modal Popup</h2>
        <p>
          This is a sample modal window created using HTML, CSS, and JavaScript.
        </p>
      </div>
    </div>
<script src="main.js"></script>

CSS code for Modal Popup :

/* Modal Box */
.modal {
  background: white;
  width: 350px;
  padding: 20px;
  border-radius: 10px;
  position: relative;
  animation: slideUp 0.3s ease;
}

/* Overlay - Hidden by Default */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  justify-content: center;
  align-items: center;
}

/* Fade-in Animation */
.modal-overlay.show {
  display: flex;
}
  • display: none hides the modal initially
  • This is the transparent black background behind the modal.
  • background: rgba(0, 0, 0, 0.5); –> rgba(0,0,0) → black, 0.5 → 50% transparency, This creates the “dimmed” background effect.
  • Using JavaScript, we add the show class to display the modal and remove it when the close button is clicked. Inside the show class, we set display: flex;, so the modal becomes visible whenever this class is applied.

JavaScript code for Modal Popup:

const openmodal = document.getElementById("openmodal");
const closeModalBtn = document.getElementById("closeModalBtn");
const modalOverlay = document.getElementById("modalOverlay");
  • Declare variable and initialize open modal button, close button and modal div container. Then simply write Event listener on click
openmodal.addEventListener("click", () => {
  console.log("open");
  modalOverlay.classList.add("show");
});

closeModalBtn.addEventListener("click", () => {
  modalOverlay.classList.remove("show");
});

HTML Code:-

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <!-- Title of the page -->
    <title>Modal PopUp</title>
    <!-- link external css -->
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div class="tech-container">
      <h1>This is a common interview Question</h1>
      <h2>On click open a Modal PopUp and close button</h2>
      <p>
        <i>By <a href="https://techinterviewhub.in/">Tech Interview Hub</a></i>
      </p>
      <!-- This button is responsible for open popup -->
      <button id="openmodal" class="open-modal">Open Modal</button>
    </div>

    <!-- Modal Overlay -->
    <div id="modalOverlay" class="modal-overlay">
      <!-- Modal Content -->
      <div class="modal">
        <!-- Close button -->
        <span id="closeModalBtn" class="close-btn">&times;</span>
        <h2>Modal Popup</h2>
        <p>
          This is a sample modal window created using HTML, CSS, and JavaScript.
        </p>
      </div>
    </div>

    <script src="main.js"></script>
  </body>
</html>

CSS Code:-

* {
  margin: 0;
  padding: 0;
}

h1,
h2 {
  margin: 10px;
}

.tech-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

#openmodal {
  margin: 20px 10px;
  padding: 16px 20px;
  border-radius: 10px;
  background-color: blue;
  color: white;
  border: none;
  cursor: pointer;
}

#openmodal:hover {
  color: blue;
  background-color: white;
  border: 1px solid blue;
}

/* Modal Box */
.modal {
  background: white;
  width: 350px;
  padding: 20px;
  border-radius: 10px;
  position: relative;
  animation: slideUp 0.3s ease;
}

/* Overlay - Hidden by Default */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  justify-content: center;
  align-items: center;
}

/* Fade-in Animation */
.modal-overlay.show {
  display: flex;
  /* animation: fadeIn 0.3s ease; */
}

#closeModalBtn {
  cursor: pointer;
}

JavaScript Code:-

const openmodal = document.getElementById("openmodal");
const closeModalBtn = document.getElementById("closeModalBtn");
const modalOverlay = document.getElementById("modalOverlay");

openmodal.addEventListener("click", () => {
  console.log("open");
  modalOverlay.classList.add("show");
});

closeModalBtn.addEventListener("click", () => {
  modalOverlay.classList.remove("show");
});

Conclusions: –

Stay Ahead in Your Tech Career 🚀

We don’t spam! Read our privacy policy for more info.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top