CSS Grid: The Complete Guide
Everything you need to know about CSS Grid in one comprehensive guide.
Search all CSS Showcase pages by name
Finally, a parent selector in CSS! Style elements based on what they contain. It's like magic, but better — it's native CSS.
The :has() pseudo-class lets you select an element if it contains another element that matches a selector. Think of it as “select a parent that has a specific child” — something we've been dreaming about for years!
// JavaScript required!
const cards = document.querySelectorAll('.card');
cards.forEach(card => {
if (card.querySelector('.badge')) {
card.classList.add('has-badge');
}
});/* Pure CSS! */
.card:has(.badge) {
border: 2px solid gold;
background: linear-gradient(
to right,
rgba(255, 215, 0, 0.1),
transparent
);
}These demos use real :has() selectors in CSS — no JavaScript involved. Interact with the elements to see the selectors in action.
Master modern CSS with this brilliant course
Start your journey with the fundamentals
Take your skills to the next level
Everything you need to know about CSS Grid in one comprehensive guide.
Quick video tutorial on mastering Flexbox layouts.
Discover how to create stunning visuals using only CSS.
Combine :has() with other selectors for powerful patterns that would have required JavaScript before.
The :has() selector enjoys broad support across modern browsers. Always provide base styles for graceful degradation.
Keep these guidelines in mind when using :has() in production.
Keep :has() selectors simple. Complex selectors can impact performance, especially with many elements.
:has() doesn't increase specificity by itself — it's the selectors inside that count.
Always test in browsers that don't support :has() to ensure graceful degradation.