Container style queries

컨테이너 스타일 쿼리

Child styles can respond to a parent container’s custom property value.

···
html
<div class="v"><header>STYLE QUERY <span id="mode">--mode: day</span></header><div class="stage cq"><div class="container" id="container"><div class="card">child card<br><b id="word">Day</b></div></div></div><div class="muted">parent state → child style</div></div>
css
.v{width:min(94vw,760px);height:min(88vh,320px);padding:clamp(9px,2.4vmin,18px);border:1px solid var(--line);border-radius:12px;background:var(--surface);font:500 clamp(12px,2.4vmin,16px)/1.3 var(--font-sans,system-ui,sans-serif);display:flex;flex-direction:column;gap:clamp(5px,1.4vmin,10px);overflow:hidden}.v header{display:flex;align-items:center;justify-content:space-between;gap:8px;color:var(--accent);font-weight:700}.v header span{color:var(--muted);font-weight:500}.v .stage{flex:1;min-height:0;position:relative}.v .muted{color:var(--muted)}.v .active{background:color-mix(in srgb,var(--accent) 18%,var(--surface));border-color:var(--accent)!important;color:var(--fg)}.v button{font:inherit;color:var(--fg);background:var(--bg);border:1px solid var(--line);border-radius:7px;padding:5px 8px;cursor:pointer}.cq{display:grid;place-items:center}.container{--mode:day;container-name:theme;container-type:normal;width:70%;height:80%;padding:8px;border:2px dashed var(--accent);border-radius:9px;display:grid;place-items:center}.container .card{width:75%;height:75%;border-radius:8px;display:grid;place-items:center;text-align:center;background:#f4d999;color:#3d2e18;transition:background .4s,color .4s}.container.night .card{background:#263f61;color:#fff}@container theme style(--mode: night){.card{background:#263f61;color:white}}
js
let night=false;function tick(){night=!night;const c=document.getElementById("container");c.style.setProperty("--mode",night?"night":"day");c.classList.toggle("night",night);document.getElementById("mode").textContent="--mode: "+(night?"night":"day");document.getElementById("word").textContent=night?"Night":"Day"}tick();setInterval(tick,1100)

Container style queries check a custom property on an ancestor container and apply conditional styles. Size queries react to available space; style queries react to theme or state values.

The demo switches the parent `--mode` value and the child card changes between day and night. Check browser support and where the property is inherited.

When to use

Use it when components should style themselves from a parent theme or state.

Open as page ↗