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

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)