</>HTML & CSS
AdvancedLesson 15 of 15

Modern CSS: :has(), Nesting, Layers & Container Queries – HTML & CSS

Current features that make a lot of JavaScript and preprocessor code unnecessary.

CSS has evolved a lot. :has() is the long-awaited "parent selector": .karte:has(img) matches cards that contain an image; label:has(input:checked) colors the label to match the state. Native nesting allows nested rules as in Sass – with & for the parent element. :is() and :where() combine selector lists.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.auswahl { display: grid; gap: 8px; max-width: 280px; }
.auswahl label { display: block; padding: 10px 12px; border: 2px solid #ccc; border-radius: 10px; cursor: pointer;
  &:hover { border-color: #888; }
  &:has(input:checked) { border-color: #2b59c3; background: #eaf0ff; font-weight: 700; }
  & input { margin-right: 8px; }
}
.gruppe:has(:invalid) { outline: 2px solid #d33; }
</style>
<div class="auswahl">
  <label><input type="radio" name="p" checked> Basic</label>
  <label><input type="radio" name="p"> Pro</label>
  <label><input type="radio" name="p"> Team</label>
</div>

@layer orders the cascade: layers later in the order win regardless of specificity – ideal for cleanly separating library CSS, base styles and component styles. Container queries (@container) adapt components to the size of their container instead of the whole window – components become truly reusable.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
@layer basis, komponenten;
@layer basis { .titel { color: gray; font-size: 1rem; } }
@layer komponenten { .titel { color: #2b59c3; } }     /* wins despite equal specificity */

.wrapper { container-type: inline-size; border: 2px dashed #999; padding: 8px; margin-bottom: 10px; }
.produkt { display: grid; gap: 6px; background: #f5f5f5; padding: 10px; }
@container (min-width: 380px) { .produkt { grid-template-columns: 80px 1fr; align-items: center; } }
.bild { height: 60px; background: #bbd; }
</style>
<p class="titel">Title from @layer komponenten</p>
<div class="wrapper" style="width:240px"><div class="produkt"><div class="bild"></div><div>Narrow container: stacked</div></div></div>
<div class="wrapper"><div class="produkt"><div class="bild"></div><div>Wide container: side by side</div></div></div>

More helpers: aspect-ratio: 16 / 9 (aspect ratio without a hack), logical properties (margin-inline, padding-block, inset) for languages with a different writing direction, min()/max()/clamp(), color-mix() for color variants, text-wrap: balance for balanced headings and scroll-snap for carousels.

<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
:root { --marke: #e34f26; }
.video { aspect-ratio: 16 / 9; background: #234; color: #fff; display: grid; place-items: center; max-width: 320px; }
.btn { padding-block: .6rem; padding-inline: 1.2rem; border: 0; border-radius: 8px; color: #fff;
       background: var(--marke); cursor: pointer; }
.btn:hover { background: color-mix(in srgb, var(--marke), black 20%); }
h2 { text-wrap: balance; max-width: 20ch; }
.snap { display: flex; overflow-x: auto; scroll-snap-type: x mandatory; gap: 10px; }
.snap div { flex: 0 0 70%; scroll-snap-align: center; height: 60px; background: #cde; display: grid; place-items: center; }
</style>
<div class="video">16:9 via aspect-ratio</div>
<p><button class="btn">Button with color-mix on hover</button></p>
<h2>Balanced heading with text-wrap balance</h2>
<div class="snap"><div>1</div><div>2</div><div>3</div></div>
WarningCheck browser support before using them (caniuse.com, MDN) and apply progressive enhancement for important functions: first a working base, then modern features – with @supports (selector(:has(*))) or as a pure enhancement without loss of function.
TipMany of these features (:has(), nesting, container queries, @layer, color-mix) are supported by all current browsers ("Baseline"). A look at webstatus.dev shows you the current state.