Responsive card
.card-shell { container-type: inline-size; }
@container (min-width: 30rem) {
.card { display: grid; grid-template-columns: 10rem 1fr; }
}
@container conditionally applies styles according to a containing element instead of the viewport. This lets one card, toolbar, or navigation component adapt wherever it is placed.
For width queries, set container-type: inline-size on an ancestor. The query styles descendants.
.wrapper { container-type: inline-size; }
@container (width >= 30rem) {
.card { grid-template-columns: 1fr 2fr; }
}| Form | Purpose | Try it |
|---|---|---|
| minimum width | Enhance when the container is wide enough | Try it |
| maximum width | Simplify in a narrow container | Try it |
| named container | Target a specific ancestor | Try it |
| range syntax | Limit a layout to a width interval | Try it |
| container units | Size content with cqi units | Try it |
| nested containers | Use the nearest matching query container | Try it |
.card-shell { container-type: inline-size; }
@container (min-width: 30rem) {
.card { display: grid; grid-template-columns: 10rem 1fr; }
}.sidebar { container: sidebar / inline-size; }
@container sidebar (width > 20rem) {
.nav-label { display: inline; }
}.hero-title { font-size: clamp(1.5rem, 8cqi, 4rem); }Size container queries are browser layout features.
Inline-size queries work in current Chrome, Edge, Firefox, Safari, and Opera.
Bottom line: Use size queries in current browsers. Keep a one-column baseline outside the query for older clients.
Declare namespaces for SVG and XML selectors.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful