Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe AND quickest way to add a css class to the body of the DOM

People also ask

How can I add class to my body?

Use the classList. add() method to add a class to the body element, e.g. document. body.

Can we add class from CSS?

Yes you can - first capture the event using onmouseover , then set the class name using Element. className . If you like to add or remove classes - use the more convenient Element.


document.body.className += ' home';

Performance comparision: className vs classList vs addClass :

Update(based on PSL's comment)

or for newer ones document.body.classList.add("home");

Make sure you do this under the <body>, it won't work if applied from a <head> script


Right after the opening body tag, you can create a script tag :

<body>
<script>
    $('body').addClass('home')
</script>
<!-- HTML content bellow -->
</body>

The only condition is that the jQuery is loaded in the head.