惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
爱范儿
爱范儿
博客园 - Franky
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
有赞技术团队
有赞技术团队
美团技术团队
Last Week in AI
Last Week in AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
J
Java Code Geeks
Martin Fowler
Martin Fowler
I
InfoQ
小众软件
小众软件
MongoDB | Blog
MongoDB | Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Advanced Use of the :has() Pseudo-Class for Creating UI L...
Nick Benksim · 2026-05-01 · via DEV Community

Nick Benksim

The Evolution of CSS: Mastering the :has() Pseudo-Class

The introduction of the :has() pseudo-class is perhaps the most significant change in CSS architecture in the last decade. Often referred to as the “parent selector,” it allows developers to style an element based on its children or even its preceding siblings. This capability transforms how we handle UI logic, moving many responsibilities from JavaScript back to pure CSS and making our codebases more maintainable.

Dynamic Form Validation Styling

One of the most practical applications of :has() is creating reactive forms without a single line of JavaScript. Traditionally, to style a form fieldset or a label based on whether an input was invalid, you needed to toggle classes via script. Now, you can target the container directly. For instance, you can apply a red border and a shake animation to a fieldset only if it contains an input with an :invalid state that is also not focused.

While exploring these modern capabilities, it is worth noting that :has() works exceptionally well alongside Style Queries in CSS, which allow you to style elements based on the computed values of their parents, creating a robust logic system for your components without relying on class-heavy frameworks.

Context-Aware Layout Adjustments

Using :has(), we can create truly context-aware layouts. Imagine a card component that changes its grid structure only if it contains an image. Previously, this required a specific modifier class like “card–with-image”. With the new pseudo-class, the CSS can detect the presence of the img tag and update the grid-template-areas automatically. This reduces the reliance on manual class management in your CMS or backend logic.

Common use cases for layout logic include:

  • Sidebars: Expanding a sidebar width only if it contains a specific “active” navigation element.
  • Grids: Changing the number of columns in a gallery if one of the items is marked as “featured” using a child attribute.
  • Empty States: Hiding a section header if the following container is empty or has no visible children.

Interactive Navigation and State Management

Creating “mega menus” or complex navigation bars becomes significantly easier. You can style the main navigation background or dim the rest of the page when a specific sub-menu is hovered or focused. By using nav:has(.submenu:hover), the parent nav element can change its appearance to accommodate the expanded content, providing a smoother user experience.

This level of control is a staple in the top 5 modern CSS features for frontend developers, as it simplifies the DOM structure by removing the need for “state” classes like “is-active” or “is-open” in many common interaction scenarios.

Advanced Logical Combinations

The true power of :has() emerges when combined with other pseudo-classes like :not() and :checked. You can create complex logic gates within your stylesheets. For example, you can style a dashboard widget differently only if it has a header but does not have a footer. Or, you can style a “Select All” label based on whether all individual checkboxes within a group are checked.

Example logic: .container:has(input[type=”checkbox”]:not(:checked)). This selector targets a container only if it contains at least one checkbox that is not checked, allowing you to visually indicate an incomplete task list at the container level.

Performance and Browser Support

While :has() is incredibly powerful, it is important to use it judiciously. Because the browser must check the descendants of an element to determine its style, extremely deep nesting or overly broad selectors (like body:has(*)) can theoretically impact rendering performance. However, in modern browser engines, the implementation is highly optimized, and for standard UI components, the performance cost is negligible compared to the overhead of JavaScript-based DOM manipulation.

As of 2024 and 2025, :has() is supported in all major evergreen browsers, making it a production-ready tool for developers looking to write cleaner, more declarative CSS.

We publish more advanced CSS tricks, ready-to-use snippets, and tutorials in our Telegram channel. Subscribe!