← Back toCross Browser

Conditional HTML classes

Written byPhuoc Nguyen
Created
06 Sep, 2022
The previous post introduces the technique to load styles for a given IE version. Let's revisit how the styles are organized:
<link href="styles.css" rel="stylesheet" />

<!--[if IE 8]>
<link href="fix-ie8.css" rel="stylesheet" />
<![endif]-->

<!--[if IE 9]>
<link href="fix-ie9.css" rel="stylesheet" />
<![endif]-->
As we can see, the approach adds one more HTTP request. For example, IE 8 will load `styles.css` and `fix-ie8.css` files as separate requests. We can reduce the number of requests by using the conditional comments for the root `html` element:
<!--[if lt IE 7]><html class="ie6"><![endif]-->
<!--[if IE 7]><html class="ie7"><![endif]-->
<!--[if IE 8]><html class="ie8"><![endif]-->
<!--[if IE 9]><html class="ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html><!--<![endif]--></html>
The root `html` element will have an additional CSS class depending on the current version of IE which visitors use. For example, IE 9 browser will add `ie9` CSS class. The styles are now combined into a single source
<link href="all-styles.css" rel="stylesheet" />
It contains the fixes for different IE versions:
.card {
background: rgba(0, 0, 0, 0.5);
}

/* Background with opacity is only available on IE 9 and later */
.ie6 .card,
.ie7 .card,
.ie8 .card {
background: #ccc;
}
The issue of the number of HTTP requests is resolved. However, the styles now contain many duplication codes. We can fix the issue by adding more CSS classes to the `html` element:
<!--[if lt IE 7]><html class="lt-ie7 lt-ie8 lt-ie9"><![endif]-->
<!--[if IE 7]><html class="lt-ie8 lt-ie9"><![endif]-->
<!--[if IE 8]><html class="lt-ie9"><![endif]-->
<!--[if gt IE 8]><!--><html><!--<![endif]--></html>
Additional styles for `card` on IE 6, 7, 8 now can be combined into a single class declaration:
.lt-ie9 .card {
/* Styles for IEs before 9 */
}

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn