|

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

  • |

    Win a Free Ticket to WP Summit!

    Last week I talked about the tail end of my speaking tour (thanks to everyone who came out to @WordCampPhilly). Up next is @WordCampChicago, but I’ll also be speaking at the WP Summit 2014, an online conference, on Wednesday, June 18th and I want to give away a free ticket!

  • |

    I’m Working at Crowd Favorite

    …or, “I really can’t wait to work for Chris Lema.”* Today, I’m very excited to announce that starting in September, I will be a Front End Developer at Crowd Favorite.  I have been following the company since Alex King started it, love the work they do, and could not be happier to join a team…

  • |

    How I Built my WordPress Development Course on Udemy

    My course has been out for 6 weeks and I’ve been promoting it on social media and in my newsletter; if you subscribe to any of those places you know it’s currently available. The cat’s out of the bag. But that doesn’t mean I don’t have more to tell about it. See, I originally set…

  • |

    Quick Tip: Add a Widget to 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!A couple of weeks ago I was working on a WordPress site and thought it might be nice to add a…

  • Articles I Wrote for Other Sites: Summer 2012

    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 Appstorm Network Favorite: What is Google Now and How Do I Use It? (Android) Access and Edit Files with Google Drive (Android)…

  • | | |

    How I Built the “WordPress: Year in Review” Website

    Earlier this month I launched WordPress: Year in Review, a content project where I’m wrapping up everything that happened with WordPress and in the community this year through an eBook, podcast, and video series. It’s an immense undertaking, so I decided to seek community support through sponsors and crowdfunding. We’re 3 weeks into September and I’m nearly 2/3 of the way to my crowdfunding goal which is exciting! I thought I’d take some time here and explain how I built the site.