大网数据广告活动
Web 标准与前端开发

CSSContainerRule: conditions property

· 52CE 我爱测

The read-only **`conditions`** property of the CSSContainerRule interface represents an associated CSS @container at-rule as an array of objects, where each object represents a single container condition.

CSSOM

The read-only conditions property of the CSSContainerRule interface represents an associated CSS @container at-rule as an array of objects, where each object represents a single container condition.

Value

An array of objects where each object has the form:

({ name: "<container-name>", query: "<container-query>" });

Either the name or query may be the empty string, but not both.

Description

The conditions property represents an associated CSS @container at-rule as an array of objects.

Each object represents a container condition as a name string property and a query string property, either of which may be the empty string if not defined. The name represents the name of a container, and the query string represents the set of feature tests that must be true for the particular container condition to match.

For example, given the following @container:

@container sidebar (width >= 700px), (height >= 400px) {
  /* Styles */
}

The conditions would be an array like this:

[
  { name: "sidebar", query: "(width >= 700px)" },
  { name: "", query: "(height >= 400px)" },
];

Examples

See also Examples in CSSContainerRule.

Basic usage

The example shows how multiple container conditions are represented in the conditions property.

Note that we've hidden the logging code, as it is not relevant.

<pre id="log"></pre>
const logElement = document.querySelector("#log");
function log(text) {
  logElement.innerText = `${logElement.innerText}${text}\n`;
  logElement.scrollTop = logElement.scrollHeight;
}
#log {
  height: 100px;
  overflow: scroll;
  padding: 0.5rem;
  border: 1px solid black;
}

HTML

First, we define the HTML for a card contained within a post. These are represented by two nested div elements.

<div class="post">
  <div class="card">
    <h2>Card title</h2>
    <p>Card content</p>
  </div>
</div>

CSS

The CSS for the container element specifies the type of the container, and may also specify a name. The card has a default font size, which is overridden when it is contained inside a sidebar @container when its width is greater than or equal to 700px, or when it is inside a container named other-name. Note that this condition is contrived to demonstrate how multiple conditions are represented (other-name doesn't actually do anything).

<style id="example-styles">
  .post {
    container-type: inline-size;
    container-name: sidebar;
  }

  /* Default heading styles for the card title */
  .card h2 {
    font-size: 1em;
  }

  @container sidebar (width >= 700px), other-name {
    .card {
      font-size: 2em;
    }
  }
</style>

JavaScript

The code below gets the HTMLStyleElement associated with the example using its id, and then uses its sheet property to get the CSSStyleSheet. From the CSSStyleSheet we get the set of cssRules added to the sheet. Since we added the @container as the third rule above, we can access the associated CSSContainerRule using the third entry (index "2") in the cssRules.

const exampleStylesheet = document.getElementById("example-styles").sheet;
const exampleRules = exampleStylesheet.cssRules;
const containerRule = exampleRules[2]; // a CSSContainerRule representing the container rule.

We then use the containerRule to log the value of the conditions property.

if ("conditions" in CSSContainerRule.prototype) {
  log("CSSContainerRule.conditions:");
  containerRule.conditions.forEach((item) => {
    const jsonString = JSON.stringify(item);
    log(`  ${jsonString}`);
  });
} else {
  log("CSSContainerRule.conditions is not supported.");
}

[!NOTE] On browsers that don't support conditions, you may be able to use CSSContainerRule.containerName and CSSContainerRule.containerQuery, provided that the @container only specifies one container condition. For more information, see the Feature testing example in CSSContainerRule.

Results

The example output is shown below.

Basic usage

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。