What is Modernizr for HTML5 feature detection?
Modernizr is a JavaScript library used to detect the support of HTML5 and CSS3 features by running functional tests when the page is loaded and adding corresponding class names (such as canvas or no-canvas) to the element based on the results, allowing developers to write conditional CSS or JavaScript code based on actual functionality, thus achieving progressive enhancement, avoiding browser sniffing, and keeping files streamlined through customizable builds. Therefore, it provides a reliable way to adapt the website to the real ability of the user's browser.

Modernizr is a JavaScript library that helps developers detect whether a user's browser supports specific HTML5 and CSS3 features. Instead of relying on browser version checks, Modernizr tests for actual feature support, allowing you to write more robust and future-friendly code.

When a web page loads, Modernizr runs a series of tests on the browser's capabilities. It then adds classes to the element indicating which features are supported (eg, flexbox , canvas , websockets ) and which are not (eg, no-audio , no-localstorage ). This lets you apply different CSS styles or JavaScript logic based on what the browser can do.
For example:

- If the browser supports the
<canvas></canvas>element, Modernizr adds the classcanvasto thetag. - If it doesn't, it adds
no-canvas.
This enables conditional CSS like:
.canvas #graphic {
background: url('animated-graphic.js');
}
.no-canvas #graphic {
background: url('static-image.png');
}You can also use Modernizr in JavaScript:

if (Modernizr.geolocation) {
navigator.geolocation.getCurrentPosition(successFunction);
} else {
alert('Geolocation is not supported in this browser.');
}Key benefits:
- Progressive enhancement : Build a basic experience for all browsers, then enhance it for those with more capabilities.
- Avoids browser sniffing : Focuses on what the browser can do , not what it claims to be .
- Customizable builds : You can include only the tests you need, keeping file size small.
Modernizr was especially valuable in the early days of HTML5 when browser support varied widely. While many modern browsers now support core HTML5 features, it's still useful when working with cutting-edge or inconsistently supported APIs.
Basically, it gives you a reliable way to adapt your site to the real capabilities of the user's browser.
The above is the detailed content of What is Modernizr for HTML5 feature detection?. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20521
7
13634
4
How to center an image vertically in HTML5? (Layout techniques)
Mar 07, 2026 am 02:05 AM
Flexbox is the most stable for centered images. The key is to set display:flex and align-items:center in the parent container and specify the height; using place-items:center for Grid is more concise; absolute positioning requires top:50% with transform:translateY(-50%); vertical-align is invalid for block-level centering.
How to use SVG graphics directly in HTML5? (Inline SVG)
Mar 07, 2026 am 01:40 AM
SVG tags can be written directly into HTML without any external reference. The core of InlineSVG is to use it as an ordinary HTML element. The browser supports it natively. It does not require additional loading, does not trigger HTTP requests, and can be directly controlled by CSS and JS. A common mistake is to insert it as an image - this way you lose the advantage of inlining, the style cannot penetrate, and JS cannot get inside. Directly copy the SVG source code (exported from Figma, or handwritten), and paste it into an HTML file or any container. Make sure the beginning is, the end is, and there is no DOCTYPE in the middle. Delete useless attributes such as xmlns="http://www.





