Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

container — query container shorthand

The CSS container property sets container-name and container-type together. It names a container and specifies which queries can use it to conditionally style descendants.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

container: <'container-name'> [ / <'container-type'> ]?;

container is shorthand for container-name and container-type, not the @container rule. Names go before /, and the type goes after it. The declaration container: demo / inline-size assigns the name demo and enables size queries along the container’s inline axis.

inline-size concerns the inline axis, whereas size concerns both axes. These types also constrain how the container’s size depends on its contents, so provide appropriate dimensions through the page layout. A name alone does not enable size queries.

Omitting the type resets container-type to normal: container: demo does not preserve an earlier inline-size. container: none removes the names and also resets the type. Neither longhand is inherited.

The rule @container demo (...) selects the nearest eligible ancestor named demo; an ordinary element cannot query itself this way. In the example, the condition changes .card’s background, not .panel’s. The query depends on the container’s size rather than directly on the viewport’s size.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Named container query at two widths</title>
  <style>
    .panel {
      container: demo / inline-size;
      writing-mode: horizontal-tb;
      margin-block-end: 8px;
    }
    .wide { inline-size: 320px; }
    .narrow { inline-size: 280px; }
    .card { background-color: lightgray; color: black; padding: 8px; }
    @container demo (inline-size >= 300px) {
      .card { background-color: lightgreen; }
    }
  </style>
</head>
<body>
  <div class="panel wide"><div class="card">Card in the 320px container</div></div>
  <div class="panel narrow"><div class="card">Card in the 280px container</div></div>
  <script>
    for (const panel of document.querySelectorAll('.panel')) {
      const width = getComputedStyle(panel).inlineSize;
      const color = getComputedStyle(panel.querySelector('.card')).backgroundColor;
      console.log(`${width}: ${color}`);
    }
  </script>
</body>
</html>

Result

320px: rgb(144, 238, 144)
280px: rgb(211, 211, 211)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources