How to Use Modernizr for Cross-Browser Compatibility
Let's face it building websites that work everywhere is no small feat. One browser supports something, another doesn't. One device renders perfectly, another squashes your layout like a pancake. That's where Modernizr comes to the rescue.
If you've ever wondered how to safely use modern web features like HTML5 input types, CSS Grid, or Flexbox without breaking your site in older browsers, then this guide is for you.
Let's break it all down: what Modernizr is, why it matters, and exactly how to use it—step by step—with real examples that are easy to follow.
What Is Modernizr, Anyway?
Modernizr is a JavaScript library that detects which HTML5 and CSS3 features a user's browser supports.
It doesn't fix compatibility issues directly, but it tells you what features are available in the browser, so you can choose to either:
- Use those features safely
- Offer fallbacks for browsers that don't support them
- Enhance the experience for users with modern browsers
Think of it like asking the browser, "Hey, can you do this?"—and then acting based on the answer.
Why Use Modernizr for Cross-Browser Compatibility?
Cross-browser issues are often caused by feature discrepancies. A browser might not support a specific HTML tag, CSS style, or JavaScript API.
Instead of guessing or targeting specific browsers (which is fragile and outdated), Modernizr helps you detect capabilities directly. This is called feature detection, and it's the most reliable way to handle browser compatibility.
Quick Example: Instead of writing "If the user is on Internet Explorer, do X," you write "If the browser supports Flexbox, do X."
How Modernizr Works (In Simple Terms)
- It checks for features like Flexbox, WebGL, SVG, or input types.
- It adds class names to your
<html>
tag, like.flexbox
,.no-webgl
,.cssgrid
, etc. - You can use those class names in your CSS to style elements differently.
- Or, you can use the Modernizr object in JavaScript to run fallback scripts.
This gives you full control over what your site does, depending on what the browser can actually handle.
How to Install Modernizr
You can include Modernizr in two main ways:
Use the Prebuilt Version (Quick Start)
Add this to your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/3.12.0/modernizr.min.js"></script>
This version includes common tests like Flexbox, Grid, and HTML5 features.
Create a Custom Build
Head to the Modernizr Download Builder and pick only the tests you need. This keeps your file size small and performance high.
Example 1: Using Modernizr in CSS
Let's say you're using CSS Grid to build your layout, but you know some users may be on older browsers that don't support it.
✅ CSS Example:
.grid-layout {
display: grid;
grid-template-columns: 1fr 1fr;
}
.no-cssgrid .grid-layout {
display: block;
}
What's happening here?
- If Modernizr detects CSS Grid, it adds the class
cssgrid
to the<html>
tag. - If not, it adds
no-cssgrid
. - You can style accordingly!
This ensures users without Grid support still see a usable layout.
Example 2: JavaScript Fallback for HTML5 Video
Say you're using the <video>
tag, but want to show a fallback message or Flash video for older browsers.
✅ HTML:
<video controls>
<source src="movie.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
✅ JavaScript with Modernizr:
if (!Modernizr.video) {
document.getElementById('fallback-video').style.display = 'block';
}
✅ HTML fallback:
<div id="fallback-video" style="display: none;">
<p>Please <a href="movie.mp4">download the video</a> instead.</p>
</div>
This is graceful degradation in action—if the feature isn't there, show something else that works.
Example 3: Detecting Input Types (like date pickers)
Let's say you want to use input type="date"
, which shows a date picker in most modern browsers—but not all.
✅ JavaScript:
if (!Modernizr.inputtypes.date) {
// Provide a polyfill or fallback
document.write('<script src="path/to/jquery-ui-datepicker.js"><\/script>');
}
This means older browsers will get a script-based date picker, while modern browsers will just use the native one. Clean and efficient.
Common Features You Might Want to Detect
Modernizr can detect dozens of features, but here are some popular ones:
Feature | Class Name | Use Case |
---|---|---|
Flexbox | .flexbox |
Layouts |
CSS Grid | .cssgrid |
Advanced grid layouts |
HTML5 Video | .video |
Media content |
WebP Images | .webp |
Image optimization |
SVG | .svg |
Scalable graphics |
Input Types | .inputtypes.* |
Form enhancements |
You can find the full list of features on modernizr.com/docs.
Pro Tips for Using Modernizr Effectively
Always Provide a Fallback
Modernizr doesn't fix the problem—it helps you respond to it. So make sure you have fallbacks in place for unsupported features.
Combine with Polyfills
For example, if !Modernizr.flexbox
, you might load a polyfill or adjust your layout using floats or tables.
if (!Modernizr.flexbox) {
// Load an alternate stylesheet or alert the user
}
Use with Progressive Enhancement
Start with a basic, functional version of your site. Then layer on enhancements that Modernizr confirms are supported.
How Modernizr Adds Classes to HTML
Here's what your HTML might look like after loading Modernizr:
<html class="no-js cssgrid flexbox no-webgl svg">
These classes give you incredible flexibility in your CSS:
.no-webgl .game-canvas {
display: none;
}
.webgl .game-canvas {
display: block;
}
You're not guessing. You're tailoring the experience based on facts.
When Not to Use Modernizr
While Modernizr is powerful, it might be overkill if:
- You're only supporting modern browsers and using evergreen front-end frameworks.
- Your site doesn't use any cutting-edge features.
- You already use other detection tools or frameworks that include feature detection.
That said, for developers targeting a wide range of browsers and devices, Modernizr is still a valuable tool.
Final Thoughts
Modernizr is like a smart assistant for your website. It doesn't fix cross-browser problems for you, but it gives you the knowledge to handle them like a pro.
By detecting features rather than browsers, Modernizr helps you create websites that are robust, future-proof, and user-friendly—no matter what browser someone's using.
So the next time you're unsure whether that fancy HTML5 feature will work for everyone, remember: Modernizr's got your back.
Key Takeaways:
- Modernizr detects features, not browsers.
- It lets you write conditional CSS and JavaScript for maximum compatibility.
- Use it to enhance modern browsers and gracefully degrade for older ones.
- Pair it with polyfills or fallback strategies to improve user experience.