<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd" -->

---
title: Can’t touch the DOM? Reach for :has() to style any element
description: When working with third-party component libraries like MUI, Ant Design, or AG Grid, developers often can&#x27;t safely edit the DOM to add custom styles. The CSS...
canonical: https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Can’t touch the DOM? Reach for :has() to style any element | daily.dev
og:description: When working with third-party component libraries like MUI, Ant Design, or AG Grid, developers often can&#x27;t safely edit the DOM to add custom styles. The CSS...
og:url: https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd
og:image: https://api.daily.dev/og/posts/7YalpjtNd.png
og:image:alt: Can’t touch the DOM? Reach for :has() to style any element
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Can’t touch the DOM? Reach for :has() to style any element

**[thoughbot](https://daily.dev/sources/thoughbot)** · 5 min read · 9 upvotes · 0 comments

## Summary

When working with third-party component libraries like MUI, Ant Design, or AG Grid, developers often can't safely edit the DOM to add custom styles. The CSS :has() pseudo-class solves this by allowing selection of parent and preceding sibling elements based on their descendants' state, something previously impossible in CSS. Practical examples include styling a label red when a sibling input has an error class, detecting table rows before a specific element, adding hover states that account for nested interactive elements, and theming an entire page based on the presence of one component.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://thoughtbot.com/blog/can-t-touch-the-dom-reach-for-has>

## Questions this post answers

### How can I style a parent element in CSS based on the state of its child?

Use the :has() pseudo-class, which lets a selector match a parent or ancestor based on whether it contains a matching descendant. For example, .input:has(.error) label turns a label red when a sibling input carries an error class. This solves cases where CSS previously could not select upward or backward through the DOM.

_Developers styling third-party UI components track CSS selector techniques like this on daily.dev._

### How do I style a table row based on the row that comes after it in CSS?

Use :has() with the next-sibling combinator, for example tr:has(+ .parent) applies a border-bottom to the row immediately before an element with the parent class. This effectively flips the usual next-sibling selector direction, something CSS could not previously do without :has().

_Anyone wrangling table styling in component libraries can keep tabs on CSS tricks like this via daily.dev._

### Can I change the entire page's styling based on whether one specific component is present?

Yes, using body:has(.special-component) as a selector applies conditional styles to any element on the page whenever an element with that class exists anywhere in the body. Combined with CSS custom properties, this lets a library's theme variables be overridden without touching its internal settings.

_daily.dev helps developers theming apps around dynamic components stay current on CSS capabilities like this._

## Similar posts on daily.dev

- [The different ways to use CSS :has\(\), with examples](https://daily.dev/posts/the-different-ways-to-use-css-has-with-examples-olvwfjtu1) · LogRocket · 10 upvotes · 0 comments
- [The CSS :has\(\) selector](https://daily.dev/posts/the-css-has-selector-csw5i3bag) · Flavio Copes · 20 upvotes · 1 comments
- [Practical CSS: Combining :has, :not, :only-child and :placeholder-shown pseudo-classes](https://daily.dev/posts/practical-css-combining-has-not-only-child-and-placeholder-shown-pseudo-classes-5gvvbff0w) · RUBYLAND · 7 upvotes · 0 comments
- [An Over-The-Top Spoiler Design with the Details Element – Frontend Masters Blog](https://daily.dev/posts/an-over-the-top-spoiler-design-with-the-details-element-frontend-masters-blog-0uqzmubnn) · Frontend Masters · 1 upvotes · 0 comments

---

Tags: [#css](https://daily.dev/tags/css), [#frontend](https://daily.dev/tags/frontend), [#dom](https://daily.dev/tags/dom)

[View this post on daily.dev](https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Can’t touch the DOM? Reach for :has() to style any element","url":"https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd"},"datePublished":"2026-08-17T00:03:10.494Z","dateModified":"2026-09-13T20:09:19.189Z","description":"When working with third-party component libraries like MUI, Ant Design, or AG Grid, developers often can't safely edit the DOM to add custom styles. The CSS...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/be9bb2f2055770952361462d41c51bfc?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/be9bb2f2055770952361462d41c51bfc?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"thoughbot","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"thoughbot","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/9fe12feb57f944a29ff63a79e350081a","url":"https://daily.dev/sources/thoughbot"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":9},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"css,frontend,dom","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"thoughbot","item":"https://daily.dev/sources/thoughbot"},{"@type":"ListItem","position":3,"name":"Can’t touch the DOM? Reach for :has() to style any element"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd#faq","mainEntity":[{"@type":"Question","name":"How can I style a parent element in CSS based on the state of its child?","acceptedAnswer":{"@type":"Answer","text":"Use the :has() pseudo-class, which lets a selector match a parent or ancestor based on whether it contains a matching descendant. For example, .input:has(.error) label turns a label red when a sibling input carries an error class. This solves cases where CSS previously could not select upward or backward through the DOM. Developers styling third-party UI components track CSS selector techniques like this on daily.dev."}},{"@type":"Question","name":"How do I style a table row based on the row that comes after it in CSS?","acceptedAnswer":{"@type":"Answer","text":"Use :has() with the next-sibling combinator, for example tr:has(+ .parent) applies a border-bottom to the row immediately before an element with the parent class. This effectively flips the usual next-sibling selector direction, something CSS could not previously do without :has(). Anyone wrangling table styling in component libraries can keep tabs on CSS tricks like this via daily.dev."}},{"@type":"Question","name":"Can I change the entire page's styling based on whether one specific component is present?","acceptedAnswer":{"@type":"Answer","text":"Yes, using body:has(.special-component) as a selector applies conditional styles to any element on the page whenever an element with that class exists anywhere in the body. Combined with CSS custom properties, this lets a library's theme variables be overridden without touching its internal settings. daily.dev helps developers theming apps around dynamic components stay current on CSS capabilities like this."}}]}
```

