Watch: Using Modernizr CSS Classes

Share this article

In this video I’ll introduce you to how Modernizr can help you style your website’s CSS based on the current browser’s feature support. Modernizr will add all the results of the detected features as classes to the html
element. In this lesson we’ll target browsers that support emoji and turn a boring bullet list into a fun emoji list. After watching this screencast you’ll have a basic understanding of how to use Modernrizr’s CSS classes to conditionally style elements on your page. The code sample for this lesson can be found at on GitHub. This is one lesson from SitePoint Premium’s latest course Modernizr 3. If you enjoyed this video and want to see more, come and check out the rest of the course.
Loading the player…

Frequently Asked Questions about Using Modernizr CSS Classes

What is Modernizr and why is it important in web development?

Modernizr is a JavaScript library that detects HTML5 and CSS3 features in a user’s browser. It’s important in web development because it allows developers to take advantage of cutting-edge features in HTML5 and CSS3, while also providing fallbacks for less capable browsers. This ensures that your website functions properly across all browsers and devices, providing a seamless user experience.

How does Modernizr work?

Modernizr works by running a series of tests on the browser’s capabilities upon page load. It then adds classes to the HTML element that indicate whether these features are supported or not. This allows developers to write conditional CSS and JavaScript to handle different browser capabilities, ensuring a consistent user experience.

How do I install and use Modernizr in my project?

To install Modernizr, you can download it from the official website or use a package manager like npm or Bower. Once installed, you simply include it in your HTML file using a script tag. You can then use Modernizr’s API to test for specific features and apply different styles or scripts based on the results.

Can I customize Modernizr to only include the tests I need?

Yes, Modernizr is highly customizable. You can select only the tests you need on the Modernizr download page, which will generate a custom build for you. This helps to keep your file size down and improve performance.

What are some examples of how to use Modernizr in CSS?

With Modernizr, you can write CSS rules that target specific browser capabilities. For example, you could use Modernizr to detect whether the browser supports CSS gradients, and provide a fallback color if it doesn’t. This would look something like this:

.no-cssgradients .element {
background: #F00;
}

.cssgradients .element {
background: linear-gradient(to bottom, #F00, #000);
}

Can I use Modernizr with JavaScript?

Yes, Modernizr can also be used with JavaScript to conditionally load scripts or execute code based on browser capabilities. This can be useful for providing polyfills for unsupported features, or for optimizing performance by only loading scripts when necessary.

How does Modernizr help with progressive enhancement?

Progressive enhancement is a web design strategy that emphasizes core webpage content first, then progressively adds more nuanced and complex layers of presentation and features on top of the content. Modernizr aids in this approach by allowing developers to easily provide fallbacks for unsupported features, ensuring that the core content and functionality of your site is accessible to all users, regardless of their browser’s capabilities.

What are some common pitfalls to avoid when using Modernizr?

One common pitfall is over-reliance on Modernizr for feature detection. While Modernizr is a powerful tool, it’s not a silver bullet and shouldn’t be used as a substitute for good coding practices. It’s also important to remember that not all features can be detected by Modernizr, and to always provide appropriate fallbacks.

How can I contribute to the Modernizr project?

Modernizr is an open-source project, and contributions are always welcome. You can contribute by reporting bugs, suggesting new features, improving documentation, or submitting pull requests. More information can be found on the Modernizr GitHub page.

Where can I find more resources to learn about Modernizr?

The official Modernizr website is a great place to start, as it provides comprehensive documentation and examples. Other resources include various online tutorials, blog posts, and forums dedicated to web development.

Ryan SeddonRyan Seddon
View Author

Ryan Seddon is Tech Lead at Zendesk, an open source junkie and a CSS abuser. Coming from a specialist front-end role he's since ventured into full stack development. He regular contributes to open-source projects such as Modernizr, gives talks at conferences and is just all round enthusiastic about the software. His personal website is http://www.thecssninja.com

AdvancedCSSangelamInstructional videoModernizr 3sitepoint coursesSitepoint Premium
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form