DEV Community

Cover image for Unleash the Power of CSS :has() Selector: A Game-Changer for Styling Your Web Content
Matt Adil
Matt Adil

Posted on

2 1

Unleash the Power of CSS :has() Selector: A Game-Changer for Styling Your Web Content

In the ever-evolving landscape of web development, staying ahead of the curve is crucial. Enter the CSS :has() pseudo-class, a revolutionary addition to the developer's toolkit, offering unprecedented control over element selection and styling within the Document Object Model (DOM). Unlike its predecessors, which focused primarily on direct hierarchy or attributes, :has() empowers developers to style elements based on the presence of specific descendants, children, or subsequent siblings. Let's delve into the transformative potential of this remarkable feature.

Understanding the :has() Pseudo-class

Before we dive into practical examples, let's grasp the concept visually. Imagine the DOM as a tree, with elements nested like branches. Traditional CSS selectors allow us to traverse down this tree, selecting child elements based on their parent's characteristics. However, the :has() selector flips the script, enabling us to select parent elements based on the presence or attributes of their descendants. This opens up a world of possibilities for dynamic styling and interaction.

What :has() Unlocks: New Frontiers in CSS Styling

Elevate Design: Styling Parent Elements via Child Presence

Consider a scenario where you have a list of articles. With :has(), you can dynamically style the parent <div> containing each article based on whether it contains an image. Previously achievable only through JavaScript, this feature simplifies the process and enhances user experience.

<!-- html -->
<div class="article-container">
  <h2>Article Title 1</h2>
  <p>Article content...</p>
  <img src="article-image.jpg" alt="Article Image">
</div>

<div class="article-container">
  <h2>Article Title 2</h2>
  <p>Article content...</p>
</div>
Enter fullscreen mode Exit fullscreen mode
/* css */
/* Style the parent container if it contains an image */
.article-container:has(img) {
  background-color: lightblue;
}

/* Style the parent container if it does not contain an image */
.article-container:not(:has(img)) {
  background-color: lightgray;
}
Enter fullscreen mode Exit fullscreen mode

Precision Styling: Targeting Elements by Content Criteria

In another scenario, let's say you have a blog and want to style article titles based on keywords. Using :has(), you can achieve this directly in CSS, enhancing the visual presentation of your content.

<!-- html -->
<div class="article">
  <h2 class="title">Introduction to CSS Grid</h2>
  <p>Article content...</p>
</div>

<div class="article">
  <h2 class="title">10 Tips for Responsive Web Design</h2>
  <p>Article content...</p>
</div>

<div class="article">
  <h2 class="title">Mastering Flexbox for Layouts</h2>
  <p>Article content...</p>
</div>
Enter fullscreen mode Exit fullscreen mode
/* css */
/* Style the title of articles containing specific keywords */
.article:has(.title:contains("CSS Grid")) .title {
  color: #ff5733; /* Orange color for emphasis */
}
Enter fullscreen mode Exit fullscreen mode

Refined Design: Tailoring Styles to Intricate Element Relationships

In a social media scenario, imagine you want to differentiate comment authors based on replies. With :has(), this becomes straightforward, offering visual emphasis to encourage further interaction.

<!-- html -->
<div class="comment">
  <p class="author">John Doe</p>
  <p class="content">This is the main comment.</p>
</div>

<div class="comment">
  <p class="author">Jane Smith</p>
  <p class="content">This is another comment.</p>
  <div class="replies">
    <div class="reply">
      <p class="author">Alex Johnson</p>
      <p class="content">Reply to Jane's comment.</p>
    </div>
    <div class="reply">
      <p class="author">Emily Brown</p>
      <p class="content">Another reply to Jane's comment.</p>
    </div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode
/* css */
/* Style the author of comments with replies */
.comment:has(.replies) .author {
  font-weight: bold; /* Bold font weight for authors with replies */
}
Enter fullscreen mode Exit fullscreen mode

Harnessing the Power of :has()

The possibilities with :has() are limitless. Whether enhancing navigation menus, elevating form interactivity, or crafting dynamic grids and galleries, this pseudo-class empowers developers to create immersive web experiences solely through CSS. Embrace its versatility, experiment with its capabilities, and unlock a new realm of creativity in web development.

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️