Modern CSS
:has(), @starting-style, field-sizing and OKLCH colour mixing, through utilities.
What it is
CSS has picked up features that used to need JavaScript, and Tailwind v4 exposes them as variants and utilities. has-*: styles an element from what's inside it. starting: sets where an entry animation begins, so popovers and dialogs can fade in with plain transitions. field-sizing-content lets a text box grow with what's typed. And colour now mixes in perceptual spaces: bg-accent/25 is color-mix(in oklab, …), and oklch(from …) derives a lighter or darker shade from one token.
When to use it
- has-*: when a parent's look depends on a child's state: checked, invalid, focused
- starting: with popover and <dialog> for entry and exit animations, no library
- field-sizing-content for comment boxes and tag inputs
- Opacity modifiers and oklch(from …) instead of hand-picking every tint
1. :has(), the parent selector
Release checklistAll done ✓
The plan cards use has-checked: and has-focus-visible:. The checklist shows its badge with group-not-has-[input:not(:checked)]:inline: “no unticked boxes left”.
2. Entry and exit animations with @starting-style
starting:open:opacity-0 is where the fade begins; open:opacity-100 where it ends. transition-discrete keeps the element on screen until the exit fade is done. With reduced motion set, they just appear.
3. Fields that fit their content
Your browser doesn't support field-sizing yet, so these stay a fixed size. It works in Chrome, Edge and Safari 26.2+.
4. Colour mixing
in srgb · the browser's old default
in oklab · Tailwind's choice for /opacity and gradients
in oklch · keeps chroma, travels round the hue wheel
Tints from one token
Top row: opacity modifiers, bg-accent/10 to /100 (color-mix in OKLab). Bottom row: lightness shifted with oklch(from var(--color-accent) calc(l + 0.15) c h) and so on.
/10/25/50/75/100−0.15±0+0.15+0.3+0.45Show code · src/app/tailwind/modern-css/Has.tsx
// :has() lets an element style itself from what's inside it, the "parent
// selector" CSS went without for 25 years. No state, no JavaScript: the
// browser already knows which radio is checked.
const plans = [
{ value: "hobby", name: "Hobby", price: "Free" },
{ value: "pro", name: "Pro", price: "$20/mo" },
{ value: "team", name: "Team", price: "$50/mo" },
];
const tasks = ["Write the tokens", "Check the contrast", "Ship it"];
export function PlanPicker() {
return (
<fieldset className="m-0 grid gap-2 border-0 p-0 sm:grid-cols-3">
<legend className="mb-2 p-0 text-sm font-semibold">Choose a plan</legend>
{plans.map((p) => (
<label key={p.value}
className="grid cursor-pointer gap-0.5 rounded-lg border border-line bg-bg p-3 has-checked:border-accent has-checked:bg-accent/10 has-focus-visible:outline-2 has-focus-visible:outline-offset-2 has-focus-visible:outline-accent">
<input type="radio" name="plan" value={p.value} defaultChecked={p.value === "pro"} className="sr-only" />
<span className="font-semibold">{p.name}</span>
<span className="text-sm text-muted">{p.price}</span>
</label>
))}
</fieldset>
);
}
export function Checklist() {
return (
<div className="group grid gap-2 rounded-lg border border-line bg-bg p-3 not-has-[input:not(:checked)]:border-accent">
<p className="m-0 flex items-center gap-2 text-sm font-semibold">
Release checklist
<span className="hidden rounded-full bg-accent px-2 py-0.5 text-xs text-white group-not-has-[input:not(:checked)]:inline" data-testid="all-done">
All done ✓
</span>
</p>
<ul className="m-0 grid list-none gap-1 p-0">
{tasks.map((t) => (
<li key={t}>
<label className="flex cursor-pointer items-center gap-2 text-sm has-checked:text-muted has-checked:line-through">
<input type="checkbox" /> {t}
</label>
</li>
))}
</ul>
</div>
);
}
Show code · src/app/tailwind/modern-css/Overlays.tsx
"use client";
import { useRef } from "react";
// Entry and exit animations in CSS alone. starting: (@starting-style) gives
// the values to animate *from* when the element first appears; transition-discrete
// (transition-behavior: allow-discrete) lets display and overlay wait for
// the fade to finish before the element disappears.
const motion =
"transition-[opacity,translate,scale,overlay,display] transition-discrete duration-200 ease-out motion-reduce:transition-none";
export function Overlays() {
const dialog = useRef<HTMLDialogElement>(null);
return (
<div className="flex flex-wrap gap-2">
<button type="button" popoverTarget="tip" className="cursor-pointer rounded-md border border-line px-3 py-1.5 text-sm">
Open a popover
</button>
<div id="tip" popover="auto" data-testid="popover"
className={`m-auto max-w-[min(20rem,calc(100vw-2rem))] rounded-xl border border-line bg-surface p-4 text-fg shadow-lg ${motion} translate-y-2 opacity-0 open:translate-y-0 open:opacity-100 starting:open:translate-y-2 starting:open:opacity-0`}>
<p className="m-0 text-sm">
A popover: the <code>popover</code> attribute and a <code>popovertarget</code> button. It sits on the top layer and
closes on Esc or a click outside, without any JavaScript.
</p>
</div>
<button type="button" onClick={() => dialog.current?.showModal()} className="cursor-pointer rounded-md bg-accent px-3 py-1.5 text-sm text-white">
Open a dialog
</button>
<dialog ref={dialog} aria-labelledby="dialog-title" data-testid="dialog"
className={`m-auto max-w-[min(24rem,calc(100vw-2rem))] rounded-xl border border-line bg-surface p-5 text-fg ${motion} scale-95 opacity-0 open:scale-100 open:opacity-100 starting:open:scale-95 starting:open:opacity-0 backdrop:bg-black/50`}>
<h3 id="dialog-title" className="mt-0">A modal dialog</h3>
<p className="text-sm">It scales and fades in, and back out when it closes. Esc works too.</p>
<form method="dialog">
<button className="cursor-pointer rounded-md bg-accent px-3 py-1.5 text-sm text-white">Close</button>
</form>
</dialog>
</div>
);
}
Show code · src/app/tailwind/modern-css/Grow.tsx
// field-sizing: content sizes a form field to what's typed in it, the job
// that used to need a script measuring scrollHeight. Min and max sizes keep it
// in bounds.
export function Grow() {
return (
<div className="grid gap-3">
<label className="grid gap-1 text-sm">
<span>Message (grows taller)</span>
<textarea rows={1} defaultValue="Type a few lines here…" data-testid="grow-textarea"
className="field-sizing-content min-h-10 max-h-60 w-full resize-none rounded-md border border-line bg-bg px-2 py-1.5 text-fg focus:outline-2 focus:outline-accent" />
</label>
<label className="flex flex-wrap items-center gap-2 text-sm">
<span>Tag (grows wider)</span>
<input defaultValue="css" data-testid="grow-input" className="field-sizing-content min-w-16 max-w-full" />
</label>
<p className="m-0 text-xs text-muted supports-[field-sizing:content]:hidden" data-testid="grow-unsupported">
Your browser doesn't support <code>field-sizing</code> yet, so these stay a fixed size. It works in Chrome,
Edge and Safari 26.2+.
</p>
</div>
);
}
Show code · src/app/tailwind/modern-css/Mixing.tsx
"use client";
import { type CSSProperties, useState } from "react";
// The same two colours blended in three colour spaces. sRGB blends the
// numbers on the screen, so blue to yellow dips through grey; OKLab and OKLCH
// blend what the eye sees, so the middle stays bright.
const spaces = [
{ space: "srgb", note: "the browser's old default" },
{ space: "oklab", note: "Tailwind's choice for /opacity and gradients" },
{ space: "oklch", note: "keeps chroma, travels round the hue wheel" },
];
export function Mixing() {
const [a, setA] = useState("#2563eb");
const [b, setB] = useState("#facc15");
return (
<div className="grid gap-3" style={{ "--a": a, "--b": b } as CSSProperties}>
<div className="flex flex-wrap gap-4 text-sm">
<label className="flex items-center gap-2">From <input type="color" value={a} onChange={(e) => setA(e.target.value)} /></label>
<label className="flex items-center gap-2">To <input type="color" value={b} onChange={(e) => setB(e.target.value)} /></label>
</div>
{spaces.map(({ space, note }) => (
<div key={space} className="grid gap-1">
<p className="m-0 text-xs"><code>in {space}</code> <span className="text-muted">· {note}</span></p>
<div className="h-8 rounded-md" data-testid={`mix-${space}`}
style={{ backgroundImage: `linear-gradient(to right in ${space}, var(--a), var(--b))` }} />
</div>
))}
</div>
);
}
// Tints and transparency from one token. Opacity modifiers compile to
// color-mix(in oklab, …), and relative colour syntax (oklch(from …)) shifts
// lightness alone. (Classes written in full so Tailwind can find them.)
const swatch = "h-10 rounded-md border border-line";
export function Tints() {
return (
<div className="grid gap-3">
<div className="grid grid-cols-5 gap-2 text-center text-xs">
{[
["/10", `${swatch} bg-accent/10`],
["/25", `${swatch} bg-accent/25`],
["/50", `${swatch} bg-accent/50`],
["/75", `${swatch} bg-accent/75`],
["/100", `${swatch} bg-accent`],
].map(([label, cls]) => (
<div key={label} className="grid min-w-0 gap-1"><div className={cls} /><code className="truncate">{label}</code></div>
))}
</div>
<div className="grid grid-cols-5 gap-2 text-center text-xs">
{[
["−0.15", `${swatch} bg-[oklch(from_var(--color-accent)_calc(l_-_0.15)_c_h)]`],
["±0", `${swatch} bg-[oklch(from_var(--color-accent)_l_c_h)]`],
["+0.15", `${swatch} bg-[oklch(from_var(--color-accent)_calc(l_+_0.15)_c_h)]`],
["+0.3", `${swatch} bg-[oklch(from_var(--color-accent)_calc(l_+_0.3)_c_h)]`],
["+0.45", `${swatch} bg-[oklch(from_var(--color-accent)_calc(l_+_0.45)_c_h)]`],
].map(([label, cls]) => (
<div key={label} className="grid min-w-0 gap-1"><div className={cls} /><code className="truncate">{label}</code></div>
))}
</div>
</div>
);
}