brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: the :has() css selector styles a parent based on its children

Css
~/tips/tip-css-has-selects-parent-element

label:has(input:checked) { background: #e6f4ea; }

Selects an element depending on what it contains, so a parent can be styled by its children.

For years I wanted a parent selector, and :has() is the answer.

label:has(input:checked) { background: #e6f4ea; }
.card:has(img) { grid-template-columns: 8rem 1fr; }
form:has(:invalid) button { opacity: .5; }

:has() matches an element when something inside it, or next to it, matches the selector list in the brackets. The first line highlights a label whose checkbox is ticked, the second changes a card layout only when it contains an image. It works in all current browsers (Firefox since version 121). Keep the inner selector narrow: a broad pattern like body:has(.x) forces the browser to re-check a lot of the page on every change.

Further reading: MDN: :has()

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link