The #id.class selector in IE6 28.08.2008 @ 02:11pm 3 comments

The ability to style an element using it's #id and several associated classes is often something that people believe is not possible in IE6. In some ways, that is true but it is possible to get this working with XHTML and CSS alone.

An example of what I am talking about can be seen below:

#feedback {
    background-color: #DFDFFF;
    border: 1px solid #4947BF;
    padding: 10px;
    }
#feedback.error {
    background-color: #FFBFC8;
    border-color: #AF4C59;
    }
#feedback.success {
    background-color: #CEFFBF;
    border-color: #64AF4C;
    }
<div id="feedback" class="success">
    <p>This is a success message</p>
</div>

Using the above XHTML markup you will notice that IE6 applies the #feedback styling and completely ignores the #feedback.success CSS. It is really quite strange how IE6 decides which rule to apply. For example, if my feedback div had the 'error' class it would apply the #feedback.error styling. So, in simple terms IE6 basically says "Check to see if the first #id.class rule matches and if so, apply it. If not, ignore the other #id.class rules and just apply the #id rule".

In order to get IE6 to pay attention to the other #id.class rules, you have to add another element inside your #feedback element that wraps it's containing markup and then tweak the CSS slightly... it's a pain, but it works. For example:

<div id="feedback" class="success"><div>
    <p>This is a success message</p>
</div></div>

Then the CSS should be tweaked to read as follows:

#feedback div {
    background-color: #DFDFFF;
    border: 1px solid #4947BF;
    padding: 10px;
    }
#feedback.error div {
    background-color: #FFBFC8;
    border-color: #AF4C59;
    }
#feedback.success div {
    background-color: #CEFFBF;
    border-color: #64AF4C;
    }

And voila! IE6 gets it... finally!

What people think...

There have been 3 comment(s) on this entry so far. Have something you'd like to say? Feel free to comment below.

Duryodhan said...
17.12.2008 @ 01:13am

Hello
Your blog is very good ,
I am Duryodhan from orissa India
I am workin as a SEO Spacalist
I want talk to U give your persional mail id

Alec Gorge said...
09.09.2008 @ 05:58am

Very neat. It makes you wonder how does microsoft make a rendering and layout engine so buggy that simply adding a child element fix the problem.

Jason Cole said...
29.08.2008 @ 03:20pm

That is a great little trick.

Add your thoughts

Fields in bold are required.

Add a comment



Who am I?

I am Jenna Smith - a front end web developer who has been busy coding XHTML & CSS for the last 6 years. If you are interested in seeing my work or getting in touch you can do so through my growldesign website.