Vertically center content with CSS
January 31st, 2008 by Verne
A convenient benefit of using tables is the ability to vertically center content within a cell using the valign attribute. Unfortunately, acheiving the same effect with CSS isn’t so convenient. So, as we continue to move towards tableless structures, there comes a need for a simple and valid CSS alternative. There have been a number of ways to approach this feat, but in this tutorial, I will lay out the technique we use to vertically center content using CSS.
The HTML
Three divs are required in order to pull this one off. Here’s an example of how we setup the divs.
<div id="container">
<div id=”position”>
<div id=”content”>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec laoreet egestas mi. Aliquam erat volutpat. Aliquam erat volutpat. Suspendisse potenti. Suspendisse potenti. Vestibulum mi nibh, hendrerit nec, feugiat vitae, feugiat a, nisl.
</div>
</div>
</div>
The #container div is used as the main reference point for centering the content. In other words, the content will be vertically centered against the height of the #container div.
The #position div is used to, naturally, position the content.
Finally, the #content div is used to encase the actual content that we are trying to center. This div is also used to correct the positioning of the content for Internet Explorer users (as you’ll see later).
The CSS
Here’s the CSS we apply to make it all come together:
<STYLE type="text/css">
#container { width: 100%; height: 100%; display: table; }
#position { display: table-cell; vertical-align: middle; width: 100%; text-align: center; }
</STYLE>
We’ve defined the height of #container to be the full height (100%) of the page. We could have just as easily given it a fixed height such as 500px. We’ve also added display: table and display: table-cell to #container and #position, respectively, which will tell it to mimic the properties of <table> and <td>.
You’ll notice that we haven’t applied any style to the #content div. This is because browsers like Firefox and Safari are able to use the uber handy vertical-align: middle property, making it almost just as easy as using tables. Using only the code we’ve shown thus far, our content should be nicely vertically centered on the page in all browsers except Internet Explorer, which doesn’t understand vertical-align: middle.
To correct this problem and ensure that our content is still vertically centered for IE users, we’ll apply the following conditional statements and extra bits of CSS.
<!--[if IE]>
<STYLE type="text/css">
#container { position: relative; }
#position { position: absolute; top: 50%; }
#content { position: relative; top: -50%; }
</STYLE>
<![endif]-->
The <!--[if IE]> ... <![endif]--> tags tell the browser to only process this CSS if the browser being used is Internet Explorer.
When processed, the CSS places the top margin of the #position div halfway down the height of #container. However, this only places the content in the lower half of #container. So, to center it vertically, #content’s top is offset by -50%. That is, it is being shifted upwards a distance that is equal to 50% of its height. If you do the math and crunch the numbers in your head, you’ll find that this does a nice job of centering the content.
Live Demo
A live demo of this tutorial is available here. You can also download the demo page for your reference.


Skylog » Blog Archive » links for 2008-02-05
February 5th, 2008 at 1:18 am[…] Vertically center content with CSS (tags: css) […]
Weekly Links - February 9th | Vandelay Website Design
February 9th, 2008 at 9:50 am[…] Vertically Center Content with CSS from Vdot Media. […]
moneyblog » Weekly Links - February 9th
February 16th, 2008 at 11:51 am[…] Vertically Center Content with CSS from Vdot Media. […]
Andy
February 26th, 2008 at 4:58 amThanks for an excellent, clearly explained demonstration. Presumably this is the sort is issue we’ll have to use MS version targetting to get to work in IE8 (assuming MS ever supports table-* css!).
Best Of February 2008 | Best of the Month | Smashing Magazine
February 29th, 2008 at 6:05 am[…] Vertically center content with CSSA convenient benefit of using tables is the ability to vertically center content within a cell using the valign attribute. Unfortunately, acheiving the same effect with CSS isn’t so convenient. So, as we continue to move towards tableless structures, there comes a need for a simple and valid CSS alternative. […]
mefisto
February 29th, 2008 at 7:40 am… but it has scrollbar in opera, so its useless crap IMO.
purrl.net |** urls that purr **|
February 29th, 2008 at 9:14 amThis is one of the web’s most interesting stories on Fri 29th Feb 2008…
These are the web’s most talked about URLs on Fri 29th Feb 2008. The current winner is …..
Tilman
February 29th, 2008 at 10:00 amExcellent solution! I really wondered if anybody will come up with a simple solution that works on all browsers without javascript or some other “dirty” things… You did!
roScripts - Webmaster resources and websites
February 29th, 2008 at 10:15 amVdot Media - Vertically center content with CSS…
A convenient benefit of using tables is the ability to vertically center content within a cell using the valign attribute Unfortunately acheiving the same effect with CSS isn t so convenient but this tutorial should make it a little easier…
Peter V Cook
February 29th, 2008 at 11:04 amIt should be noted that this only works with HTML Transitional Doctype:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">XHTML or other HTML doctypes won’t work, even
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">Andy
February 29th, 2008 at 11:30 amWell spotted Peter. Does anyone know how to make this work with XHTML Strict?
Matt
February 29th, 2008 at 12:59 pmThe doctype switching is in the handling of the “height: 100%;” … HTML Tran. recognizes height: 100% in most browsers…
theoretically couldn’t you just use the ie “hack” on all borwsers and forego the conditional statement? (untested per se, just more of a curious observation and initial reaction.)
Otherwise it looks like javascript will have to play a role unless everything is fixed height.
Metaholic » Blog Archive » Vdot Media - Vertically center content with CSS
February 29th, 2008 at 1:00 pm[…] [original post | via Delicious] […]
Infect The System
February 29th, 2008 at 1:30 pmAnd why not just use this CSS code instead?
width: 500px;
margin: 0 auto 0 auto;
Gives you centered content, with a whole hell of alot less code.
Make Money Online
February 29th, 2008 at 2:47 pmCongrats on making the frontpage of del.ic.ious!
shmack
February 29th, 2008 at 4:30 pm“Infect The System” - learn reading - the whole point of this article is to demonstrate “how to center VERTICALLY”.
“Gives you centered content, with a whole hell of alot less code.” - it also requires “a whole hell of alot” more brain to see the difference between horizontal and vertical.
John Faulds
February 29th, 2008 at 9:58 pm@ Peter: the doctype on the page is actually incomplete; if you use a full transitional doctype it doesn’t work either (in either IE or other browsers). So from my point of view, not much use for this technique.
There is of course a way to do this with a valid doctype and it’s actually pretty close to the technique described here: http://www.pmob.co.uk/pob/hoz-vert-center.htm
Centering Content Vertically On A Web Page | Web Tech Daily
February 29th, 2008 at 11:03 pm[…] Vertically center content with CSS […]
Infect The System
March 1st, 2008 at 1:21 am@ Shmack
Oopps, so I misread.
You can be as insulting and angry as you want. Don’t bother me. :)
At the same time, “learn reading”? How bout you, “learn writing”.
fcicq's del.icio.us » Blog Archive » links for 2008-03-01
March 1st, 2008 at 4:29 am[…] Vertically center content with CSS (tags: css tutorial webdesign layout) […]
links for 2008-03-01 « toonz
March 1st, 2008 at 6:20 pm[…] Vdot Media - Vertically center content with CSS (tags: css tutorial vertical center layout) […]
Ferg
March 1st, 2008 at 7:47 pmI have been using the solution here and it seems to work with XHTML 1.0 strict:
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
To adapt the solution to this example’s markup…
*{margin:0;padding:0}
html,body{height:100%;}
#container{height:100%;width:100%;overflow:hidden;position:relative;}
#container[id]{display:table;position:static;}
#position{position:absolute;top:50%;}
#position[id]{display:table-cell;position:static;vertical-align:middle}
#content{position:relative;top:-50%;}
#content[id]{position:static}
The code below works in Internet Explorer 5.0, 5.5 and 6.0, in Gecko browsers (Mozilla, Firefox, Netscape 7), in Opera 7, Konqueror 3.3.1. (maybe lower too), and in Safari. The page can be HTML or XHTML, standard or quirk mode.
Both examples don’t work in IE 5.2 for Mac.
oil
March 3rd, 2008 at 4:34 amto Ferg:
It doesn’t work in IE7!
?CSS????????? | ????
March 3rd, 2008 at 1:52 pm[…] ???????????????????????????????????? <table>???valign????????????????????<table> ??????????????????????????????????????????Verne?????????????CSS?????????? […]
55disk » CSS tutorials - links
March 4th, 2008 at 10:31 am[…] Vertically center content with CSS […]
Jermayn Parker
March 4th, 2008 at 11:16 pmVery cleaver indeed but it is very hacky!
My only problem is that I fail to comprehend any situations were this could be used successfully for a feature or look on a website.
Internet Brain » Vertically center content with CSS
March 5th, 2008 at 12:42 pm[…] Vdot Media - Vertically center content with CSS A convenient benefit of using tables is the ability to vertically center content within a cell using the valign attribute. Unfortunately, acheiving the same effect with CSS isn’t so convenient. So, as we continue to move towards tableless structures, there comes a need for a simple and valid CSS alternative. There have been a number of ways to approach this feat, but in this tutorial, I will lay out the technique we use to vertically center content using CSS. […]
Allstar's Reggae Band
March 7th, 2008 at 9:23 amWell, if you declare the right doctype like this:
it doesn’t work anymore, so it’s not the right solution. This isn’t html 4.01, it only works in quirk mode! I always use negative margins, but if the browser’s viewport is smaller than the s in the website, a part of it will disappear on the top of the website, so that’s not good either. It’s just not possible to center vertically with web standards (preferably xhmtl). Why is it so important to center something vertically? This isn’t a book or a leaflet. It’s a screen…
Allstar's Reggae Band
March 7th, 2008 at 9:25 amUgh, my doctype declaration just disappeared in my last comment. I meant:
!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd”
Fatih Hayrio?lu’nun not defteri » 08 Mart 2008 web’den seçme haberler
March 8th, 2008 at 7:50 am[…] ?çeri?i css ile dikeyde ortalamay? anlatan bir makale. Ba?lant? […]
Kolz Blog » Blog Archive » Best Of February 2008
March 12th, 2008 at 8:27 pm[…] Vertically center content with CSS A convenient benefit of using tables is the ability to vertically center content within a cell using the valign attribute. Unfortunately, acheiving the same effect with CSS isn’t so convenient. So, as we continue to move towards tableless structures, there comes a need for a simple and valid CSS alternative. […]
polarizer
March 19th, 2008 at 6:10 amNice, despite the fact that i’ve a right scrollbar here on my firefox 2.0.0.12 on Ubuntu 7.10. May be it’s caused by the additional place the firefox web developer extension bar requires?!
the polarizer
PS: The scrollbar only allows to scroll approx. 5px up/down.
????? ?? ??? » links for 2008-03-30
March 29th, 2008 at 10:26 pm[…] Vdot Media - Vertically center content with CSS (tags: css css-vertical-align) […]