本文へスキップ
Skillpedia
WebサイトとUI(フロントエンド)75k · #2865

has-selector

できること

このスキルは、子の内容によって親のスタイルを変えるために JavaScript を使っているコードを探します。よりシンプルで CSS によるスタイルを作りたい開発者向けです。

使う場面

  • ✓コンテナの中の入力が変わるたびに、スクリプトがコンテナのクラスを追加しているとき。
  • ✓子の内容に応じて親をスタイルするためだけに、JavaScript を書いているとき。
  • ✓同期を保つのが難しいスタイルのコードを減らしたいとき。

スキルのリンク

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/has-selector/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/has-selector/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“子の要素が変わったとき、JavaScript の代わりに CSS で親をスタイルできますか。”

毎週5つの新しいスキル。1分で読めます。

仕組み

エージェントは、親のクラスを切り替える JavaScript を見つけ、子の条件を満たしたときに親を対象にする :has のセレクターを提案します。また、:has が :is や :not と、どう違うかも説明します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

Use :has() to style parent elements based on their descendants

Before :has(), styling a parent based on its children required JavaScript: detecting state, toggling classes, and keeping DOM and styles in sync. This created coupling between styling logic and application logic. :has() moves that relationship back into CSS where it belongs, reducing JavaScript complexity, eliminating class-toggling boilerplate, and making styling intent readable in the stylesheet itself.

Quick Reference

  • :has() selects an element when a matching descendant exists inside it
  • It is the parent selector CSS has needed for decades
  • Baseline support since late 2023 — all major browsers include it
  • Avoid complex :has() selectors in hot reflow paths — they can affect layout performance

Check

Look for patterns in this code where JavaScript toggles a class on a parent element based on the state or content of a child element. These are candidates for replacement with :has().

Fix

Replace the JavaScript class-toggling logic with a CSS :has() selector. Show the selector that targets the parent element when the relevant child condition is met.

Explain

Explain how the :has() pseudo-class works, how it differs from :is() and :not(), what descendant vs child combinators mean inside :has(), and how browser support stands today.

Code Review

Review the stylesheets and component JavaScript in this file. Flag any locations where a parent element's style is controlled by toggling a class from JavaScript based on child state — and show the equivalent :has() rule.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/css/has-selector

よくある質問

:has は何をしますか?

要素の中に一致する子や子孫があるときに、その要素を選択します。以前は CSS でできなかったことです。

広くサポートされていますか?

2023年後半から、すべての主要なブラウザでベースラインのサポートがあるとこのスキルは説明しています。

ページが遅くなることはありますか?

頻繁に再計算されるレイアウトの部分では、複雑な :has セレクターを避けるよう警告しています。

詳細

作者による元の説明

Use when reviewing stylesheets or components that use JavaScript to toggle classes on parent elements based on child state, form input values, or content presence — :has() may replace the JavaScript entirely.

repo: thedaviddias/Front-End-Checklist

人気

GitHubのスター
75k
ランキング
#2865
ライセンス
MIT
更新日
2026/10/31

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル