views:

65

answers:

5

Given a div with known dimensions, say width: 300px; height: 200px, what is the easiest method to place it in the middle of the screen both vertically and horizontally ? Example here

I'm interested in latest Firefox (no need for IE hacks).

CSS only please, no Javascript.

+5  A: 

Position it 50% from the window/parent container and use a negative margin size that's half of the elements width/height :)

position: absolute; // or fixed if you don't want it scrolling with the page.
width: 300px;
height: 200px;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -100px;

You might need to set height: 100%, on both the body and html

html, body {
    height: 100%;
}

Edit: Here's a working example .

Marko
Wouldn't `position: fixed` be more appropriate?
sje397
@sje397, that would depend on whether they want the div to scroll with the page or not.
Marko
@Marco, it seems working without setting `height: 100%` on `html, body`. Why it is needed ?
Misha Moroshko
@Misha: It's just to ensure that the body element fills the entire window height. It could've been a IE7/IE6 issue but I still tend to write it when I have to center something vertically.
Marko
Thanks Marco !!
Misha Moroshko
@Misha it's MarKKKKKKo :) Did you think I misspelled my name? :) haha
Marko
A: 

What methods are you open to using? For example CSS up to what level - 2 or 3? Javascript? jQuery? My first thought is that, since divs can be centred horizontally through margin-left: auto; and margin-right: auto;, maybe try margin: auto; for all 4 sides. Let me know if it works.

ClarkeyBoy
CSS -2 or 3? What do you mean @ClarkeyBoy?
Marko
Well I dont know if there is anything available from CSS3 which would help, but if it needs to work in CSS2. I wasnt gonna make the effort to find out if that was the case without knowing whether it can be CSS3 in the first place.
ClarkeyBoy
Doesn't work for vertical. http://jsfiddle.net/Wv2jZ/15/
Marko
I mean definitely without Javascript. CSS 3 is fine (as I wrote: latest Firefox). `margin: auto` does not work.
Misha Moroshko
Ok well I am at a loss. I am currently using the other method, as suggested by Marko originally, because I hit this problem a while back and that was a quick solution.
ClarkeyBoy
A: 

Without supporting IE, this is actually pretty easy to achieve using display: table and display: table-cell.

Here's an update to your HTML:

<div id='my_div'>
    <div class="centered">this is vertically centered</div>
</div>

CSS:

body, html
{
    height: 100%;
}
body
{
    margin: 0;
    padding: 0;
    border: 1px solid blue;
}
#my_div
{
    width: 300px;
    border: 1px solid red;
    margin-left: auto;
    margin-right: auto;
    height: 100%;
    display: table;
    overflow: hidden;
}
.centered
{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

And to preview: http://jsfiddle.net/we8BE/

wsanville
Just out of curiosity, why do you have 2 height declarations for `#my_div`?
Marko
Ah, good catch. I didn't see you already had a height defined in your styles. That might not be exactly what you're looking for, but a demonstration of how it can be done in a non-IE setting.
wsanville
The centred div is 100% height - the original question stated 200px. It doesnt work if you remove the 100% height either (at least not in Chrome). Firefox doesnt run well on my machine so cant test it - I am hoping Chrome behaves the same.
ClarkeyBoy
A: 

The easiest? One of the numerous jQuery center plugins available...

Lorenzo
Read the comments on my post - "I mean definitely without Javascript."
ClarkeyBoy
@ClarkeyBoy: Ok... that comment was not there when I started writing... No problem!
Lorenzo
This doesn't deserve a down-vote since the original post didn't explicitly say without JS. I'm going to upvote just for that reason :)
Marko
@Marko Ivanovski: thanks!
Lorenzo
@Lorenzo - Prego :)
Marko
+1  A: 

I don't know if this is the simplest way, but it seems to work.

http://www.infinitywebdesign.com/research/cssverticalcentereddiv.htm

tonyopp
That's using exactly the same solution I am :)
Marko