What is feature detection and how to use Modernizr

Feature detection is a technique used in web development to determine whether a user's browser supports a specific feature. This approach allows developers to implement fallbacks or alternative implementations for browsers that lack support for certain functionalities. Modernizr is a popular JavaScript library that simplifies feature detection and enables developers to write more effective, cross-browser compatible code.

To use Modernizr, you need to include the Modernizr library in your HTML document, then you can leverage its methods to check for various features and apply conditionally styles or scripts accordingly.

<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/3.11.7/modernizr.min.js"></script> <script> if (Modernizr.geolocation) { // Geolocation is supported navigator.geolocation.getCurrentPosition(function(position) { console.log(position); }); } else { // Geolocation is not supported console.log('Geolocation is not supported by this browser.'); } </script>

feature detection Modernizr web development JavaScript cross-browser compatibility