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:
html
<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:html
<!--[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 sourcehtml
<link href="all-styles.css" rel="stylesheet" />
It contains the fixes for different IE versions:
css
.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:html
<!--[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:css
.lt-ie9 .card {
/* Styles for IEs before 9 */
}
Questions? 🙋
Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
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