:has() — the Parent Selector
:has() selects a parent element if it contains a specific child. It is CSS's long-awaited parent selector, now supported in all modern browsers.
:has() selects a parent element if it contains a specific child. It is CSS's long-awaited parent selector, now supported in all modern browsers.
/* Card with an image gets different padding */
.card:has(img) { padding: 0; }
.card:not(:has(img)) { padding: 1.5rem; }
/* Form with errors shows error styling */
form:has(input:invalid) .submit-btn {
opacity: 0.5;
pointer-events: none;
}
/* Figure that has a figcaption */
figure:has(figcaption) { margin-bottom: 2rem; }
:has() is a game changer — it enables styling based on child state without JavaScript class toggling.