https://chromestatus.com/feature/5072263730167808
詳しい説明
https://lilles.github.io/explainers/state_container_queries.html
position: sticky でスクロールしたときに要素を簡単に固定できるようになりましたが 固定されてるときだけスタイルを変えたいということはよくありました
それができるようになります
使い方はコンテナクエリを使って
@container state(stuck: top) {
#sticky-child { font-size: 75% }
}
のようになるそうです
HTML の例だと
<style>
#sticky {
container-name: my-menu;
container-type: sticky;
position: sticky;
top: 0px;
height: 100px;
}
#sticky-child {
background-color: orange;
color: white;
height: 100%;
}
@container my-menu state(stuck: top) {
#sticky-child { width: 50%; }
}
</style>
<div id="sticky">
<div id="sticky-child">
Sticky
</div>
</div>
できるようになるのは嬉しいのですが 少し手間なのが難点ですね
コンテナクエリが必須ですし contaner-type の指定も必要です
お手軽に擬似クラスで :stuck でいいと思うのですが パフォーマンスの懸念など色々あってコンテナクエリになったようです