Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

CSSStyleSheet

BaselineWidely available *

TheCSSStyleSheet interface represents a singleCSS stylesheet, and lets you inspect and modify the list of rules contained in the stylesheet. It inherits properties and methods from its parent,StyleSheet.

StyleSheet CSSStyleSheet

A stylesheet consists of a collection ofCSSRule objects representing each of the rules in the stylesheet. The rules are contained in aCSSRuleList, which can be obtained from the stylesheet'scssRules property.

For example, one rule might be aCSSStyleRule object containing a style such as:

css
h1,h2 {  font-size: 16pt;}

Another rule might be anat-rule such as@import or@media, and so forth.

See theObtaining a StyleSheet section for the various ways aCSSStyleSheet object can be obtained. ACSSStyleSheet object can also be directly constructed. The constructor, and theCSSStyleSheet.replace(), andCSSStyleSheet.replaceSync() methods are newer additions to the specification, enablingConstructable Stylesheets.

Constructor

CSSStyleSheet()

Creates a newCSSStyleSheet object.

Instance properties

Inherits properties from its parent,StyleSheet.

CSSStyleSheet.cssRulesRead only

Returns a liveCSSRuleList which maintains an up-to-date list of theCSSRule objects that comprise the stylesheet.

Note:In some browsers, if a stylesheet is loaded from a different domain, accessingcssRules results in aSecurityError.

CSSStyleSheet.ownerRuleRead only

If this stylesheet is imported into the document using an@import rule, theownerRule property returns the correspondingCSSImportRule; otherwise, this property's value isnull.

Instance methods

Inherits methods from its parent,StyleSheet.

CSSStyleSheet.deleteRule()

Deletes the rule at the specified index into the stylesheet's rule list.

CSSStyleSheet.insertRule()

Inserts a new rule at the specified position in the stylesheet, given the textual representation of the rule.

CSSStyleSheet.replace()

Asynchronously replaces the content of the stylesheet and returns aPromise that resolves with the updatedCSSStyleSheet.

CSSStyleSheet.replaceSync()

Synchronously replaces the content of the stylesheet.

Legacy properties

These properties are legacy properties as introduced by Microsoft; these are maintained for compatibility with existing sites.

rulesRead onlyDeprecated

Therules property is functionally identical to the standardcssRules property; it returns a liveCSSRuleList which maintains an up-to-date list of all of the rules in the style sheet.

Legacy methods

These methods are legacy methods as introduced by Microsoft; these are maintained for compatibility with existing sites.

addRule()Deprecated

Adds a new rule to the stylesheet given the selector to which the style applies and the style block to apply to the matching elements.

This differs frominsertRule(), which takes the textual representation of the entire rule as a single string.

removeRule()Deprecated

Functionally identical todeleteRule(); removes the rule at the specified index from the stylesheet's rule list.

Obtaining a StyleSheet

A stylesheet is associated with at most oneDocument, which it applies to (unlessdisabled). A list ofCSSStyleSheet objects for a given document can be obtained using theDocument.styleSheets property. A specific style sheet can also be accessed from itsowner object (Node orCSSImportRule), if any.

ACSSStyleSheet object is created and inserted into the document'sDocument.styleSheets list automatically by the browser, when a stylesheet is loaded for a document.

A (possibly incomplete) list of ways a stylesheet can be associated with a document follows:

Reason for the style sheet to be associated with the document Appears indocument.
styleSheets
list
Getting the owner element/rule given the style sheet objectThe interface for the owner objectGetting the CSSStyleSheet object from the owner
<style> and<link> elements in the documentYes.ownerNodeHTMLLinkElement,
HTMLStyleElement,
orSVGStyleElement
HTMLLinkElement.sheet,
HTMLStyleElement.sheet,
orSVGStyleElement.sheet
CSS@import rule in other style sheets applied to the documentYes.ownerRuleCSSImportRule.styleSheet
<?xml-stylesheet ?> processing instruction in the (non-HTML) documentYes.ownerNodeProcessingInstruction.sheet
JavaScriptimport ... with { type: "css" }NoN/AN/AN/A
HTTP Link HeaderYesN/AN/AN/A
User agent (default) style sheetsNoN/AN/AN/A

Specifications

Specification
CSS Object Model (CSSOM)
# the-cssstylesheet-interface

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp