This article will describe the methods to add CSS with JavaScript.
How to Add CSS With JavaScript?
In JavaScript, you can add CSS styles to an element by modifying its style property using the following methods or approaches:
- style Property as inline styling
- setAttribute() Method as inline styling
- createElement() Method as a global styling
Method 1: Add CSS With JavaScript Using the “style” Property
For adding CSS in JavaScript, use the “style” property. It is utilized to access and manipulate the inline styles of an element. It gives an object that represents the inline styles of an element and allows one to get or set individual style properties.
Syntax
For adding CSS style in JavaScript, the following syntax is utilized for the “style” property:
Here, “element” is a reference to an HTML element.
Example
In the following example, we will style the buttons using JavaScript. Firstly, we will create three buttons and assign ids to them, which helps to access the button elements for styling:
<button id="btn2">Reject</button>
<button id="btn3">Accept</button>
Before styling the output will look like this:
Now, let’s style these buttons in JavaScript using the “style” property. First, get all the button elements using their assigned ids with the help of the “getElementById()” method:
let reject = document.getElementById("btn2");
let accept = document.getElementById("btn3");
Set the background colors of all these buttons using the “style” property:
reject.style.backgroundColor = "red";
accept.style.backgroundColor = "yellow";
As you can see, the buttons have been successfully styled using the “style” property:
Method 2: Add CSS With JavaScript Using “setAttribute()” Method
To add CSS styling in JavaScript, use the “setAttribute()” method. It is used to set or add an attribute and its value to an HTML element.
Syntax
The following syntax is utilized for the “setAttribute()” method:
Example
Here, we will set the different styles on the buttons in JavaScript using the “setAttribute()” method. Set the border radius of all buttons to “.5rem”, and the text color of the “Agree” and “Reject” buttons to “white”.
reject.setAttribute("style", "background-color: red; color: white; border-radius: .5rem;");
accept.setAttribute("style", "background-color: yellow; border-radius: .5rem;");
Output
Method 3: Add CSS With JavaScript Using the “createElement()” Method
If you want to create classes or ids in JavaScript styling as in the CSS styling, use the “createElement()” method. It is utilized to dynamically create a new element. For styling create a “style” element using this method. The “createElement(‘style’)” method in JavaScript is used to dynamically create a new style element, which can be used to add CSS styles to a web page.
Syntax
The given syntax is used for the “createElement()” method:
For adding CSS style in JavaScript, use the given syntax:
Then append the style element in the head tag using the below syntax:
Here, “style” is a reference to the newly created style element, and “document.head” is the head element of the HTML document.
Example
First, create a style element using the “createElement()” method:
Now, create a “btn” class for applying the same styling on all buttons and ids “btn1”, “btn2” and “btn3” for individual button styling:
.btn {
font-size: 1.1rem;
padding: 3px;
margin: 2px;
font-family: sans-serif;
border-radius: .5rem;
}
#btn1{
background-color: green;
color: white;
}
#btn2{
background-color: red;
color: white;
}
#btn3{
background-color: yellow;
}
`;
Now, append the style element to the head of the document by passing as a parameter to the “appendChild()” method:
Output
That’s all about adding CSS in JavaScript.
Conclusion
For adding CSS with JavaScript, utilize the inline styling including the “style” Property, and “setAttribute()” method, or the global styling using the “createElement()” method. Global styling is more efficient while an inline method is not recommended as it makes it difficult to maintain the styles of application and can lead to code repetition. This article described the methods to add CSS with JavaScript.