<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n" -->

---
title: Practical uses of the :has() relational pseudo class
description: An exploration of practical applications for the CSS :has() relational pseudo class, which had landed in Chrome and Safari. Examples include using :has() as a...
canonical: https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Practical uses of the :has() relational pseudo class | daily.dev
og:description: An exploration of practical applications for the CSS :has() relational pseudo class, which had landed in Chrome and Safari. Examples include using :has() as a...
og:url: https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n
og:image: https://api.daily.dev/og/posts/HoOWLKw7N.png
og:image:alt: Practical uses of the :has() relational pseudo class
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.

# Practical uses of the :has() relational pseudo class

**[utilitybend](https://daily.dev/sources/utilitybend)** · 6 min read · 0 upvotes · 0 comments

## Summary

An exploration of practical applications for the CSS :has() relational pseudo class, which had landed in Chrome and Safari. Examples include using :has() as a parent selector to style grid items based on whether they contain an image, combining :has() with :not() to change layout when no images are present, styling list hover states without extra pseudo-elements, using :has(:invalid) and :has(:placeholder-shown) for CSS-only form validation feedback, and creating a striped table pattern based on row count using :has() with :nth-child.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://utilitybend.com/blog/practical-uses-of-the-has-relational-pseudo-class>

## Questions this post answers

### How can I style a form field's error message using only CSS when the input is invalid?

Use the :has() pseudo class on a parent element to check for the :invalid state, like fieldset:has(:invalid) .feedback { opacity: 1; }. To avoid triggering the error on an empty required field, add fieldset:has(:placeholder-shown) .feedback { opacity: 0; } so the message stays hidden until the user actually types invalid text.

_Developers wiring up CSS-only form validation can find more :has() patterns like this on daily.dev._

### How do I style a parent element differently depending on whether it contains a specific child, like an image, using CSS?

Use the :has() relational pseudo class, for example article:has(figure) { } to target an article element that contains a figure. This avoids manually adding classes like has-image via a templating language, reducing inconsistencies across a codebase, and can be combined with :not() as article:not(:has(figure)) to target elements that lack that child.

_Anyone cleaning up templating-driven CSS classes can track :has() techniques like this via daily.dev._

### Can I create a striped table pattern in CSS only when the table has more than a certain number of rows?

Yes, combine the :has() pseudo class with :nth-child to conditionally apply striping. For example, table:has(tr:nth-child(5)) tr:nth-child(odd) { background: hotpink; color: #FFF; } applies odd-row striping only once the table has at least 5 rows, keeping the layout decision in CSS instead of templating logic.

_Front-end developers exploring conditional CSS layouts can follow more :has() tricks on daily.dev._

## Similar posts on daily.dev

- [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
- [Can’t touch the DOM? Reach for :has\(\) to style any element](https://daily.dev/posts/can-t-touch-the-dom-reach-for-has-to-style-any-element-7yalpjtnd) · thoughbot · 9 upvotes · 0 comments
- [Chris’ Corner: Lovingly Esoteric CSS](https://daily.dev/posts/chris-corner-lovingly-esoteric-css-znejy2bhb) · CodePen · 20 upvotes · 0 comments

---

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

[View this post on daily.dev](https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n)

```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":"Practical uses of the :has() relational pseudo class","url":"https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n"},"datePublished":"2026-09-20T07:43:20.368Z","dateModified":"2026-09-20T08:14:04.834Z","description":"An exploration of practical applications for the CSS :has() relational pseudo class, which had landed in Chrome and Safari. Examples include using :has() as a...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/86628e6d7d4c13ce024e8dae8a97877a?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/86628e6d7d4c13ce024e8dae8a97877a?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"utilitybend","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":"utilitybend","logo":"https://media.daily.dev/image/upload/s--hONVFSz4--/f_auto,q_auto/v1789890196/logos/utilitybend?_a=BAMAMicg0","url":"https://daily.dev/sources/utilitybend"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"webdev,css","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"utilitybend","item":"https://daily.dev/sources/utilitybend"},{"@type":"ListItem","position":3,"name":"Practical uses of the :has() relational pseudo class"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/practical-uses-of-the-has-relational-pseudo-class-hoowlkw7n#faq","mainEntity":[{"@type":"Question","name":"How can I style a form field's error message using only CSS when the input is invalid?","acceptedAnswer":{"@type":"Answer","text":"Use the :has() pseudo class on a parent element to check for the :invalid state, like fieldset:has(:invalid) .feedback { opacity: 1; }. To avoid triggering the error on an empty required field, add fieldset:has(:placeholder-shown) .feedback { opacity: 0; } so the message stays hidden until the user actually types invalid text. Developers wiring up CSS-only form validation can find more :has() patterns like this on daily.dev."}},{"@type":"Question","name":"How do I style a parent element differently depending on whether it contains a specific child, like an image, using CSS?","acceptedAnswer":{"@type":"Answer","text":"Use the :has() relational pseudo class, for example article:has(figure) { } to target an article element that contains a figure. This avoids manually adding classes like has-image via a templating language, reducing inconsistencies across a codebase, and can be combined with :not() as article:not(:has(figure)) to target elements that lack that child. Anyone cleaning up templating-driven CSS classes can track :has() techniques like this via daily.dev."}},{"@type":"Question","name":"Can I create a striped table pattern in CSS only when the table has more than a certain number of rows?","acceptedAnswer":{"@type":"Answer","text":"Yes, combine the :has() pseudo class with :nth-child to conditionally apply striping. For example, table:has(tr:nth-child(5)) tr:nth-child(odd) { background: hotpink; color: #FFF; } applies odd-row striping only once the table has at least 5 rows, keeping the layout decision in CSS instead of templating logic. Front-end developers exploring conditional CSS layouts can follow more :has() tricks on daily.dev."}}]}
```

