style property ExamplesΒΆ

The general syntax for the style property:

element.style.aStyleProperty

The style property is an object that allows you to read and update the INLINE style properties of the element. The style property does NOT read or update styles defined in a <style> tag or an external CSS file linked with a <link> tag.

Example

 1<!DOCTYPE html>
 2<html>
 3   <head>
 4      <title>DOM Examples</title>
 5   </head>
 6   <body>
 7      <h1>style property Example</h1>
 8      <div id="strawberry" style="color: red;">Strawberry</div>
 9      <div id="blackberry" style="color: purple; font-size: 5px">Blackberry</div>
10
11      <script>
12            let strawberry = document.querySelector("#strawberry");
13            console.log(strawberry.style.color);
14            let blackberry = document.querySelector("#blackberry");
15            console.log(blackberry.style.fontSize);
16            // Update the font size of strawberry
17            strawberry.style.fontSize = "45px";
18            console.log(strawberry.style.fontSize);
19      </script>
20   </body>
21</html>

Console Output

red
5px
45px