|

Add IE Conditionals to wp_enqueue_style

When creating a WordPress theme, it’s best practice to use wp_enqueue_style for adding all stylesheets, including style.css. At first glance, this can pose a challenge if you want to conditionally include CSS based on the browser (like IE-only styles, for example). Luckily, there is a quick way to do this in WordPress using $wp-styles:

global $wp_styles;
wp_enqueue_style( 'jlc_ie_styles',get_template_directory_uri() . 'css/ie-style.css', array(), '1.0.0' );
$wp_styles->add_data( 'jlc_ie_styles', 'conditional', 'IE' );

The code above calls on the $wp-styles class to associate our IE-only stylesheet (by tag/name/slug) with a condition, the condition being “IE.” If you wanted IE 9 and below, you could do this:

$wp_styles->add_data( 'jlc_ie_styles', 'conditional', 'lte IE 9' );

This is a great (and best practice) way to conditionally call styles. You can see more examples with comments over in this gist by wpscholar.

Similar Posts

  • | |

    Rock-Solid WordPress 3.0 Themes using Custom Post Types

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!The good people over at Nettuts+ have published a tutorial I wrote for them called Rock-Solid WordPress 3.0 Themes using Custom…

  • Writing a User log-in

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Here is the user login code I have been promising. It’s an image so it looks nice. You can download it…

  • |

    Fix wp_nav_menu on Custom Type Archives in WordPress

    Recently I was working with an issue in WordPress where the site’s menu was not showing up on an archive page for one of my custom post types. There were a few troubleshooting things I tried, including the most common recommendation, use theme_location instead of menu when referencing the menu in your theme (code after…

  • |

    Make an Easy Membership Site with WordPress and Patreon

    This week I wrote about how I’m doubling down on Patreon to deliver more quality content to my backers. Well, things have just gotten a lot easier for me, because their timing is impeccable. Patreon has recently release a WordPress plugin that allows you to take posts on your blog and make them viewable to…

  • | | |

    Learning HTML and CSS (with My New Book, Out Now!)

    I’ve always been a learn-by-doing kind of guy. Maybe it’s because I’m thinking about other things when something is explained to me, or maybe people are bad at explaining things. But either way, a concept doesn’t really sink in until I do it, or until I see an immediate application for it. When I learned…

  • |

    A Year of Responsive Design with WordPress

    A little over a year ago, my second book (and the first one to actually be in book stores) came out. It’s called Responsive Design with WordPress and it’s all about how to leverage WordPress to create responsive themes. It’s been an incredible year and I wanted to talk about two things: what has happened since the book’s release,…