Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

container-name — naming query containers

The container-name property assigns names to an element so @container rules can select a container. Naming alone does not enable size queries: those require an appropriate container-type.

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

Syntax

container-name: none | <custom-ident>+;

container-name assigns an element one or more names that filter the containers considered by @container. A query selects the nearest eligible ancestor with the specified name; it does not query the styled element itself. Names do not have to be unique.

The initial value none means no name, not that all queries are disabled. The notation <custom-ident>+ means one or more space-separated names, without commas or quotation marks, such as panel sidebar. Names are case-sensitive. none, and, not, and or cannot be names; CSS-wide keywords are not names either.

The property applies to all elements, is not inherited, and is not animatable. It does not replace container-type. In a horizontal writing mode, container-type: inline-size enables queries on physical width (width). In a vertical writing mode, however, the inline axis corresponds to height, so a container with inline-size is not eligible for a width query. Use container-type: size in that case to enable queries on both axes. The shorthand container: panel / inline-size sets both properties; omitting the part after / resets container-type to normal.

The example uses the default horizontal writing mode, and the container is 320px wide, so the named query with a 300px threshold gives the .card descendant 16px padding. Changing the container name to other prevents the match and leaves 4px.

Examples

<!doctype html>
<meta charset="utf-8">
<title>container-name</title>
<style>
  .panel {
    width: 320px;
    container-type: inline-size;
    container-name: panel sidebar;
  }
  .card { padding: 4px; }
  @container panel (min-width: 300px) {
    .card { padding: 16px; }
  }
</style>
<div class="panel">
  <div class="card">Card</div>
</div>
<script>
  const card = document.querySelector('.card');
  console.log(getComputedStyle(card).paddingTop);
</script>

Result

16px

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