135

I have in my page a button which when clicked displays a div (popup style) in the middle of my screen.

I am using the following CSS to center the div in the middle of the screen:

.PopupPanel
{
    border: solid 1px black;
    position: absolute;
    left: 50%;
    top: 50%;
    background-color: white;
    z-index: 100;

    height: 400px;
    margin-top: -200px;

    width: 600px;
    margin-left: -300px;
}

This CSS works fine as long as the page is not scrolled down.

But, if I place the button at the bottom of my page, when it is clicked, the div is displayed at the top, and the user has to scroll up to view the contents of the div.

I would like to know how to display the div in the middle of the screen, even when the page has been scrolled.

DavidRR
  • 18,291
  • 25
  • 109
  • 191
tanya
  • 2,925
  • 7
  • 32
  • 49
  • question question. Why are you subtracting margin-top: (200) and margin-left. I feel this is the medium of the height and width, but why do we have to do that to get the absolute center? Shouldn't the left 50% and top 50% do the trick? – jmoon90 Jun 06 '16 at 00:07
  • @jmoon90 The `left: 50%; top: 50%` moves the *top left corner* of the `.PopupPanel` to the center of the screen. We then move it half of the width and height back to center it's center. See [Centering on css-tricks.com](https://css-tricks.com/centering-css-complete-guide/) – kub1x Mar 30 '17 at 10:55

5 Answers5

209

Change the position attribute to fixed instead of absolute.

BraedenP
  • 7,125
  • 4
  • 33
  • 42
  • 2
    What about if you need to scroll the pop up div and its larger than the screen? – Darcbar Feb 27 '12 at 15:32
  • Keep in head that this is not the most responsive solution (but a perfect solution for the problem above). Check http://getbootstrap.com/javascript/#modals and look with your DevTools to get some nice ideas about working with your popups/modals. – Cas Bloem Jun 13 '14 at 09:02
36

Change position:absolute; to position:fixed;

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
19

Quote: I would like to know how to display the div in the middle of the screen, whether user has scrolled up/down.

Change

position: absolute;

To

position: fixed;

W3C specifications for position: absolute and for position: fixed.

Sparky
  • 98,165
  • 25
  • 199
  • 285
7

Here's how to center a box in the middle of the screen even if you don't have fixed dimensions. Let's say you would like a box 60% width / 60% height. The way to make it centered is by creating 2 boxes: a "container" box that position left: 50% top :50%, and a "text" box inside with reverse position left: -50%; top :-50%;

It works and it's cross browser compatible.

Check out the code below, you probably get a better explanation:

html, body {
  min-height: 100%;
}

#message {
  height: 100%;
  left: 0;
  position: fixed;
  top: 0;
  width: 100%;
}

#message .container {
  height: 60%;
  left: 50%;
  position: absolute;
  top: 50%;
  z-index: 10;
  width: 60%;
}

#message .container .text {
  background: #fff;
  height: 100%;
  left: -50%;
  position: absolute;
  top: -50%;
  width: 100%;
}

#message .bg {
  background: rgba(0, 0, 0, 0.5);
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%;
  z-index: 9;
}
<div id="message">
  <div class="container">
    <div class="text">
      <h2>Warning</h2>
      <p>The message</p>
    </div>
  </div>
  <div class="bg"></div>
</div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
SequenceDigitale.com
  • 4,038
  • 1
  • 24
  • 23
5

Correct Method is

.PopupPanel
{
    border: solid 1px black;
    position: fixed;
    left: 50%;
    top: 50%;
    background-color: white;
    z-index: 100;
    height: 400px;
    margin-top: -200px;

    width: 600px;
    margin-left: -300px;
}
Sakthi Karthik
  • 3,105
  • 4
  • 25
  • 29