@container <container-condition># {
/* <stylesheet> */
}Using container size and style queries / notebook
Using container size and style queries
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
@container (orientation: landscape) {
/* styles applied to descendants of this size container */
}
.sizeContainer {
container-type: size;
}form {
container-type: inline-size;
}
@container (10em <= width <= 20em) {
/* styles */
}@container [ <container-name>? <container-query>? ]!# {
/* <stylesheet> */
}@container card (orientation: landscape) {
/* styles */
}
.todo-panel > li {
container-type: inline-size;
container-name: card;
}@container wide (width >= 20em) {
/* styles applied to descendants of wide .sizeContainer */
}
@container narrow (width < 20em) {
/* styles applied to descendants of narrow .sizeContainer */
}
.sizeContainer {
container-type: size;
container-name: wide narrow;
}@container style(<style-feature>),
not style(<style-feature>),
style(<style-feature>) and style(<style-feature>),
style(<style-feature>) or style(<style-feature>) {
/* <stylesheet> */
}@container style(color: green) and style(background-color: transparent),
not style(background-color: red),
style(--themeBackground),
style(--themeColor: blue) or style(--themeColor: purple),
(width <= 100vw) and style(max-width: 600px) {
/* <stylesheet> */
}@container style(--themeBackground),
style(--themeColor: blue) or style(--themeColor: purple) {
/* <stylesheet> */
}:root {
--theme-color: rebeccapurple;
}
@container style(--theme-color) {
/* <stylesheet> */
}@property --theme-color {
initial-value: rebeccapurple;
inherits: true;
}
:root {
--theme-color: rebeccapurple;
}
main {
--theme-color: blue;
}
@container style(--theme-color) {
/* <stylesheet> */
}@container style(--accent-color: blue) {
/* <stylesheet> */
}@property --accent-color {
syntax: "<color>";
inherits: true;
initial-value: #0000ff;
}<fieldset>
<legend>Change the value of <code>--theme</code></legend>
<ol>
<li>
<input type="radio" name="selection" value="red" id="red" />
<label for="red">--theme: red;</label>
</li>
<li>
<input type="radio" name="selection" value="green" id="green" />
<label for="green">--theme: green</label>
</li>
<li>
<input type="radio" name="selection" value="blue" id="blue" />
<label for="blue">--theme: blue</label>
</li>
<li>
<input type="radio" name="selection" value="currentColor" id="other" />
<label for="other">Other</label>
<label for="color">color:</label>
<input name="selection-value" value="currentColor" id="color" />
</li>
</ol>
</fieldset>
<output>I change colors</output>const radios = document.querySelectorAll('input[name="selection"]');
const body = document.querySelector("body");
const other = document.getElementById("other");
const color = document.getElementById("color");
for (const radio of radios) {
radio.addEventListener("change", (e) => {
body.style.setProperty("--theme", e.target.value);
});
}
color.addEventListener("input", (e) => {
other.style.setProperty("value", e.target.value);
if (other.checked) {
body.style.setProperty("--theme", e.target.value);
}
});@property --theme {
syntax: "<color>";
inherits: true;
initial-value: red;
}