Solstice Coil's first video. Written by Shir Deutch and directed by Amir Armel in the summer of 2006, it recaptures the story of the protagonist of the graphic novel that appears on the album, only from a slightly different angle.
Solstice Coil
Horizontally centering content with dynamic width in CSS
Update!
Long overdue, but this solution now finally uses semantic HTML! Yey!
Just returned from a lecture by Christopher Schmitt at the ISOC convention, which was really interesting. He introduced and covered several aspects of CSS to a tough Israeli audience, a congregation that found itself mostly dumbfounded by this "new" technology. Heh, It's about time someone gave those people a nudge. My only caveat with the lecture that he only showed slides and not how live code behaves in a browser. I believe it would have been much more effective had he showed a simple web page and illustrated changes on the fly with Firebug instead of just looking at the code, but maybe that's just me. Anyhow I promised I'll send him my solution to centering content with dynamic width so here it is!
Centering in CSS always requires some amount of work. It's rather easy when you know the exact dimensions of the element you're trying to center - but what happens if you want to horizontally center something of unknown width? For example - a list of links in a footer or a header? After much testing and fiddling around I've come up with a solution that is both viable and cross browser.
The markup goes a little something like this:
<div class="centeringContainer">and the CSS:
<div class="centered">
My Content
</div>
</div>
.centered{
margin-left: auto;
margin-right: auto;
display: table;
} So what happens here? We're giving our centered span element a display type of "table" rather than "block" or "inline", and since "table" knows how do be centered with auto margins, so does our div. Problem solved...
...or is it? Seems like IE always has a party to poop on and it doesn't like "display: table". Luckily, we can use "display: inline" to switch the div render type from "block" to "inline". This of course requires the entire content to be centered using "text-align: center", hence the centeringContainer div. On top of that, and because of the IE hasLayout bugs we need to enforce the weird propiteray "zoom: 1". This in essence changes nothing in the rendered element, but it does set its hasLayout value to true. So we create an IE only stylesheet file that has this:
.centered{
display: inline;
zoom: 1;
}
.centeringContainer{
text-align: center;
} Slap on conditional comments for IE browsers only that override the default:
<!--[if IE 6]>
<link rel="stylesheet" href="ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" href="ie7.css">
<![endif]-->
And there you have it. The reason I have two separate conditional stylesheets is just force of habit, since usually I find myself having to rely on these to fix specific bugs in IE6\IE7.
Click here for a clean live example.
Comments
(no subject)
Thanks for the update Opher. Looks like you found the way to make it work with a div in IE.
html validity
A need to use this technique again brought me back here. I can't believe the the nerve of the previous poster - talk about arrogant!!
I did try using div instead of span for the centered container (which would make it perfectly valid HTML) and it seems to work fine in all browsers except IE so I'm sticking with span for now but there is probably a way to make it work using a div in IE.
Anyway this is the simplest technique for centering a block of undefined width that I've come across so it has a place in my toolbox.
2nd stylesheet for IE may not be necessary
I find that this can work without conditional comments and special stylesheets.
.centered {
margin-left: auto;
margin-right: auto;
display: table;
display: inline-block;
}
.centeringContainer{
text-align: center;
}
This code works in both IE6, IE7 & Firefox (also tested in Safari and Opera)
Cheers,
Aidan
sww.co.nz
Great!
Thanks, this is nice!
(no subject)
Float automatically makes divs shrink wrap, so all you need to do is to float their container as well. I hope that's what you mean..?
Shrink Wrap 2 divs
Any way to shrink wrap to divs that are floated left. I want to center two divs with unspecified widths (floated left).
Thanks.
Shrink Wrap
Anyway to shrink wrap two divs that are floated left? Basically, I want to divs with unspecified widths (floated left) to be centered...?
Woah
Talk about some bitterness...
I agree that this indeed uses some invalid HTML, but no need to go postal. There are some more important things in life....
You'll be happy to hear though that I've found a way to to the same simple thing with valid HTML, that works in all browsers (including that damned IE).
Hopefully I can find a time during the weekend to update the new solution. Check back here soon.
Horizontally centering
The solution on this page depends on invalid HTML because it contains a block element inside an inline element i.e. ul within span. Can't be used in dynamic HTML e.g. with AJAX.
There is a tutorial on centering ul, horizontal lists at:
http://css.maxdesign.com.au/listutorial/horizontal_master.htm
I would suggest to remove this article because it is arrogant and creates unnecessary confusion.