The Hickensian
18.07.03 How to: vertical centering with CSS
For a long time I'd wanted to achieve a vertical centering effect with CSS that I used to achieve with tables or frames. A fixed size block that floats dead centre in the browser window, no matter what its size. Techniques for horizontal positioning have been known for some time, with the 2 methods described on Blue Robot. Then I found across a piece on Web Page Design for Designers, outlining how to do this. Nirvana! It uses absolute positioning to put find the exact centre point of the window, and then uses negative margins to 'shift it all back halfway', producing the effect. It works, but there are 2 problems with this:
the problems
- As the CSS technique uses absolute positioning with negative margins, if the browser window is smaller than the centered content, it crops it off without giving you scrollbars to access it. I wanted the site to get a long 'panoramic' style, but I also didn't want to exclude users with 800x600 resolution. The fact that some users couldn't even scroll to see content was a big problem.
- In Interner Explorer 5 for Mac, if an XHTML doctype is used instead of HMTL Transitional (as used in the Web Page Design for Designers article), the content gets uncereromoniously pushed off the top of the browser window. While IE5 Mac is being phased out now that development has been stopped, its still the main browser for OS 9 users.
the solutions:
the cut-off content problem
The WPDFD technique uses 2 container divs - #horizon for the vertical and #content for the horizontal. Originally, these 2 divs were needed due to a bug in Opera. While this has since been fixed in version 7, there is another use for the extra container div.
Instead of using absolute positioning to center horizontally, use the auto margin method to centre the second <div> within the first. This is the best way, as it stops trying to centre the content when the window is too small. This still cuts of the the left hand side in Mozilla/Camino/Firebird, but all it needs is a 'min-width' value adding to the containing <div> to stop this.
Here's the new CSS:
#horizon {
background-color: transparent;
position: absolute;
top: 50%;
left: 0px;
width: 100%;
margin-top: -200px;
text-align: center;
min-width: 900px;
}
#wrapper {
background-color: #fff;
position: relative;
text-align: left;
width: 900px;
height: 380px;
margin: 0px auto;
}
The addition of position:relative in the #wrapper rule allows me to position everything inside it using position:absolute rather than floats, which makes life easier and accurate. You may not need this however. This solves the problem of content being cut off horizontally.
As for the vertical - you just have to make sure that you specify a height that will fit in your target market's lowest screen resolutions. I used 380px, which should fit in most browsers at 800x600 (once you've allowed for all that browser chrome).
IE5 mac and the commented backslash hack
To solve the IE5 mac problem, you need to use the commented backslash hack. First you add your style rules to work in IE5 mac, then you add your real styles rules, beginning with a backslashed comment and ending with a normal comment. IE 5 Mac ignores everything between the 2 comments, and these override the previous rules, providing the centering effect for all the other browsers:
/* styles for IE 5 Mac */
#horizon {
background-color: transparent;
position: absolute;
top: 20px;
left: 20px;
}
/* following rules are invisible to IE 5 \*/
#horizon {
top: 50%;
left: 0px;
width: 100%;
margin-top: -200px;
text-align: center;
min-width: 900px;
}
/* end IE 5 hack */
#wrapper {
background-color: #fff;
position: relative;
text-align: left;
width: 900px;
height: 380px;
margin: 0px auto;
}
As we've already stated rules like 'position: absolute' in the first set of style rules, we don't need to repeat these, only redefine rules that we want to change. So far I haven't been able to replicate the vertical centering effect, but at least it centres horizontally.
Recent Posts
26.01.10 The Handbag has been raised!
22.01.10 Guide to the Internet (2000)
20.01.10 Designer, not a construction worker
19.01.10 Why you can never work 'full time'
16.01.10 Dream Report: Look at the Hygiene!
Or Full Archives
The Hickensian is the journal of Hicksdesign, a creative partnership of Jon & Leigh Hicks. Read more about us.
playlist
Hickr | RSS
Contact
Hicksdesign
Island House
Lower High Street
Burford
Oxfordshire, UK
OX18 4RR
+44 (0)7917 391 536
I am currently working full-time with Opera, and not taking on any new projects
17 comments
Journal RSS Feed





Download vCard
Comments | RSS
∞ Scott Johnson said 2392 days ago:
Nice article! I can't wait to try this out for my own sites. (My blog certainly needs a redesign. :)∞ Will said 2368 days ago:
Nice article, I've been frustrated by vertical alignment for a long time. It's not perfect, but as I design sites with CSS for layout, I'm both familiar with the necessity of 'hacks' and happy to have it.Thanks for the Web Page Design for Designers link too - a resource until now I've been unaware of.
∞ Jeremy Fuksa said 2346 days ago:
I've been working with this example, which seems to be working perfectly in IE6 Win, but does not work in Mozilla Firebird 0.6.1 Win.Any suggestions?
∞ Jeremy Fuksa said 2346 days ago:
never mind... the problem was with an outline style that I had made to wrap around the wrapper. I fixed the problem on my own.∞ Jon Hicks said 2346 days ago:
Great - glad to hear you got it working!∞ bongoman said 2224 days ago:
So does the wrapper div replace the content dic used in the original article?∞ Jon Hicks said 2224 days ago:
Bongoman - thats right, I just preferred 'wrapper' because my actual content div is further down the page.∞ alex lee said 2224 days ago:
holy jumping jetfish! that is just about the best vertical center hack I have ever seen!I used to use the css margin hack, but would then cut off a considerable number of people because they couldnt scroll to what they couldnt see...
so I downloaded studioVII's dw extension layout designer which did the job, but with nothing less than 50 lines of tightly packed java, and only to find out it looks completely messed up in opera.
so you showed me how to do it with just CSS! in short, this is gRRRRReat! I thank you 10k times for this extraordinary help!
btw where can I get this commenting script, it looks pretty nice!
∞ Jon Hicks said 2221 days ago:
Alex -Glad it worked for you! For live previews:
http://www.hicksdesign.co.uk/journal/archives/000317.php
∞ Adam Bell said 2213 days ago:
Excellent work on this one, Jon. I appreciated the pointer!∞ David Laakso said 2212 days ago:
A beginner, I find articles such as this extremly helpful. Thanks!∞ mike said 2210 days ago:
this would be a bit shorter, same effect. even works in IE5mac with XHTML doctype:#horizon {
position: absolute;
left: -half widthpx;
top: -half heightpx;
width: 100%; height: 100%;
}
#wrapper {
position: absolute;
left: 50%; top: 50%;
width: widthpx;
height: heightpx;
}
btw, love your site ;-)
cheers,
mike
∞ Jon Hicks said 2209 days ago:
Mike,Interesting - It does work, but not in IE 5Mac (Strict or Transistional Doctype).
You can actually do away with the second DIV altogether, just combine negative margins with the left 50%, top 50% position in the one div.
The point is that this method means content disappears when its larger than the window. The method outlined above solves the problem horizontally by using auto margins instead. Just the vertical that's a problem...
∞ Ed said 2206 days ago:
You could of course ignore W3Cs pointless insistance on using CSS to do all the things we could already do far easier in HTML years ago.They only mess about with stuff to give themselves work for the next few years. Now they can earn a packet writing longwinded articles on how to achieve, with huge pieces of CSS, the simple things we used to be able to do in a single line of HTML.
It's about time we told them to stop buggering about with a perfectly good markup language and leave us to get on with the real work of providing well executed content.
CSS will always be an obscure, dysfunctional barrier between content and effective presentation. It can only make the job of the people who do the real work on the net, the content providers, hugely more time consuming and far less effective.
∞ Jon Hicks said 2206 days ago:
Its nothing to do with W3C. Its my decision to use CSS, and I don't think anyone earns money out of articles like this!"CSS will always be an obscure, dysfunctional barrier between content and effective presentation"
- that gave me a chuckle anyway
∞ Ed said 2206 days ago:
I notice you're own homepage doesn't crop correctly despite being something of a CSS guru with a workable solution detailed above.Perhaps it was too much effort to employ the twenty five line solution on this page to achieve what anyone can do with ordinary HTML in a couple of tags?
Or perhaps the fact this unnessecary intermediate technology requires you to employ such a longwinded solution gave you doubts about the validity of the approach?
Whatever the reason, if a CSS wizz such as yourself cannot make their homepage accessible via common scrollbars, it suggests the rest of us might avoid the use of CSS until it's been written to function in a more useable fashion.
∞ Jon Hicks said 2206 days ago:
I don't think this too long-winded. Sure it doesn't crop properly, but the amount of code needed is pretty small.The point here is to try out new methods - experiment, practice, refine. Methods that allow you to seperate content from presentation.
CSS is just fine. Its the browsers that cause the problems. Does that mean you shouldn't use and experiment with CSS? No.