|

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

  • | |

    Create a Temp/Splash Page in WordPress

    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!Often times I will be working on a WordPress theme that I want to live test, but I don’t want to…

  • | |

    2015: Resolving to Do More, and Less

    Every year I publish my list of resolutions as well as a scorecard from the previous year. For 2015, I’ve redesign the site (well, I used HTML5 Boilerplate) and broke my resolutions down into 2 categories: “Do More” and “Do Less.”

  • |

    How to Easily Search Your Tweets in Chrome

    Since moving back to Chrome, I’ve missed some of my custom searches in the Keyword Search extension for Safari, despite having the same support in TextExpander. It just didn’t feel as fast as native browser support. Luckily, Chrome’s search shortcuts are fantastic and super easy to manage from chrome://settings/searchEngines1. Did you know you can do…

  • Types of classes

    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!Today and over the last couple of days we have been talking about classes in Java. I’m fairly certain I understand…

  • |

    Why the WordPress.com / Spotify Deal is Bad for Podcasting

    Imagine telling Gordon Ramsay that he cooked dinner last night…why not just serve the leftovers to his customers today? While I’m no Michelin chef, I do create a lot of content. And you can’t just throw the left overs out there and expect the same quality content. So why are WordPress.com and Spotify telling us to do that?

  • | |

    Quick Tip: Facebook Privacy Settings

    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!Facebook, at their annual F8 Conference in San Fransisco, announced that they were expanding the reach of social networking to make…