CSSPageRule

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("CSSOM")}}

**CSSPageRule** represents a single CSS {{cssxref("@page")}} rule.

{{InheritanceDiagram}}

Instance properties

_Inherits properties from its ancestors {{domxref("CSSGroupingRule")}} and {{domxref("CSSRule")}}._

  • {{domxref("CSSPageRule.selectorText")}}
  • : Represents the text of the page selector associated with the at-rule.
  • {{domxref("CSSPageRule.style")}} {{ReadOnlyInline}}
  • : Returns the declaration block associated with the at-rule.

Instance methods

_Inherits methods from its ancestors {{domxref("CSSGroupingRule")}} and {{domxref("CSSRule")}}._

Examples

Filtering for page rules

This example shows how you can find CSSPageRule objects for {{cssxref("@page")}} rules loaded by the document.

html hidden
js hidden
css hidden

CSS

Below we define styles for the page using a {{cssxref("@page")}} rule.

css
@page {
  margin: 1cm;
}

JavaScript

The code iterates through all the sheets in the document, and through all the cssRules in each sheet, logging the sheet index, the number of rules, and the type of each rule object. We then detect CSSPageRule objects using their type (doing nothing with the information).

js
for (
  let sheetCount = 0;
  sheetCount < document.styleSheets.length;
  sheetCount++
) {
  const sheet = document.styleSheets[sheetCount].cssRules;
  log(`styleSheet: ${sheetCount}`);

  const myRules = document.styleSheets[sheetCount].cssRules;
  log(`rules: ${myRules.length}`);
  for (const rule of myRules) {
    log(`rule: ${rule}`);
    if (rule instanceof CSSPageRule) {
      // Do something with CSSPageRule
    }
  }
}

Results

The results are shown below. As you can see there are two sheets, corresponding to this main document and the example code frame, and each has a number of rules, only one of which is our CSSPageRule.

{{EmbedLiveSample("Filtering for page rules", "100%", "300px")}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}