JavaScript element.style Property

You are Here:

JavaScript element.style Property

The element.style property gets or sets the inline style of an element.

Set CSS Inline Style

When setting, you should specify the individual CSS properties of style attribute which overrides the existing inline styles.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>I'm red</p> <script> var elem = document.getElementsByTagName("p")[0]; elem.style.color = "red"; </script> </body> </html>

Syntax

element.style

Return Values

ValueExplanation
ObjectReturns an object representing the inline style of an element.

Get CSS Inline Style

When getting, it returns a CSSStyleDeclaration object that contains a list of all styles properties for that element with values assigned for the attributes that are defined in the element's inline style attribute.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p style="color:red">I'm red</p> <p>Click on the button to get the color of the above paragraph.</p> <button onclick="myFunction()">Click Me</button> <p id="point"></p> <script> var x = document.getElementById("point"); var elem = document.getElementsByTagName("p")[0]; function myFunction(){ x.innerHTML = "Color is " +elem.style.color; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author