Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Using dynamic styling information

The CSS Object Model (CSSOM), part of the DOM, exposes specific interfaces allowing manipulation of a wide amount of information regarding CSS. Initially defined in theDOM Level 2 Style recommendation, these interfaces forms now a specification,CSS Object Model (CSSOM) which aims at superseding it.

In many cases, and where possible, it is best practice to dynamically manipulate classes via theclassName property since the ultimate appearance of all of the styling hooks can be controlled in a single stylesheet. One's JavaScript code also becomes cleaner since instead of being dedicated to styling details, it can focus on the overall semantics of each section it is creating or manipulating, leaving the precise style details to the stylesheet. However, there are cases where actually obtaining or manipulating the rules can be useful (whether for whole stylesheets or individual elements), and that is described in further detail below. Note also that, as with individual element's DOM styles, when speaking of manipulating the stylesheets, this is not actually manipulating the physical document(s), but merely the internal representation of the document.

The basicstyle object exposes theStylesheet and theCSSStylesheet interfaces. Those interfaces contain members likeinsertRule,selectorText, andparentStyleSheet for accessing and manipulating the individual style rules that make up a CSS stylesheet.

To get to thestyle objects from thedocument, you can use theDocument.styleSheets property and access the individual objects by index (e.g.,document.styleSheets[0] is the first stylesheet defined for the document, etc.).

Modify a stylesheet rule with CSSOM

In this example the background of the page is set tored using CSS. The JavaScript then accesses the property using the CSSOM and changes the background tocornflowerblue.

html
<p>The stylesheet declaration for the body is modified via JavaScript.</p>
css
body {  background-color: red;  font: 1.2em / 1.5 sans-serif;  color: white;  padding: 1em;}
js
const stylesheet = document.styleSheets[1];stylesheet.cssRules[0].style.backgroundColor = "cornflowerblue";

The list of properties available in the DOM from thestyle property is given on theDOM CSS Properties List page.

To modify styles to a document using CSS syntax, one can insert rules or insert<style> tags whoseinnerHTML property is set to the desired CSS.

Modify an element's style

The elementstyle property (see also the section "DOM Style Object" below) can also be used to get and set the styles on an element. However, this property only returns style attributes that have been setin-line (e.g., accessingelement.style.color on an element defined as<td> returns the string"", even though the element may have acolor declared via a stylesheet).

Also, when you set this property on an element, you override any styles that have been set elsewhere for that element's particular property you are setting. Setting theborder property, for example, will override settings made elsewhere for that element'sborder property in the head section, or external style sheets. However, this will not affect any other property declarations for that element's styles, such as padding or margin or font, for example.

To change a particular element's style, you can adapt the following example for the element(s) you want to style.

html
<p>Click here to change background color.</p><button>Reset background color</button>
css
#p1 {  border: solid blue 2px;}
js
const p1 = document.getElementById("p1");const button = document.querySelector("button");p1.addEventListener("click", () => {  p1.style.background = "green";});button.addEventListener("click", () => {  p1.style.background = "white";});

ThegetComputedStyle() method on thedocument.defaultView object returns all styles that have actually been computed for an element.

Using the setAttribute method

Note that you can also change style of an element by getting a reference to it and then use itssetAttribute method to specify the CSS property and its value.

js
const el = document.getElementById("some-element");el.setAttribute("style", "background-color:darkblue;");

Be aware, however, thatsetAttribute removes all otherstyle properties that may already have been defined in the element'sstyle object. If thesome-element element above had an in–linestyle attribute of saystyle="font-size: 18px", that value would be removed by the use ofsetAttribute.

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp