Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. CSSContainerRule

CSSContainerRule

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨February 2023⁩.

* Some parts of this feature may have varying levels of support.

TheCSSContainerRule interface represents a single CSS@container rule.

An object of this type can be used to get the query conditions for the@container, along with the container name if one is defined.Note that the container name and query together define the "condition text", which can be obtained usingCSSConditionRule.conditionText.

CSSRule CSSGroupingRule CSSConditionRule CSSContainerRule

Instance properties

Inherits properties from its ancestorsCSSConditionRule,CSSGroupingRule, andCSSRule.

CSSContainerRule.containerNameRead only

Returns a string representing the name of an@container, or an empty string.

CSSContainerRule.containerQueryRead only

Returns a string representing the set of features or "container conditions" that are evaluated to determine if the styles in the associated@container are applied.

Instance methods

No specific methods; inherits methods from its ancestorsCSSConditionRule,CSSGroupingRule, andCSSRule.

Examples

Unnamed container rule

The example below defines an unnamed@container rule, and displays the properties of the associatedCSSContainerRule.The CSS is the same as in the@container exampleSetting styles based on a container's size.

The first part of the code simply creates a list for logging the container rule properties, along with a JavaScriptlog() method to simplify adding the properties.

html
<div>  <h2>Log</h2>  <ul></ul>  <hr /></div>
js
// Store reference to log listconst logList = document.querySelector("#log ul");// Function to log data from underlying sourcefunction log(result) {  const listItem = document.createElement("li");  listItem.textContent = result;  logList.appendChild(listItem);}

Then we define the HTML for acard (<div>) contained within apost.

html
<div>  <div>    <h2>Card title</h2>    <p>Card content</p>  </div></div>

The CSS for the example is shown below.As described in the corresponding@container example, the CSS for the container element specifies the type of the container.The@container then applies a new width, font-size and background color to the card if the width is less than 650px.

html
<style>  /* A container context based on inline size */  .post {    container-type: inline-size;  }  /* Apply styles if the container is narrower than 650px */  @container (width < 650px) {    .card {      width: 50%;      background-color: gray;      font-size: 1em;    }  }</style>

The code below gets theHTMLStyleElement associated with the example using its id, and then uses itssheet property to get theStyleSheet.From theStyleSheet we get the set ofcssRules added to the sheet.Since we added the@container as the second rule above, we can access the associatedCSSContainerRule using the second entry, with index "1", in thecssRules.Last of all, we log thecontainerName,containerQuery andconditionText (inherited) properties.

js
const exampleStylesheet = document.getElementById("example-styles").sheet;const exampleRules = exampleStylesheet.cssRules;const containerRule = exampleRules[1]; // a CSSContainerRule representing the container rule.log(`CSSContainerRule.containerName: "${containerRule.containerName}"`);log(`CSSContainerRule.containerQuery: "${containerRule.containerQuery}"`);log(`CSSContainerRule.conditionText: "${containerRule.conditionText}"`);

Note:The styles for this example are defined in an inline HTMLstyle element with an id in order to make it easy for the code to find the correct sheet.You might also locate the correct sheets for each example from the document by indexing against the length (e.g.,document.styleSheets[document.styleSheets.length-1] but that makes working out correct sheet for each example more complicated).

The example output is shown below.The log section lists thecontainerName, which is an empty string as no name has been defined.ThecontainerQuery andconditionText strings are also logged, and have the same value because there is no name defined.The card should change background and as the width of the page transitions through 650px.

Named container rule

The example below defines a named@container rule, and displays the properties of the associatedCSSContainerRule.The CSS is very similar to that in the@container exampleCreating named container contexts.

<div>  <h2>Log</h2>  <ul></ul>  <hr /></div>
// Store reference to log listconst logList = document.querySelector("#log ul");// Function to log data from underlying sourcefunction log(result) {  const listItem = document.createElement("li");  listItem.textContent = result;  logList.appendChild(listItem);}

First we define the HTML for acard (<div>) contained within apost (the example does not show the logging code, as this is the same as in the previous example).

html
<div>  <div>    <h2>Card title</h2>    <p>Card content</p>  </div></div>

As described in@container, the CSS for the container element specifies the type of the container, and may also specify a name for the container.The card has a default font size, which is overridden for the@container namedsidebar if the minimum width is greater than 700px.

html
<style>  .post {    container-type: inline-size;    container-name: sidebar;  }  /* Default heading styles for the card title */  .card h2 {    font-size: 1em;  }  @container sidebar (width >= 700px) {    .card {      font-size: 2em;    }  }</style>

The code for getting the sheet and rules is almost identical to the previous example.The only difference is that in this example we have three CSS rules, so to get the associatedCSSContainerRule we get the third entry in thecssRules.

js
const exampleStylesheet = document.getElementById("example-styles").sheet;const exampleRules = exampleStylesheet.cssRules;const containerRule = exampleRules[2]; // a CSSContainerRule representing the container rule.log(`CSSContainerRule.containerName: "${containerRule.containerName}"`);log(`CSSContainerRule.containerQuery: "${containerRule.containerQuery}"`);log(`CSSContainerRule.conditionText: "${containerRule.conditionText}"`);

The example output is shown below.The log section lists thecontainerName andcontainerQuery strings.TheconditionText is also logged, and shows the combination of these two strings.The title in the card section should double in size as the width of the page goes over 700px.

Specifications

Specification
CSS Conditional Rules Module Level 5
# the-csscontainerrule-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