Home >Web Front-end >JS Tutorial >How to Dynamically Set CSS Properties for a Created `` Element in JavaScript?

How to Dynamically Set CSS Properties for a Created `` Element in JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2024-10-29 13:37:02947browse

How to Dynamically Set CSS Properties for a Created `` Element in JavaScript?

How to Set CSS Properties in JavaScript

When working with the DOM in JavaScript, it's often necessary to manipulate the CSS properties of elements. This includes setting attributes such as width, height, color, and more.

Question:

Suppose you have created a element to 100 pixels using JavaScript. You can apply any supported CSS property in this way, such as height, background-color, font-size, and so on.

Note that the style property is a CSSStyleDeclaration object that allows you to manipulate individual styles or get their current values. You can also use it to set multiple styles at once by passing an object:

<code class="javascript">menu.style = {
  width: "100px",
  height: "200px",
  backgroundColor: "blue",
};</code>

The above is the detailed content of How to Dynamically Set CSS Properties for a Created `` Element in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn