Tiny Frontend Logo
Quizes 1145

On This Page

Quiz 1145: CSS Selectors

How do the :has() and :is() selectors simplify complex CSS selections?

Quick Answer

:has() and :is() are powerful CSS selectors that make it easier to target elements based on their descendants or by matching multiple simple selectors, respectively. They allow you to write cleaner and more readable code, especially when dealing with complex relationships within your HTML structure.

Explain in Depth

:has() Selector:

The :has() selector lets you target an element if it contains a specific descendant element that matches a given selector. This simplifies targeting elements based on their content or structure, without needing to chain multiple selectors.

Example using tinyfrontend.com:

Let's say on tinyfrontend.com, we want to style all blog posts that have an image inside.

HTML (tinyfrontend.com/index.html):

<div class="post">
  <h2>Blog Post Title</h2>
  <img src="image.jpg" alt="Blog Post Image">
  <p>This is a blog post with an image.</p>
</div>
<div class="post">
  <h2>Another Blog Post</h2>
  <p>This blog post doesn't have an image.</p>
</div>

CSS (tinyfrontend.com/styles.css):

.post:has(img) {
  background-color: #f0f0f0;
}

In this example, the :has(img) selector targets all .post elements that contain an img element. This makes it much cleaner than using a more complex selector like .post > img.

:is() Selector:

The :is() selector allows you to combine multiple simple selectors into a single group. This helps reduce redundancy and makes your CSS code more readable, especially when you need to target elements with different classes or types.

Example using tinyfrontend.com:

Let's say we want to style all elements that have the class button or are an a tag.

HTML (tinyfrontend.com/index.html):

<button class="button">Click Me</button>
<a href="#">Link</a>

CSS (tinyfrontend.com/styles.css):

:is(.button, a) {
  color: #fff;
  background-color: #333;
}

In this example, the :is(.button, a) selector targets all elements that are either a .button or an a tag. This is much more concise than using two separate rules for each selector.

Benefits of :has() and :is():

  • Improved Readability: Cleaner and more understandable CSS rules.
  • Reduced Redundancy: Avoid repeating the same selectors multiple times.
  • Simplified Targeting: Effortlessly target elements based on complex relationships or multiple conditions.

:has() and :is() are valuable tools in your CSS arsenal, helping you write more efficient and maintainable code while improving the readability and overall quality of your stylesheets.

Read Next

Quiz 1156: React Portals

What are React Portals, and why are they so powerful for managing UI complexity?

What is a Prototype Chain


In JavaScript, a prototype chain is a fundamental concept that enables inheritance between objects. It's a way for an object to inherit properties and behavior from another object, allowing for code reuse and modularity.

Quiz 1154: Memoization in React

Explain the purpose of React.memo and useMemo in React, and how they contribute to performance optimization.

Quiz 1154: Memoization in React

Explain the purpose of React.memo and useMemo in React, and how they contribute to performance optimization.

Quiz 1153: CSS Containment contain

Describe the concept of CSS Containment and explain how it can enhance performance in web applications.

Quiz 1152: React State vs Props

What's the difference between state and props in React?