The :has() selector styles elements based on their descendants. It's the long-awaited parent selector.
Basic Usage#
Form Validation Styles#
Card Layouts#
Navigation#
Checkbox and Radio Styling#
Conditional Layouts#
Sibling Styling#
Empty State Handling#
Media Queries with :has()#
Quantity Queries#
State-Based Theming#
Modal and Dialog#
Combining with Other Selectors#
Performance Considerations#
Browser Support Fallback#
Best Practices#
Usage:
✓ Style parent based on child state
✓ Create conditional layouts
✓ Form validation feedback
✓ Interactive hover effects
Performance:
✓ Use direct child selectors (>)
✓ Be specific with element types
✓ Avoid deeply nested :has()
✓ Test performance impact
Accessibility:
✓ Ensure focus states are visible
✓ Don't hide important content
✓ Maintain keyboard navigation
✓ Test with screen readers
Conclusion#
The :has() selector enables parent styling based on descendants, something CSS couldn't do before. Use it for form validation, conditional layouts, and interactive effects. Be mindful of performance with complex selectors and provide fallbacks for older browsers.