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 the page content.

<div id="outer">
  <div id="inner">
    [content here]
  </div>
</div>

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.

Peter's picture

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?

tom's picture

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.

neil's picture

great article i'll definately use this tip thanks!

Anonymous's picture

on safari 4 beta on mac do not work, and still not work with firefox 3.01 on mac

tom's picture

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?

Anonymous's picture

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)

Corey Freeman's picture

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.

Anonymous's picture

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

Anonymous's picture

Great article, thanks!

Stuart's picture

Great article! - Keep up the good work ;-)
Cheers, Stuart.
Website Design & Development - Edit Studios

Marisa's picture

Thanks a mil, this bit of info was a lifesaver
Ciao
Marisa

Chuck's picture

Thanks. It's so simple that it works!

Ben May's picture

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

Jonas's picture

If you make the browser window smaller than the div, the upper part of the div will disappear. (viewed in FF3)

Ben May's picture

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.

Chuck's picture

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.

Anonymous's picture

Still borked on mac firefox

Webby's picture

Doesn't seem to work in Chrome though. It's really taking that margin-left:350px seriously.

Webby's picture

Doesn't seem to work in Chrome though. It's really taking that margin-left:350px seriously.

Anonymous's picture
Anonymous's picture

GREAT! This is the only solution that worked for me.
It works perfectly in FF3.x and Safari 4 on Mac.

Thanks!

Anonymous's picture

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!!

Pravin's picture

really cool and easy! Also work for me in IE7 & Firefox

ninjatoe's picture

Excellent, worked great for me, thanks for sharing it :-)

Post new comment

The content of this field is kept private and will not be shown publicly.
If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.