The Juice Journal

A Safe, Flexible Replacement for the * html Hack

Sunday April 6th 2008

Time was when a web designer needed to know all the hacks to get his or her sites to display properly in all the major browsers (all right, Internet Explorer is really the main problem). Then along came IE7, and our hacks stopped working (IE fixed some of the rendering and parsing bugs, but not all), leaving us desperate to find a new way to target the atrocities of Internet Explorer.

Amongst others, we lost the * html hack.

The * html hack (starting a CSS selector with * html) allowed us to target CSS just at IE. And although it’s no more, we, as designers for the web, still need a way around the crap of IE.

Along came conditional comments, which had technically been around for years but only now really seemed useful. With IE’s proprietary conditional comments, we could serve up separate stylesheets or even content just to specific versions of Internet Explorer.

There are several reasons conditional comments aren’t awesome. For one, we must put them directly in our HTML, rather than in our CSS. Two, at first glance it seems like we’d need to create many different CSS files, one for each version of IE we need to target. These two downsides are a nuisance and make IE hacks comment-served-styles perhaps even less maintainable than before.

Then Hiroki Chalfant came up with a clever solution that gave us the ability to target IE directly within our CSS. Basically, use a conditional comment to serve an enclosing <div> with an id of "IERoot" around everything inside the <body>. Only IE would see the <div>, so only IE would have that clever #IERoot. Therefore, instead of using * html whatever, we could now use #IERoot whatever in exactly the same way. Clever clever.

This solution still leaves us with the problem of not being able to address specific versions of Explorer, something I need while creating templates for dreamscape. I’ve taken the (logical) next step from Chalfant’s method, and created separate <div>s for IE5, IE6, and IE7. It looks like this:

<body>
<!-- provides IE-named containers to allow IE-specific CSS -->
<!--[if IE 5]>
	<div id="ie5">
<![endif]-->
<!--[if IE 6]>
	<div id="ie6">
<![endif]-->
<!--[if IE 7]>
	<div id="ie7">
<![endif]-->

And then obviously we need to put this in the footer to close the <div>:

<!--[if lt IE 8]>
	</div>
<![endif]-->
</body>

Now in my CSS I’m able to make a declaration like this one (a replacement of Tantek’s famous Box Model Hack):

#ie5 div {
	padding: 10px;
	width: 220px;
	}
#ie6 div, #ie7 div {
	padding: 10px;
	width: 200px;
	}

As you can see, we can use this technique to specifically target any version of IE for just about any CSS rendering bug.

Shortcomings

I would have liked to make the <div>s have an id of "ie" and a class with the version number. That way we could target all buggy versions of IE at once, or just a specific version. However, old versions of IE (surprise surprise) don’t support selectors with both a class and an id, so no dice. If you want to target more than one version of IE, you have to write comma-separated selectors, which can occasionally get a bit long if you’re addressing something buried deeply in your HTML that needs a great deal of specificity. Meh.

I’ll update this post with other shortcomings as I discover them. Please feel free to comment with any further ideas you may have, or questions or concerns.

Shameless Plug

If you’d like to try this IE-targeting thing, but don’t really want to muck about with your code, or perhaps your CMS doesn’t give you the flexibility to add this sort of thing, you can download the latest versions of dreamscape cms from the SVN at Google Code page. The default theme, "The Neue Standard", has this feature built in.

Thank you.

UPDATE: I feel totally silly for accidentally misnaming the * html hack the “Holly Hack”. Which it’s not. The * html hack is also called the Star HTML Hack or the Tan Hack. I’ve updated this post. Thanks, Wojtek.

UPDATE 2: At John Lascurettes’s suggestion, I shortened up the closing <div> by making it a single conditional comment. Thanks, John.

{love},
{ryan}

4 Comments

1 Wojtek Z.

So you think nested, not needed wrapper elements are better than CSS that only IE browsers see? Is it worth to make our document tree more complicated? Do you really want to make your HTML uglier in order to save yourself from creating just one new stylesheet? (btw. you can place IE-only CSS directly in HTML document too, but that’s not recommended).

By serving additional CSS you don’t do anything wrong, while I personally think there is no point in additional unsemantic HTML elements.

With “* html”, you probably wanted to mention “star html hack”. Holly hack is applying hasLayout via { height: 100% }.

 —W.

Posted on Sunday April 6th 2008 at 3:36 pm

2 Ryan Miglavs

Wojtek,

Thanks for reminding me of my mistake over the Holly Hack / * html hack difference. I’ve been using both for so long I guess I just forgot that * html isn’t the Holly Hack. The Holly Hack normally uses “height: 1%;” to trigger hasLayout.

Yes, I find an overall wrapper element superior to as-needed wrappers for dealing with IE. This way I can always know the wrapper is there, but only for IE. And I’m not peppering my HTML with an absurd number of extra conditional comments or IE-specific cruft.

Note, too, that this method does NOT add anything to the document tree for browsers other than IE. Your valid, semantic HTML stays exactly the same in those browsers.

Although this method would work right in your single CSS file, I always put all IE junk in a separate file. This method lets me target specific versions within that file.

Posted on Sunday April 6th 2008 at 3:52 pm

3 John Lascurettes

No need to put all those separate if IE CCs at the end to close the div. Why not just make one general if IE? If you want to exclude the (alleged) compliant IE8, then just make it if LT IE8.

Posted on Monday April 7th 2008 at 8:13 am

4 Brownspank

Personally, I'm all for superfluous elements if it gets the job done (read: puts a smile on the client's face), but I'm not sure replacing HEAD cruft with BODY cruft is the best idea.

It's great though to be able to centralize your styles (including IE-specific ones) in one file.

And ditto on using just one conditional comment to close the div. That should clean it up a bit.

Posted on Tuesday April 8th 2008 at 3:23 am

Leave a Comment

No HTML is allowed. All links and new lines will be automagically converted to HTML for you.

= required


© 2008 A Socialist Pear
we ♥ dreamscape cms

XHTML
CSS