Vertically and horizontally center a div using CSS
As a web designer, I'm often asked to center content on a web page, vertically as well as horizontally. This is often referred to as 'the letterbox effect'. Back in the day, when we all used tables to mark up our pages, this was a pretty simple task. However, now that everyone knows the error of their ways and tables are considered old hat, ugly and an improper way of marking up a page, this task has actually become more complicated.
The question 'How do I vertically center text on a page?' is one that's commonly asked on webmasters forums, and one that has been answered in many different ways. Well, today I was asked once again to do the terrible deed. After digging through all of my old code looking for the answer, I began to wonder why I never posted this to a blog when I first discovered it...
Try a demonstration (and let me know if it doesn't work in your browser)
The technique I'm going to look at uses CSS only and works under the following conditions:
- You do not know the dimensions of the browser window (user can resize)
- You do know the dimensions of your main content area (fixed size)
The mark-up
The mark up is simple and consists of two structural divs - one container div, and another to hold.
[content here]
The CSS:
#outer {
position: absolute;
top: 50%;
left: 0px;
width: 100%;
height: 1px;
overflow: visible;
}
#inner {
width: 300px;
height: 200px;
margin-left: -150px; /*** width / 2 ***/
position: absolute;
top: -100px; /*** height / 2 ***/
left: 50%;
}
The outer div is is positioned half way down the page (top 50%). It has a height of 1px and it's overflow is set to visible.
The content box has it's width and height absolutely specified. It is then positioned 50% horizontally across the page (left:50%) and has it's left margin set to a value that is half it's width. This deals with the horizontal centering. To do the vertical centering we give it a negative top position that is half it's height.
Compatibility
I have tested on the following browsers, although I don't see any reason why it shouldn't work on others - but that's not to say that it does!
- Firefox 2 (pc/linux)
- Firefox 3.0.1 (pc/linux)
- Opera 9.27 (pc/linux)
- Safari 3.1.1 (pc)
- IE (4.01, 5.01, 5.5, 6, & 7)
This isn't a technique that I claim to have invented and neither is it one which I want to take any credit for - I would have posted references to the original articles which I used to help me create it, but unfortunately this was done so long ago that I have since forgotten where I found it.
Similar articles on this site
LATEST ARTICLES
TODAY'S MOST POPULAR
-
4th Sep 08
-
8th Aug 08
-
20th Sep 08
MOST POPULAR ARTICLES
RECENT COMMENTS
-
1 day 6 hours ago
-
3 days 19 hours ago
-
4 days 32 min ago
-
4 days 6 hours ago
-
4 days 23 hours ago

Comments
4th Sep 2008, 8:24am
Vertical centering without knowing the dimensions of the main content area is (as far as I know) impossible without tables.
Why has this not been included in CSS3?
4th Sep 2008, 8:29am
Well, vertical centering without knowing the dimensions of the main content area is actually not impossible, but I've never found a way to do it that is completely cross browser compatible.
4th Sep 2008, 9:10am
great article i'll definately use this tip thanks!
4th Sep 2008, 4:29pm
on safari 4 beta on mac do not work, and still not work with firefox 3.01 on mac
5th Sep 2008, 2:15am
That's really strange. I just tested it on 56 different browsers on 4 different platforms at browsershots.org - including Firefox 3.0, 3.1.2 and Safari 2.0.4 on Mac OS X (They don't cover Safari 4 beta). Out of all that lot, Flock 2.0b2 and Dillo 0.8.6 on Linux were the only two that didn't display it correctly. Can anybody else confirm that it doesn't work on a mac?
3rd Apr 2009, 1:28pm
I'm using a mac and for me it works in both Firefox and Opera, but it's not working in Safari(version 3.1.2)
7th Sep 2008, 11:48am
Interesting method. I just use "margin:auto" for my centering. I don't ever really need something vertically centered, just horizontally, but I can see where this would come in handy.
11th Sep 2008, 8:42pm
hi, thx for the info... i tried it and it worked fine.... i was even tempted to go back to using tables just to create this effect but dont need to now
17th Sep 2008, 10:23am
Great article, thanks!
18th Nov 2008, 9:45am
Thanks a mil, this bit of info was a lifesaver
Ciao
Marisa
21st Dec 2008, 12:08am
Simple is always the best option, Thank you so much for posting this great bit of useful code. It's just what I needed and I have already used it!
Cheers
Ben
6th Feb 2009, 2:28pm
If you make the browser window smaller than the div, the upper part of the div will disappear. (viewed in FF3)
12th Feb 2009, 3:54am
I noticed that too. Problem was one of my clients pointed it out to me when they viewed my web site on a laptop at a low resolution.
This is where I used the code:
http://www.milkbottlephotography.co.uk
I wonder if you you could include an "if" statement that says if screen resolution is less that x, discount the verticle centering, or change the value so that the top will always show. Maybe an "if window size is less than 600px high....
I might look into it and if I find a solution I'll post it here.
7th Mar 2009, 5:51am
This is a great technique, which works fine in the browser. But with Firefox 3.0.6, it fails when you print (the page is blank). Oddly, IE6 is okay with printing.
I haven't yet found a vertical centering technique that works with printing in FF.
14th Mar 2009, 10:31pm
Still borked on mac firefox
30th Mar 2009, 6:15pm
Doesn't seem to work in Chrome though. It's really taking that margin-left:350px seriously.
30th Mar 2009, 6:21pm
Doesn't seem to work in Chrome though. It's really taking that margin-left:350px seriously.
1st Apr 2009, 10:53am
For the not know height:
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
29th Apr 2009, 8:18am
GREAT! This is the only solution that worked for me.
It works perfectly in FF3.x and Safari 4 on Mac.
Thanks!
8th May 2009, 10:02am
Great article.
I was looking for some kind of this,. But I have another problem.
I have a div tag in my code and it should be show when there is an inactivity. Its working fine, until today I found a bug.
When I scroll my browser to the end, the div is still shown up.
Unless i scroll up (as a programmer, I know that div will be showing up but ofcourse user will not know) I dont know whether the div is shown.
How can I solve this?
I tried to put in your code and change, but could not :(
Thanks so much!!
5th Jun 2009, 1:17pm
really cool and easy! Also work for me in IE7 & Firefox
17th Jun 2009, 10:48am
Excellent, worked great for me, thanks for sharing it :-)
25th Aug 2009, 7:26pm
I'm having issues with it on safari 4 too ...
I've never had this problem when using this markup before (I only stumbled onto this site trying to find out why it wasn't working in Safari). There must be something safari doesn't like about my particular syntax or something; because strangely, a div right under the troubled one is working fine ... with essentially exactly the same CSS ...
Anyone found out what's causing this odd bug?
23rd Sep 2009, 4:51pm
Amazingly helpful! Thank you very much. Everyone else seems to really want to complicate the issue but this was nice and easy to understand.
Many thanks again!
29th Sep 2009, 11:24am
Spot on. You know it's the simple looking things that cause you grief and this is one of them.
Thanks for clearing this up Tom.
Please share your thoughts, comments and suggestions...