How do you add a class to the <html> root element using Javascript?
-
this was one of the questions I asked when I started out. I never did find a good place that mentioned it. I am glad to see it here.Pow-Ian– Pow-Ian2012-12-20 21:46:09 +00:00Commented Dec 20, 2012 at 21:46
-
Just curious - why would you want to do this?David Hoerster– David Hoerster2012-12-20 21:49:01 +00:00Commented Dec 20, 2012 at 21:49
-
@David To add a fallback in case Modernizr doesn't load. Modernizr adds the class "js" when it loads.Brandon Lebedev– Brandon Lebedev2012-12-20 21:51:24 +00:00Commented Dec 20, 2012 at 21:51
-
1You should really add the class "no-js" to your markup if you are using modernizr. (If modernizr loads it will remove this class)Kevin Boucher– Kevin Boucher2012-12-20 21:54:34 +00:00Commented Dec 20, 2012 at 21:54
-
@Kevin - Already did. Go HTML5 Boilerplate!Brandon Lebedev– Brandon Lebedev2012-12-20 22:02:31 +00:00Commented Dec 20, 2012 at 22:02
Add a comment
|
8 Answers
Like this:
var root = document.getElementsByTagName( 'html' )[0]; // '0' to assign the first (and only `HTML` tag)
root.setAttribute( 'class', 'myCssClass' );
Or use this as your 'setter' line to preserve any previously applied classes: (thanks @ama2)
root.className += ' myCssClass';
Or, depending on the required browser support, you can use the classList.add() method:
root.classList.add('myCssClass');
https://developer.mozilla.org/en-US/docs/Web/API/Element/classList http://caniuse.com/#feat=classlist
UPDATE:
A more elegant solution for referencing the HTML element might be this:
var root = document.documentElement;
root.className += ' myCssClass';
// ... or:
// root.classList.add('myCssClass');
//
5 Comments
Kelderic
FYI, classlist.add/.remove doesn't work on document.documentElement.
Kevin Boucher
@AndyMercer It's
classList.add (camel case).Kelderic
Yes obviously that was a typo. But what I said is still correct.
classList doesn't work on document.documetElement.Kevin Boucher
I tried it in three different browsers before replying, so it wasn't "obviously a typo." (Chrome, Firefox, Safari--
root.classList.add('myCssClass') worked in all three.) Are you using IE?adrian
document.documentElement.classList.add seems to work fine here. Have browsers in 2018 started supporting this?document.documentElement.classList.add('myCssClass');
classList is supported since ie10: https://caniuse.com/#search=classlist
Comments
This should also work:
document.documentElement.className = 'myClass';
Edit:
IE 10 reckons it's readonly; yet:

Opera works:

I can also confirm it works in:
- Chrome 26
- Firefox 19.02
- Safari 5.1.7
6 Comments
David Hoerster
You don't need jQuery to select an element with JavaScript.
aebersold
jQuery is infact easy to learn, and is used most of the time anyway, these days. But yes, you don't need it for this task ;)
povilasp
yeah, right, we know what jquery is, but answering a purely javascript question with "learn jquery" is what surprised me :)
David Hoerster
@aebersold I agree that jQuery is easy to learn and use, but at 50K to download (plus an additional request) just to add a class, it's a bit of overkill IMHO.
aebersold
@Mick thanks for pointing this out a decade after I wrote this answer.
|
<script>
(function () {
try {
const mode = localStorage.getItem('darkMode');
if (!JSON.parse(mode)) return;
document.documentElement.classList.add('dark');
} catch (e) {}
})();
</script>
1 Comment
trincot
This answer just repeats what was already answered a decade(!) ago. All the rest is irrelevant to the question.