All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Add Style To Element

Last Updated : Mar 11, 2024

JavaScript Add Style To Element

In this article we will show you the solution of JavaScript add style to element, basically, style an element using a cascading style sheet or CSS in an HTML file.

Now we can add style elements also using JavaScript. We are going to use different methods to add style to an element.

Step By Step Guide On JavaScript Add Style To Element :-

At first, let’s look at the HTML code first:

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF-8 ">
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge ">
    <meta name = "viewport " content = " width=device-width , initial-scale = 1.0 ">
    <title> JavaScript add style to an element </title>
</head>
<body>
    <h2 style = "color :indigo ;" >
        JavaScript add style to an element
    </h2>
    <div id = " div1 " >
        this is a DIV
    </div>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the < html > tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both < head > and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively. Create < script > tag to add JS.
  5. Thirdly, the < body > tag is used to define the webpage body. All the contents to show on the website are written here.
  6. < h2 > tag used to add heading here. We used inline CSS here to set the color to the heading.
  7. Then add < div > tag also with the id “div1”.

Method 1

Here we will use document.querySelector.

const h2 = document.querySelector( ' h2 ' )
h2.style.color = ' green ' ;
h2.style.backgroundColor = ' almond ' ;
  1. Here using document.querySelector to select h2
  2. Add style by h2.style.StyleProperty

(note: if the JS code written in the console window , by typing it you will see various type of style property there)

Method 2

Here we will also use document.querySelector and add colour to an Id.

var element;
document.querySelector("#div1").style.backgroundColor ="yellow";
document.querySelector("#div1").style.color ="blue";
console.log(element);
  1. Here using document.querySelector to select the id div.
  2. Add style property to it (ex: background-colour, colour) by.

Method 3

Here we will use .setAttribute property.

let el = document.querySelector("body")
e1.setAttribute("class","yellow")
  1. Here using document.querySelector to select the <body>
  2. Here .setAttribute property is use to set a value to the specified element
  3. Then add the style property.

Method 4

This is another method to add style using JavaScript.

var prop=document.createElement("div");
prop.innerHTML="<style>h1{background:red;}</style>";
document.body.appendChild(prop);
  1. Create a variable named prop.
  2. Use prop.innerHTML to add desired property.
  3. Using .appendChild to add the style to only one node.

Conclusion :-

At last here in conclusion, here we can say that with the help of this article we are able to add style using JavaScript.

I hope this article on JavaScript add style to element helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪