Sassafrass
« stick it to the robots: email munging
» 21 “free” photo galleries

Web Design

Internet explorer is a jerk: css hacks

posted by Jon | Wednesday, November 21, 2007 | 15 Comments
printer-friendly printer-friendly | e-mail post

e-mail this post





e-mail this post

Jerk SauceInternet Explorer 6 is a jerk. It’s one of the first things I discovered upon coming to web design from the video world. Video production has its weirdness, but once you render something, you don’t have to worry about people’s TVs changing your font sizes or column alignments behind your back.

Anyway, I’ve been looking for a good way to send one CSS rule to IE6 and another to the rest of the free world (the standards-compliant browsers like Firefox). Like most design-related things, there are about a million ways to do it, but I have a favorite: the [id] attribute.

Here comes the [id]

When you add the [id] attribute to the end of a CSS selector, Internet Explorer 6 (and older) will ignore the rule. Assume you have a CSS rule like this :

p { color: red; }

Obviously, all the text in the <p> tags will be red. But if you wanted Firefox to display in blue and Internet Explorer 6 to display paragraphs in red, you’d do this:

p { color: red; }

p[id] {color: blue; }

[id] in action

There’s no good reason to do that. Pretty much all browsers display paragraph text in the same way. But there are lots of reasons you’d want to send different rules to different browsers. This blog is one example. The default version of the template we use (from UpstartBlogger.com) has two columns inside a #container that is 920 px wide. Ideally, the columns sit side-by side, like this:

ideal two-column layout

I like this theme a lot, but after I made some other changes, I thought the columns were a little too far apart. There’s better ways to do this, but I decided to just shrink the width of the #container from 920px to 820px. Big mistake, sort of. Firefox was fine. But when I tested in IE6, the columns were now stacked on top of one another, like this:

Layout all screwed up by IE6

The Compromise

So here’s the problem in a nutshell: If I set the #container to 920px, Internet Explorer is fine, but the columns look too far apart in Firefox. If I make the columns the way I like them in Firefox, they sidebar will be pushed under the main content when viewed in IE6.

A Solution: Pass one set of rules to IE6, and another set to everybody else. Make the following CSS changes:

#container { width: 920px; }

#container[id] { width: 820px; }

Firefox and the good guys see the second rule, the one that uses the narrower layout to keep the columns closer together. Internet Explorer ignores the second rule, and keeps the width at 920px, so the columns remain side-by-side.

For the record, the [id] trick should work on the following browsers:

* Netscape 4.x
* Mac IE 4.01
* Mac IE 5
* Win IE 6 and lower
* Mac iCab 2.51
* Win Opera 3.60
* Win Amaya 5.1

I’m not sure exactly why Internet Explorer renders this layout differently (although I believe it has something to do with how it processes the combination of floated <divs> with fixed widths). If you’re interested, A List Apart has a great amount of CSS layout resources and explanations, and there are many more out there.

Also, I’m pretty sure that out of the box, the theme we use works fine in all browsers; it’s just that I made other cosmetic changes that broke the layout in a weird way.

More Info: Floatutorial | CSS Hacks | Quirksmode

Feed Subscribe to the Sassafrass Feed

RELATED STORIES:

15 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Or not. Whatever. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« stick it to the robots: email munging
» 21 “free” photo galleries