For most of the history of CSS, selectors could only look down and forward. You could style a child based on its parent, or a sibling based on the one before it, but never the other way round. If you wanted a card to look different when it contained an image, you added a class in your template or reached for JavaScript.
The :has() pseudo-class changes that. It lets you select an element based on what is inside it, or what comes after it. People call it the parent selector, but that undersells it. It is closer to a general "if this element matches this condition" tool, and once you start using it you find uses everywhere.
This article is for front-end developers who know their way around selectors and want to use :has() in real projects. We start with the basics, move through forms, layout and quantity queries, and finish with performance notes and when to avoid it.
The basic idea
#:has() takes a relative selector list as its argument. The element matches if at least one of those selectors matches something relative to it.
_10/* Any .card that contains an img anywhere inside it */_10.card:has(img) {_10 padding: 0;_10}_10_10/* Any .card whose direct child is an img */_10.card:has(> img) {_10 display: grid;_10 grid-template-rows: auto 1fr;_10}
Read the first rule as "a card that has an image". The styles apply to the .card, not the image. That is the key difference from a normal descendant selector like .card img, which styles the image.
The argument is relative to the element you are testing, so you can start it with a combinator:
:has(img)means a descendant anywhere inside:has(> img)means a direct child:has(+ p)means the next sibling is a paragraph:has(~ p)means some later sibling is a paragraph
The sibling forms are what make :has() more than a parent selector. They let you style an element based on what comes after it, which CSS could never do before.
_10/* A heading that is immediately followed by a subtitle */_10h2:has(+ .subtitle) {_10 margin-block-end: 0.25rem;_10}
Combining with :not() and selector lists
#You can pass several selectors separated by commas. The element matches if any of them match.
_10/* Articles that contain either a video or an iframe embed */_10article:has(video, iframe) {_10 max-width: 100%;_10}
To require several things at once, chain :has() calls:
_10/* Cards that have both an image and a footer */_10.card:has(img):has(.card-footer) {_10 border-color: var(--accent);_10}
And to test for absence, wrap it in :not():
_10/* Cards with no image get a coloured top border instead */_10.card:not(:has(img)) {_10 border-block-start: 4px solid var(--accent);_10}
Be careful with the order here. .card:not(:has(img)) means "a card that does not contain an image". .card:has(:not(img)) means "a card that contains at least one element that is not an image", which is true for almost every card. The first one is nearly always what you want.
Styling forms based on their state
#Forms are where :has() earns its keep fastest. Input state pseudo-classes such as :checked, :invalid, :focus-visible and :placeholder-shown used to style only the input. Now the surrounding field, label or whole form can react too.
_10<div class="field">_10 <label for="email">Email</label>_10 <input id="email" type="email" required>_10 <p class="hint">We will never share it.</p>_10</div>
_14/* Highlight the whole field while its input is focused */_14.field:has(input:focus-visible) {_14 outline: 2px solid var(--focus);_14 outline-offset: 4px;_14}_14_14/* Show an error state only after the user has typed something invalid */_14.field:has(input:user-invalid) {_14 --field-colour: var(--error);_14}_14_14.field:has(input:user-invalid) .hint {_14 color: var(--error);_14}
:user-invalid only matches after the user has interacted with the field, so the error does not shout at someone who has not typed anything yet. It pairs well with :has() because the error styling usually belongs on the wrapper, not the input itself.
You can also disable a submit button visually until a checkbox is ticked:
_10form:has(#terms:not(:checked)) button[type="submit"] {_10 opacity: 0.5;_10 pointer-events: none;_10}
Treat that as a visual hint only. Keep the real validation in HTML attributes or your submit handler, because CSS cannot stop a keyboard user from pressing Enter.
Page-level state without JavaScript
#Because :has() can be used on html or body, a control anywhere on the page can change styles everywhere else. A classic example is a theme switcher built from radio buttons.
_10<fieldset class="theme-picker">_10 <legend>Theme</legend>_10 <label><input type="radio" name="theme" value="light" checked> Light</label>_10 <label><input type="radio" name="theme" value="dark"> Dark</label>_10</fieldset>
_14:root {_14 --bg: white;_14 --text: #111;_14}_14_14:root:has(input[name="theme"][value="dark"]:checked) {_14 --bg: #111;_14 --text: #eee;_14}_14_14body {_14 background: var(--bg);_14 color: var(--text);_14}
You would still need a little JavaScript to remember the choice between visits, but the switching itself is pure CSS.
Another common one is stopping the page from scrolling while a modal dialog is open:
_10html:has(dialog[open]:modal) {_10 overflow: hidden;_10}
Layout that adapts to its content
#:has() lets a component pick its layout based on what it has been given, which is handy when content comes from a CMS and you cannot control every combination.
_10.media {_10 display: grid;_10 gap: 1rem;_10}_10_10/* Only switch to two columns when there is actually an image */_10.media:has(> img) {_10 grid-template-columns: 200px 1fr;_10}
It also works nicely with container queries. The container query decides how much room there is; :has() decides what is there.
_10.card-list {_10 container-type: inline-size;_10}_10_10@container (min-width: 40rem) {_10 .card:has(.card-image) {_10 grid-template-columns: 1fr 1fr;_10 }_10}
Quantity queries
#A quantity query styles a container based on how many children it has. Before :has(), this needed some clever :nth-last-child tricks on the children themselves. Now you can put the condition on the parent.
_14/* A list with at least five items switches to two columns */_14ul:has(> li:nth-child(5)) {_14 columns: 2;_14}_14_14/* A grid with exactly one child centres it */_14.grid:has(> :only-child) {_14 justify-items: center;_14}_14_14/* Between 2 and 4 children */_14.grid:has(> :nth-child(2)):not(:has(> :nth-child(5))) {_14 grid-template-columns: repeat(2, 1fr);_14}
The trick is that if the fifth child exists, there are at least five children. Combining that with :not() gives you ranges.
Styling previous siblings
#The sibling combinators inside :has() let you style an element based on the element after it. A popular use is a hover effect where the items either side of the hovered one react too.
_17.dock-item {_17 transition: scale 150ms ease;_17}_17_17.dock-item:hover {_17 scale: 1.4;_17}_17_17/* The item just before the hovered one */_17.dock-item:has(+ .dock-item:hover) {_17 scale: 1.2;_17}_17_17/* The item just after the hovered one */_17.dock-item:hover + .dock-item {_17 scale: 1.2;_17}
A quieter but very practical example is dimming every row in a table except the one being hovered:
_10tbody:has(tr:hover) tr:not(:hover) {_10 opacity: 0.6;_10}
Specificity
#:has() takes the specificity of the most specific selector in its argument, the same way :is() and :not() do. The pseudo-class itself adds nothing extra.
_10/* Specificity is (0, 1, 1): .card plus img */_10.card:has(img) { }_10_10/* Specificity is (1, 1, 0): .card plus #hero */_10.card:has(#hero, img) { }
That second rule is worth noticing. Even when it matches because of the img, it still carries the ID's specificity, because the most specific item in the list wins. If you put IDs in a :has() list, you may find those rules hard to override later. If that becomes a problem, wrap the argument in :where() to drop its specificity to zero.
Things :has() cannot do
#A few limits are worth knowing so you do not lose an afternoon to them.
- You cannot nest
:has()inside another:has(). The browser ignores a selector likea:has(b:has(c)). - Pseudo-elements are not allowed inside the argument, so
:has(::before)will not work. - The argument list is unforgiving: if one selector in it is invalid or unsupported, the browser throws away the whole rule, not just that item. Keep each
:has()list simple, or wrap uncertain parts in:is(), which is forgiving.
Performance
#When :has() was first proposed, browser engineers worried about performance, because a change deep in the tree can affect whether an ancestor matches. The shipped implementations handle this well, and for normal use you do not need to think about it.
It can still be slow if you are careless with very broad selectors on very large pages. A few habits keep it fast:
- Anchor the subject to something specific.
.field:has(input:focus)is cheaper than*:has(input:focus)or a bare:has(input:focus), which means the same as the universal version. - Prefer the child combinator when you can.
:has(> img)only has to look one level down.:has(img)may need to search the whole subtree. - Be careful with
body:has(...)or:root:has(...)rules that depend on frequently changing state, such as:hover. Each change can make the browser re-check the whole document.
If you suspect a :has() rule is causing slow style recalculation, the Performance panel in Chrome DevTools shows the cost of "Recalculate Style" and can show which selectors took the longest when you enable selector stats.
Browser support
#:has() is supported in all current major browsers: Chrome and Edge 105, Safari 15.4 and Firefox 121. With Firefox 121 shipping in December 2023 it became Baseline that month, so it is safe to use on most sites today.
If you still need to support older browsers, treat :has() as an enhancement. You can check for support with @supports:
_10.card {_10 border-block-start: 4px solid var(--accent);_10}_10_10@supports selector(:has(*)) {_10 .card:has(img) {_10 border-block-start: none;_10 }_10}
In JavaScript, CSS.supports('selector(:has(*))') gives you the same answer.
When not to use it
#:has() is powerful enough that it is easy to overuse. Reach for something else when:
- The state already lives in your component. If React or another framework already knows a card has an image, adding a class or data attribute from that state is clearer and easier to debug than a selector that inspects the DOM.
- The logic is really behaviour.
:has()can make a button look disabled, but it cannot stop it being used. Anything that affects what the user can actually do belongs in HTML or JavaScript. - You are building long chains to recreate application logic. If a selector needs three
:has()calls and two:not()wrappers to express it, the next person to read it will struggle. A class name is often kinder. - The rule sits on the root and reacts to hover or input on every keystroke. It works, but it is the one pattern most likely to cause measurable style recalculation on large pages.
Wrapping up
#:has()selects an element based on what it contains or what follows it, so it covers parent selection, previous-sibling selection and much more.- Combine it with
:not()for absence, chain it for "and", and use a comma list inside it for "or". - Forms, page-level toggles, content-aware layouts and quantity queries are the strongest everyday uses.
- Keep selectors anchored and prefer the child combinator to stay fast on large pages.
- It has been Baseline since December 2023, but state that your components already know about is often better expressed as a class.
Further reading: MDN: :has()
CSSWas this article helpful?

About the author
Danny Engineering
A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.