skip to Main Content

`I have designed a project ,where a popup appears upon clicking a button,
but the problem is that the popup doesn’t appear in the center.
How can I solve this issue using CSS?

My HTML,CSS and JS code:`

 <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>SabzLearn | Js | 165</title>
    <link rel="stylesheet" href="test.css" />
  </head>
  <body>
    <section>
      <div class="center">
        <h1>click the button below !</h1>
        <button type="button">Click Here</button>
      </div>
    </section>

    <div class="modal-parent">
      <div class="modal">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
          eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
          minim veniam, quis nostrud exercitation ullamco laboris nisi ut
          aliquip ex ea commodo consequat. Duis aute irure dolor in
          reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
          pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
          culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum
          dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
          incididunt ulorem lomre t labore et dolore magna aliqua. Ut enim ad
          minim veniam, quis nostrud exercitation ullamco laboris nisi ut
          aliquip ex ea commodo consequat. Duis aute irure dolor in
          reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
          pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
          culpa qui officia deserunt mollit anim id est laborum.
        </p>
        <span class="X">&times;</span>
      </div>
    </div>

    <script src="test.js"></script>
  </body>
</html>```

2

Answers


  1. I would use flexbox:

    .modal-parent {
      display: flex;
      align-items: center;
      justify-content: center;
      position: fixed;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      background: rgba(0, 0, 0, 0.6); /* Semi-transparent overlay */
    }
    
    .modal {
      background: #fff;
      width: 80%;
      max-width: 600px;
      padding: 20px;
      border-radius: 10px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
      text-align: center;
      position: relative;
    }
    
    .X {
      position: absolute;
      top: 10px;
      right: 10px;
      cursor: pointer;
    }
    
    Login or Signup to reply.
  2. try it :

    .modal{
      background: white;
      width: 70%;
      padding: 30px;
      border-radius: 6px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 22px;
      font-family: arial;
      position: relative;
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search