<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>CSS :Has() - 标签 - 技术手册</title><link>https://manual.toulan.fun/tags/css-has/</link><description>CSS :Has() - 标签 - 技术手册</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><lastBuildDate>Fri, 24 Nov 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://manual.toulan.fun/tags/css-has/" rel="self" type="application/rss+xml"/><item><title>CSS :has() Parent Selector and Relational Queries</title><link>https://manual.toulan.fun/en/posts/css-has-parent-selector/</link><pubDate>Fri, 24 Nov 2023 00:00:00 +0000</pubDate><author><name>HenryZ</name><uri>https://nodewee.github.io/</uri><email>nodewee@gmail.com</email></author><guid>https://manual.toulan.fun/en/posts/css-has-parent-selector/</guid><description><![CDATA[<p>Hi, I&rsquo;m HenryZ 👋.</p>
<p>In frontend development, we often encounter these requirements:</p>
<ul>
<li><strong>Parent element styles depending on child element states</strong> — Such as navigation bars where parent <code>&lt;li&gt;</code> needs to highlight based on whether child <code>&lt;a&gt;</code> has <code>.active</code>.</li>
<li><strong>Layout changes with content</strong> — For example, CSS Grid containers switching column numbers when child elements exceed a threshold.</li>
<li><strong>Interaction-driven parent styles</strong> — During form validation, parent <code>&lt;form&gt;</code> changes state based on the validity of internal <code>&lt;input&gt;</code> elements.</li>
</ul>
<p>Previously, these scenarios could only rely on <strong>JavaScript</strong>: traversing the DOM to query child elements, then manually adding classes to parent elements. This not only increased code volume but also scattered logic between CSS and JS, reducing maintainability.</p>]]></description></item><item><title>CSS :has() 父级选择器与关系查询</title><link>https://manual.toulan.fun/css-has-parent-selector/</link><pubDate>Fri, 24 Nov 2023 00:00:00 +0000</pubDate><author><name>HenryZ</name><uri>https://nodewee.github.io/</uri><email>nodewee@gmail.com</email></author><guid>https://manual.toulan.fun/css-has-parent-selector/</guid><description><![CDATA[<p>嗨，我是芦苇Z 👋。</p>
<p>在前端开发中，我们经常会遇到以下需求：</p>
<ul>
<li><strong>父元素样式依赖子元素状态</strong> —— 如导航栏中，父级 <code>&lt;li&gt;</code> 需根据子 <code>&lt;a&gt;</code> 是否 <code>.active</code> 来高亮。</li>
<li><strong>布局随内容变化</strong> —— 比如 CSS Grid 容器在子元素数量超过阈值时切换列数。</li>
<li><strong>交互驱动父级样式</strong> —— 表单验证时，父级 <code>&lt;form&gt;</code> 根据内部 <code>&lt;input&gt;</code> 的合法性改变状态。</li>
</ul>
<p>过去，这类场景往往只能靠 <strong>JavaScript</strong>：通过 DOM 遍历查询子元素，再手动给父元素加 class。这样不仅增加代码量，也让逻辑分散在 CSS 与 JS 之间，降低可维护性。</p>]]></description></item></channel></rss>