CSS has advanced enormously in recent years — features that used to need JavaScript or preprocessors are now native. This closing chapter covers the modern tools that make responsive, maintainable CSS dramatically easier: fluid sizing, container queries, :has(), and more.
Fluid design with clamp()
clamp(min, preferred, max) gives a value that scales with the viewport but stays within bounds — responsive typography and spacing with no media queries:
h1 { font-size: clamp(1.75rem, 5vw, 3.5rem); } /* fluid heading */.section { padding: clamp(2rem, 6vw, 6rem); } /* fluid spacing */.container { width: min(100% - 2rem, 1200px); } /* min()/max() too */
Container queries
Media queries respond to the viewport. Container queries respond to a parent's size — so a component can adapt based on where it's placed (a card in a sidebar vs full-width), the missing piece for truly reusable components:
.card-list { container-type: inline-size; }@container (min-width: 400px) {.card { display: grid; grid-template-columns: 120px 1fr; }}
The :has() parent selector
Long the "holy grail", :has() styles an element based on what it contains — the first true parent selector:
/* A card that contains an image gets different padding */.card:has(img) { padding: 0; }/* Style a label when its input is checked */label:has(input:checked) { font-weight: 600; }/* A form that has an invalid field */form:has(:invalid) .submit { opacity: 0.5; }
Logical properties
Instead of left/right, logical properties use inline-start/block-start — which automatically flip for right-to-left languages, making internationalisation effortless:
margin-inline: auto; /* left+right (or start+end in RTL) */padding-block: 1rem; /* top + bottom */border-inline-start: 2px solid; /* the 'start' edge — flips in RTL */
Native nesting & variables
:root { --brand: #2563eb; --space: 1rem; }.card {padding: var(--space);border: 1px solid var(--brand);& .title { color: var(--brand); } /* native nesting — no preprocessor */&:hover { transform: translateY(-2px); }}
Practice
Make a heading fluid with clamp(). Turn a card list into a container context and switch a card to a two-column layout with a @container query. Use :has() to style a label whose checkbox is checked, and refactor a component to use nesting and --custom-properties. You've now completed CSS end to end.
Key takeaways
- 01clamp(min, preferred, max) gives fluid, bounded values — responsive type/spacing with no media queries.
- 02Container queries (@container) respond to a parent's size — the key to truly reusable components.
- 03:has() styles an element by its contents — the long-awaited parent selector.
- 04Logical properties (inline/block, margin-inline) auto-flip for RTL languages.
- 05Native nesting and custom properties (var()) organise and theme CSS without a preprocessor.
