Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Modernizr library, why does it add classes to the html tag

Tags:

html

modernizr

Can anyone explain (because the github page doesn't) why Modernizr feels the need to add all of those classnames to the HTML tag ? After loading Modernizr, it looks like this:

<!doctype html>
<html class=" js flexbox canvas canvastext webgl ~~~~ etc etc

I see no explanation at all why it wants to do that. Modernizr provides properties to let me know whether for example canvas is supported (Modernizr.canvas == true?). Are those html classnames added for a test that's easier than that?

like image 820
Dee2000 Avatar asked Dec 17 '12 13:12

Dee2000


People also ask

What is modernizr in HTML?

HTML5 Modernizr: It is a JavaScript library that detects which next-generation web technologies feature our web browser supports. There are many new features that are being introduced in HTML and CSS but many browsers do not support these new features.

What is the use of modernizer?

Modernizr is a JavaScript library that detects which HTML5 and CSS3 features your visitor's browser supports. In detecting feature support, it allows developers to test for some of the new technologies and then provide fallbacks for browsers that do not support them.


2 Answers

These class names are meant to be used in your CSS code, so you can add fallback styles in case a feature is unsupported. Example from modernizr docs page:

/* Simulated box shadow using borders: */
.box {
   border-bottom: 1px solid #666;
   border-right:  1px solid #777;
}
.boxshadow div.box {
   border: none;
   -webkit-box-shadow: #666 1px 1px 1px;
      -moz-box-shadow: #666 1px 1px 1px;
           box-shadow: #666 1px 1px 1px;
}
like image 66
pawel Avatar answered Sep 21 '22 16:09

pawel


I may be wrong (and please correct me if I am), the classes here indicate the features that Modernizr has detected that are (or indeed are NOT) supported in the given browser.

Look at the docs here: http://modernizr.com/docs/

Take this example:

<html class="no-js">

if JavaScript is disabled, then modernizr cannot run, and therefore the class will remain "no-js" but if JavaScript is enabled, Modernizr will replace "no-js" with "js", and similarly will tell you what other features are supported

like image 26
Matthew Layton Avatar answered Sep 22 '22 16:09

Matthew Layton