5

I have a pop up which contains an ASP.NET form, click the link "Request Information" and the form appears.

However, the pages that have the link "Request Information" to trigger the pop up have a lot of content therefore scrolling is required to see the link.

I need to have the div always centered if a user scrolls to read the content, otherwise if they don't scroll the pop up still appears centered on screen.

The div is positioned absolutely, the whole page width is 960px with margin set to 0 auto.

Core Xii
  • 6,270
  • 4
  • 31
  • 42
Filth
  • 3,116
  • 14
  • 51
  • 79

5 Answers5

17

If the div has an fixed width and height use: (if width=120px and height=80px)

position: fixed;
top: 50%;
left: 50%;
margin-left: -60px; /* negative half of the width */
margin-top: -40px; /* negative half of the height */
ninov
  • 629
  • 1
  • 6
  • 17
0

In your CSS:

.ClassCenter {
    position: absolute;
    left: 50%;
    top: 50%;
}

Source: http://demo.tutorialzine.com/2010/03/centering-div-vertically-and-horizontally/demo.html

Core Xii
  • 6,270
  • 4
  • 31
  • 42
Alan
  • 101
  • 1
  • 6
0

Use a position:fixed; for this, use 0 for top, left, right and bottom, and give the div a display:table-cell; also, like this, setting text-align:center; and vertical-align:middle; will make everything inside appear exactly in the middle, without pixel-exact hacks like negative margins.

0

If your popup div has a fixed size then you can use this CSS:

position: fixed;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto; /* this requires a fixed size */

Otherwise you have to fiddle around with display: table-cell; and the like.

Neil
  • 54,642
  • 8
  • 60
  • 72
0

Is this what you're trying to do? http://jsfiddle.net/Hrwf8/

The key here is to set the left and top styles to 50% and set the margin-left and top to the negative amount of half of the width and height of the div. This of course requires that you have a fixed size for your div.

Godwin
  • 9,739
  • 6
  • 40
  • 58