555

I notice that in a lot of template engines, in the HTML5 Boilerplate, in various frameworks and in plain php sites there is the no-js class added onto the <HTML> tag.

Why is this done? Is there some sort of default browser behavior that reacts to this class? Why include it always? Does that not render the class itself obsolete, if there is no no-"no-js" case and html can be addressed directly?

Here is an example from the HTML5 Boilerplate index.html:

<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->

As you can see, the <html> element will always have this class. Can someone explain why this is done so often?

7 Answers 7

527

When Modernizr runs, it removes the "no-js" class and replaces it with "js". This is a way to apply different CSS rules depending on whether or not Javascript support is enabled.

See Modernizer's source code.

17
  • 6
    There's another great write-up on it here: alistapart.com/articles/…
    – Hannele
    Commented Feb 28, 2012 at 15:11
  • 52
    Since Modernizr replaces "no-js" with "js", you can use this as a way to do the equivalent of an if/else statement in your CSS code. For example, .myclass { /* CSS code for all versions of your page goes here */ }, .js .myclass { /* This CSS code will only show up if JS is enabled */ } and .no-js .myclass { /* This CSS code will only show up if JS is disabled. */ }. Hope this helps. -Nick
    – skcin7
    Commented Aug 22, 2012 at 9:17
  • 35
    @Ringo: HTML 5 specifies that any element may have a class attribute. Although HTML 4 technically doesn't, no browser chokes on it; even the ones that don't support it just ignore it, and i haven't seen one of those browsers in years.
    – cHao
    Commented Nov 4, 2012 at 23:16
  • 4
    @ZachL I realize I must be smoking crack here, but it seems to me that .js { padding: ...} is just fine since you know as the one who implemented it that body = .js. More directly to your point, it seems you are claiming that body.js is worse than .js body which I am not following...
    – wired_in
    Commented Mar 14, 2014 at 7:46
  • 4
    It's worth mentioning html5-boilerplate puts it on <html> and no issue has ever been reported against that. Somehow putting it on <html> has become an established practice and people immediately know what it is and what it does. In the end, deviating from that practice just raises more questions. Commented Jun 23, 2014 at 8:36
120

The no-js class is used by the Modernizr feature detection library. When Modernizr loads, it replaces no-js with js. If JavaScript is disabled, the class remains. This allows you to write CSS which easily targets either condition.

From Modernizrs' Anotated Source (no longer maintained):

Remove "no-js" class from element, if it exists: docElement.className=docElement.className.replace(/\bno-js\b/,'') + ' js';

Here is a blog post by Paul Irish describing this approach: http://www.paulirish.com/2009/avoiding-the-fouc-v3/


I like to do this same thing, but without Modernizr. I put the following <script> in the <head> to change the class to js if JavaScript is enabled. I prefer to use .replace("no-js","js") over the regex version because its a bit less cryptic and suits my needs.

<script>
    document.documentElement.className = 
       document.documentElement.className.replace("no-js","js");
</script>

Prior to this technique, I would generally just apply js-dependant styles directly with JavaScript. For example:

$('#someSelector').hide();
$('.otherStuff').css({'color' : 'blue'});

With the no-js trick, this can Now be done with css:

.js #someSelector {display: none;}
.otherStuff { color: blue; }
.no-js .otherStuff { color: green }

This is preferable because:

  • It loads faster with no FOUC (flash of unstyled content)
  • Separation of concerns, etc...
3
  • 1
    The Modernizr version using a RegExp is safer (and likely faster) though.
    – AndrewF
    Commented Aug 17, 2013 at 20:22
  • @AndrewF - could you maybe expand on that a bit? How exactly is it safer? Commented Sep 5, 2013 at 13:34
  • 12
    @ZachL The regexp version (with \b) doesn't match something like anno-jsus, the other changes it to anjsus. no-js is not a typical substring of any class name, but still in this respect it's "safer" with \bs. Commented Sep 22, 2013 at 13:50
42

Modernizr.js will remove the no-js class.

This allows you to make CSS rules for .no-js something to apply them only if Javascript is disabled.

17

The no-js class gets removed by a javascript script, so you can modify/display/hide things using css if js is disabled.

3
  • You said "The no-js class gets removed by a javascript script". So how Javascript works (Able to remove 'no-js' class) if it 's disabled. Could you give me a clear?
    – haind
    Commented Dec 26, 2013 at 18:17
  • 2
    You're right and this is actually the point: If JavaScript is disabled, the class will stay and you can for example show some html part using CSS to warn the user about it. .no-js #js-warning {display: block;}
    – marc
    Commented Jan 14, 2014 at 12:39
  • Be aware, JS errors may occur after Modernizr has done this so its not a foolproof way of testing JS is functioning Commented Feb 11, 2014 at 4:21
15

This is not only applicable in Modernizer. I see some site implement like below to check whether it has javascript support or not.

<body class="no-js">
    <script>document.body.classList.remove('no-js');</script>
    ...
</body>

If javascript support is there, then it will remove no-js class. Otherwise no-js will remain in the body tag. Then they control the styles in the css when no javascript support.

.no-js .some-class-name {

}
5

Look at the source code in Modernizer, this section:

// Change `no-js` to `js` (independently of the `enableClasses` option)
// Handle classPrefix on this too
if (Modernizr._config.enableJSClass) {
  var reJS = new RegExp('(^|\\s)' + classPrefix + 'no-js(\\s|$)');
  className = className.replace(reJS, '$1' + classPrefix + 'js$2');
}

So basically it search for classPrefix + no-js class and replace it with classPrefix + js.

And the use of that, is styling differently if JavaScript not running in the browser.

1

The no-js class is used to style a webpage, dependent on whether the user has JS disabled or enabled in the browser.

As per the Modernizr docs:

no-js

By default, Modernizr will rewrite <html class="no-js"> to <html class="js">. This lets hide certain elements that should only be exposed in environments that execute JavaScript. If you want to disable this change, you can set enableJSClass to false in your config.

Not the answer you're looking for? Browse other questions tagged or ask your own question.