Yuhu's Definitive Solution with Unknown Height
Though there is a CSS property vertical-align, it doesn't work like attribute valign in HTML tables. CSS property vertical-align doesn't seem to be able to solve this problem:
No general solution was known until yesterday. I have found it going home on Wilson street. See an example in browser.
The keystone of the solution in Internet Explorer: the internal object is absolutely positioned in half of the area height. Then is moved up by half of it's height. The wrong interpretation of the height property in Internet Explorer is used (counted height is taken as a base of percentage height of nested tags). One extra nested tag <div> is needed for Explorer.
Solution for standard browsers like Mozilla, Opera, Safari etc. is completely different. Entire area (top <div>) is set to be displayed as a table (display: table; part of CSS2). The internal object is set as table-cell (display: table-cell). But -- there is the wit -- it is possible to use vertical-align property for such displayed element in standard browsers. (Internet Exlorer ignores those properties or doesn't know their values.)
Then both syntax are merged. I use the Pixy's underscore hack. A CSS property written with the underscore on the start is visible for Internet Explorer (all versions), but invisible for any other standard browser (e.g. Explorer interprets _position: absolute; unlike other browsers). Underscore hack seems to be valid, but if you don't want to use it, you may use the more structured code below.
The code below works in Internet Explorer 5.0, 5.5 and 6.0, in Gecko browsers (Mozilla, Firefox, Netscape 7), in Opera 7 and in Safari (Konqueror not tested yet). The page can be HTML or XHTML, standard or quirk mode.
Both examples don't work in IE 5.2 for Mac. As I haven't Mac, I can't test it. Please let me know (dusan@pc-slany.cz) when you'd find any workaround.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Universal vertical center with CSS</title>
<style>
.greenBorder {border: 1px solid green;} /* just borders to see it */
</style>
</head>
<body>
<div class="greenBorder" style="display:
table; height: 400px;
_position:
relative; overflow: hidden;">
<div style=" _position: absolute;
_top: 50%;display: table-cell; vertical-align: middle;">
<div class="greenBorder" style="
_position: relative;
_top: -50%">
any text<br>
any height<br>
any content, for example generated from DB<br>
everything is vertically centered
</div>
</div>
</div>
</body>
</html>
See this example in
browser
Legend for colors:
CSS styles for every browser
CSS styles for standard browsers
CSS style for Internet Explorer only (with
underscore hack)
Example above is not nice, but I hope you have understood it. It's possible to write code differently. For example this way:
<div id="outer">
<div id="middle">
<div id="inner">
any text
any height
any content, for example generated from DB
everything is vertically centered
</div>
</div>
</div>
And the structured valid style:
<style type="text/css">
#outer {height: 400px; overflow: hidden; position:
relative;}
#outer[id] {display: table; position: static;}
#middle {position: absolute; top: 50%;}
/* for explorer only*/
#middle[id] {display: table-cell; vertical-align:
middle; position: static;}
#inner {position: relative; top: -50%}
/* for explorer only */
/* optional: #inner[id] {position: static;} */
</style>
See the valid example in browser (it looks the same way as the last one).
Color legend:
CSS style for Internet Explorer only
CSS styles for standard browsers
CSS2 selector #value[id]
is equivalent to selector #value
,
but Internet Explorer ignores these types of selectors. Generally: syntax
*[foo]
means any element with attribute foo
. Any
HTML element #something must have the attribute id set to "something". That's
the trick -- #value[id]
works in standard browsers only (similarly
works .value[class])
There's CSS property position set to absolute or relative for Internet
Explorer. The code position: static
get's it back to default
value in standard browsers (property top doesn't work then).
The same way, and just add the style:
<style>
body, html {height: 100%;}
#outer {height: 100%; overflow: visible;} /* or without overflow */
...
</style>
It seems that #outer declaration should be sufficient, but it is not. Body and html declaration sets 100% of the window's height as a base for next percentage. Now it is better not to set overflow: hidden (like in the examples above), because when the content would be taller than window, then it would be impossible to scroll on.
I'll update this article with more information if you wish.
First published Sep 21, 2004, updated Nov 8, 2004
Dušan Janovský
aka Yuhů
dusan@pc-slany.cz
www.jakpsatweb.cz
Thanks for testing: Chose, Lukáš Mačí, Pixy and Daniel Wallace.
Anatoly Papirovsky has the same idea independently to me 5 hours later.