Add a CSS class to an HTML element with JavaScript/jQuery
This post will discuss how to add a CSS class to an HTML element using JavaScript and jQuery.
1. Using JavaScript className property
The className property is commonly used to set the value of the class attribute of an element in plain JavaScript. The following code demonstrates this by setting the class attribute for the div element and replaces its existing classes, if any:
JS
|
1 |
document.getElementById("container").className = "main"; |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container"></div> |
To add a class to an element rather than replacing its existing classes, use the += operator instead. Note, it is important to prefix the new classname with space; otherwise, one of the existing classes of an element is lost.
JS
|
1 2 |
var div = document.getElementById("container"); div.className += " border"; |
CSS
|
1 2 3 4 5 6 7 8 |
.dimensions { width: 500px; height: 300px; } .border { border: 1px solid blue; } |
HTML
|
1 |
<div id="container" class="dimensions"></div> |
To apply multiple classes, specify the list of space-separated names of the classes for the className property.
JS
|
1 |
document.getElementById("container").className = "dimension border"; |
CSS
|
1 2 3 4 5 6 7 8 |
.dimension { width: 500px; height: 300px; } .border { border: 1px solid blue; } |
HTML
|
1 |
<div id="container"></div> |
2. Using JavaScript classList property
The classList property returns a collection of the class attributes of the element, which can be later modified using the add() and remove() methods. The add() method adds a class to the list.
JS
|
1 |
document.getElementById("container").classList.add("main"); |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container"></div> |
To remove a class, you can use the element.classList.remove() method.
JS
|
1 |
document.getElementById("container").classList.remove("main"); |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container" class="main"></div> |
3. Using jQuery .addClass() method
If you’re on jQuery, you can use the .addClass() method to add the specified class to an element. It works by manipulating the class attribute of the element.
jQuery
|
1 2 3 |
$(document).ready(function() { $("#container").addClass("main"); }); |
CSS
|
1 2 3 4 5 |
.main { width: 500px; height: 300px; border: 1px solid blue; } |
HTML
|
1 |
<div id="container"></div> |
4. Using jQuery .css() method
Alternatively, you can use jQuery’s .css() for setting one or more CSS properties to an element. It works by modifying the value of the style property of the element.
jQuery
|
1 2 3 4 5 6 7 8 9 |
$(document).ready(function() { var styles = { backgroundColor: "#ddd", width: "500px", height: "300px", border: "1px solid black" }; $("#container").css(styles); }); |
HTML
|
1 |
<div id="container"></div> |
Note that unless values of the CSS properties are dynamically generated, it’s recommended to use classes instead.
That’s all about adding a CSS class to an HTML element using JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)