<div class="post">
<div class="card">
<h2>Card title</h2>
<p>Card content</p>
</div>
</div>CSS container queries / notebook
CSS container queries
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
.post {
container-type: inline-size;
}/* Default heading styles for the card title */
.card h2 {
font-size: 1em;
}
/* If the container is larger than 700px */
@container (width > 700px) {
.card h2 {
font-size: 2em;
}
}.post {
container-type: inline-size;
container-name: sidebar;
}@container sidebar (width > 700px) {
.card {
font-size: 2em;
}
}<div id="container">
<p>I'm in the container.</p>
<p>I'm also in the container.</p>
</div>
<p>I'm not in the container.</p>#container {
container-name: my-container;
}@container my-container {
p {
background-color: lime;
font-size: 1.3rem;
width: 50vw;
padding: 0.5rem;
font-family: sans-serif;
}
}.post {
container: sidebar / inline-size;
}@container (width > 700px) {
.card h2 {
font-size: max(1.5em, 1.23em + 2cqi);
}
}.card {
display: grid;
grid-template-columns: 2fr 1fr;
}@media (width <= 700px) {
.card {
grid-template-columns: 1fr;
}
}