0

Possible Duplicate:
How can I vertically center text in a dynamically high div?

How to vertically center div with dynamic height and 100% width in browser window?

<div class="login-signup-page">
<div class="login-signup"></div>
</div>

where login-signup has 100% width and dynamic height

Community
  • 1
  • 1
treng
  • 1,665
  • 3
  • 15
  • 22
  • 3
    **Dublicate**: http://stackoverflow.com/questions/10939288/how-can-i-vertically-center-text-in-a-dynamically-high-div/10939940 – doptrois Jun 12 '12 at 19:51

4 Answers4

1

Short answer: You can't.

Long answer: If you know the height, you can use fixed positioning, a top:50% and a margin-top equal to negative half the height. Short of that, you can use some basic JS to calculate what the margin-top should be based on the offsetHeight. If you are vertically centering in another element, rather than the window, you can do the same but with an absolute position provided the container is not static-positioned.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    This user speaks the truth. Further, from personal experience, a set top margin of 50px to 150px usually looks far better than a vertically centered box. The reason being that, usually, the eye will scan from the top of the page down, so a large margin at the top will affect the user far more than a large margin at the bottom. – Ryan Kinal Jun 12 '12 at 19:22
  • @RyanKinal - Very true. If you want to do it, it's possible, but in my opinion, not a very smart design choice. – Anthony Atkinson Jun 12 '12 at 19:27
0

Kolink is indeed correct, but it doesn't take much work. All it takes is some clever CSS and JavaScript (I used jQuery in my example below) to make it work.

<div class="login-signup-page">
<div class="login-signup">Sample Text</div>
</div>​

.login-signup-page {
    position:absolute;
    left:0;
    top:0;
    height:100%;
    width:100%;
    background:grey;
}

.login-signup {
    width:100%;
    background:white;
    position:absolute;
    top:50%;
}
​

$(document).ready(function() {
   $('.login-signup').css('margin-top', -$('.login-signup').outerHeight() + 'px'); 
});​

Demo

Anthony Atkinson
  • 3,101
  • 3
  • 20
  • 22
0

See the jsFiddle example or go straight to the code below:

HTML

<div class="login-signup-page">
<div class="login-signup"></div>
</div>

CSS

.login-signup-page {

 /* Position at top of page */
 position:fixed;
 top:0;

 /* Position halfway down page (in supporting browsers) */
 -moz-transform:translate(0, 50%);
 -webkit-transform:translate(0, 50%);
 -ms-transform:translate(0, 50%);
 -o-transform:translate(0, 50%);
 transform:translate(0, 50%);

 /* Prevent hidden content */
 height:100%;
 width:100%;

}

.login-signup {

 /* Prevent hidden content */
 max-height:100%;
 max-width:100%;
 overflow:auto;

 /* Position in center of page (in supporting browsers) */
 -moz-transform:translate(0, -50%);
 -webkit-transform:translate(0, -50%);
 -ms-transform:translate(0, -50%);
 -o-transform:translate(0, -50%);
 transform:translate(0, -50%);

}

Note: In browsers that don't support translate(), the content will appear at the top of the page (instead of being cut off, had top:50%; been used instead).

0b10011
  • 18,397
  • 4
  • 65
  • 86
-2

You'll need Javascript or jQuery to get the height of the element and the browser window, then calculate based on those numbers. Something like this:

$(document).ready(function() {
    var signupHeight = $('.login-signup').height(); // height of your dynamic element
    var winHeight = $(window).height(); // height of the browser window
    $('.login-signup').css('margin-top',(winHeight - signupHeight) / 2); // subtract one height from the other, then divide by 2 so the top and bottom margins are equal
});
AdamJ
  • 184
  • 1
  • 1
  • 11