DEV Community

Cover image for Using Modernizr with Nuxt.js to Detect Browser Features
Sebastian Landwehr
Sebastian Landwehr

Posted on • Edited on • Originally published at sebastianlandwehr.com

3 3

Using Modernizr with Nuxt.js to Detect Browser Features

What is Modernizr?

Modernizr is a package that detects browser features and makes them queriable via JavaScript and CSS. This is very handy to find out which CSS properties are supported, for example. The approach is much more flexible and stable than using browser vendor and version for this. I wrote a module that allows to use Modernizr with Nuxt.js.

Using Modernizr with Nuxt.js

Modernizr uses a plugin-based architecture, and you have to build the package yourself, depending on the features you need to detect – at least if you are not using Nuxt.js! I have created the nuxt-modernizr module that makes it easy to add Modernizr to your Nuxt.js app. You pass the features as module options, and the module creates a Modernizr build and places it in the .nuxt folder. Here is how to configure it:

First, install the module via npm install nuxt-modernizr or yarn add nuxt-modernizr.

Then, in your nuxt.config.js, add the module and pass the options that should be passed to Modernizr:

export default {
  modules: [
    ['nuxt-modernizr', {
      'feature-detects': ['css/scrollbars', 'css/overflow-scrolling'],
      options: ['setClasses'],
    }],
  ],
}
Enter fullscreen mode Exit fullscreen mode

Check out the Modernizr documentation for details.

Now we can use the Modernizr variable and the generated CSS classes on the HTML root element to check for browser features. My all-time favorite among browser features is CSS scrollbars, because the support is pretty different between browsers and depending on if they are available, you can adjust container sizes and style them or not.

if (Modernizr.cssscrollbar) {
  // CSS scrollbar support
}
Enter fullscreen mode Exit fullscreen mode
html.cssscrollbar {
  /* CSS scrollbar support */
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

That was already it! Usage is pretty simple. Let me know if you find it useful or if there are any things that you are missing. Also, if you find it useful, leave a GitHub star on the repository.

If you like what I'm doing, follow me on Twitter or check out my website. Also consider donating at Buy Me a Coffee, PayPal or Patreon. Thank you so much! ❤️

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️