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

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 【当耐特】
A
About on SuperTechFans
Last Week in AI
Last Week in AI
雷峰网
雷峰网
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
博客园 - 叶小钗
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
The Cloudflare Blog
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Recent Announcements
Recent Announcements

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!