【CSS】コンテナスタイルクエリ(container style queries)について

  • URLをコピーしました!

この記事は学習ノートです。間違っている表現や説明があると思いますが、気づいた時点で修正していきます。

コンテナスタイルクエリとは、祖先要素のスタイルに基づいて子孫要素のスタイルを定義できる機能です。例えば、祖先要素の「背景色が黒色のとき」や「文字色が赤色のとき」に子孫要素のスタイルを自由に定義できます。

似た用語でコンテナサイズクエリと呼ばれる機能があります。こちらは、祖先要素のサイズに基づいて子孫要素のスタイルを定義できます。コンテナサイズクエリについてはこちらの記事をご覧ください↓

以降では、「コンテナスタイルクエリ」のことを「スタイルクエリ」、「コンテナサイズクエリ」のことを「サイズクエリ」と表現して説明します。

目次

使い方

サイズクエリを使用するときはcontainer-typeを指定する必要がありましたが、スタイルクエリを使用するときは必要ありません。

全ての要素がデフォルトでスタイルクエリを使用できる状態です。

Unless otherwise noted, all elements are query containers for the purpose container queries that do no require explicit containment (such as container style queries), reagardless of the specified container-type.

4.1. Creating Query Containers: the container-type property

サイズクエリとの違いは、スタイルの条件を定義するときにstyle()を使用する点です。

通常の例

具体的な例を見てみます。以下のようなHTMLを用意しました↓

<div class="ancestor">
  <div class="parent">
    <div class="child">child</div>
  </div>
</div>

class属性childを持つdiv要素(以後childと表記)から見て祖先に当たる要素を基準としてスタイルを定義していきます。

例えば、「class属性parentを持つdiv要素(以後parentと表記)の--textSizelarge」のときスタイルを定義する場合以下のように記述します↓

<style>
  .parent {
  --textSize:large;
}

@container  style(--textSize:large) {
  .child {
    font-size:120px;
  }
}

</style>
<div class="ancestor">
  <div class="parent">
    <div class="child">child</div>
  </div>
</div>

直接の親ではない要素(今回の場合、class属性ancestorを持つdiv要素(以後、ancestorと表記))に対してもスタイルクエリは有効です↓

<style>
  .ancestor {
  --textSize:large;
}

@container  style(--textSize:large) {
  .child {
    font-size:120px;
  }
}

</style>
<div class="ancestor">
  <div class="parent">
    <div class="child">child</div>
  </div>
</div>

container-nameを使用する

通常の例で用意したindex.htmlをそのまま流用します。

ancestorparentの両方に異なる--textSizeの値が指定されていた場合どうなるでしょうか?

それは、childから見て直近の親要素であるparent--textSizeの値が優先されます↓

<style>
  .ancestor {
    --textSize: large;
  }

  .parent {
    --textSize: small;
  }

  @container style(--textSize:large) {
    /*parentのtextSizeがsmallのため、この条件は無効です*/
    .child {
      font-size: 120px;
    }
  }
</style>
<div class="ancestor">
  <div class="parent">
    <div class="child">child</div>
  </div>
</div>

ancestorchildのスタイルクエリしたい場合は、container-nameを使用します↓

<style>
  .ancestor {
    --textSize: large;
    container-name:large;
  }

  .parent {
    --textSize: small;
  }

  @container large style(--textSize:large) {
     /*childのスタイルクエリはancestorであるためこの条件は有効です*/
    .child {
      font-size: 120px;
    }
  }
</style>
<div class="ancestor">
  <div class="parent">
    <div class="child">child</div>
  </div>
</div>

注意点

カスタムプロパティにのみ対応

2023年4月17日現在では、カスタムプロパティによるスタイル定義のみ対応しています。

background-color#fffのとき」や「font-size24pxのとき」などの条件は今のところ適用されません。

Adds the style() function to @container rules to make it possible to apply styles based on the computed values of custom properties of an ancestor element.

Style Container Queries for CSS Custom Properties

ショートハンドプロパティも使用できる

@container style(margin:40px) {...}のように指定することができます。この場合、margin-topmargin-rightmargin-bottommargin-leftの値全てが40pxのとき条件が有効になります。

Style features that query a shorthand property are true if the computed values match for each of its longhand properties, and false otherwise.

5.2. Style Container Features

revertやrevert-layerは使用できない

revertrevert-layerなどは、スタイルクエリの値として使用できません。

その他のグローバルな値initialinheritunsetは使用できます。

Cascade-dependent keywords, such as revert and revert-layer, are invalid as values in a style feature, and cause the container style query to be false.

Note: The remaining non-cascade-dependent CSS-wide keywords are computed with respect to the query container, the same as other values.

5.2. Style Container Features

実際の使用例(Twitterより)

Twitterでスタイルクエリの使用例が紹介されていました。

のうめんさん↓

久保さん↓

Ahmad Shadeedさん↓

ブラウザ対応状況

2023年4月17日現在ではChrome111〜とEdge111〜のみ対応で、カスタムプロパティのみ機能します。

参考サイト

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次