Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to get CSS style by JavaScript

Shulou Source: shulou.com Published: 2022-05-31 10:40:16 09月23日 Update

This article mainly introduces JavaScript how to obtain the relevant knowledge of CSS style, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this JavaScript how to obtain CSS style article, let's take a look at it.

Syntax:

NodeObject.style.cssProperty

Where nodeObject is the node object and cssProperty is the CSS attribute.

For example:

?

one

two

Document.getElementById ("demo"). Style.height

Document.getElementById ("demo"). Style.border

Note: for CSS attributes separated by "-", remove "-" and capitalize the first letter after "-". For example:

Background-color is going to write backgroundColor

Line-height is going to write lineHeight

For example:

Document.getElementById ("demo") style. BackgroundColor

Document.getElementById ("demo"). Style.lineHeight

For example, get the style of the node of id= "demo":

Click here to get the CSS style

How to set and modify CSS style by JavaScript

Syntax:

NodeObject.style.cssProperty=newStyle

Where nodeObject is the node object, cssProperty is the CSS attribute, and newStyle is the value of the CSS attribute.

Note: for CSS attributes separated by "-", remove "-" and capitalize the first letter after "-".

For example:

Document.getElementById ("demo") .style.height = "50px"

Document.getElementById ("demo") .style.border = "1px solid # ddd"

Document.getElementById ("demo") .style.backgroundColor = "# ccc"

Document.getElementById ("demo") .style.textAlign = "center"

For example, style the node of id= "demo":

This is the end of the article on "how JavaScript gets the CSS style". Thank you for reading! I believe you all have a certain understanding of "how JavaScript obtains CSS style" knowledge. If you want to learn more, you are welcome to follow the industry information channel.

Tags: Style attribute node knowledge content uppercase letter object article grammar and will value easy to operate article easy to understand more look industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple macOS Shulou Tech Info NVidia