Parental Control: CSS :has() Selector for Dynamic Parent Styling
Prompt
Create an interactive demo showcasing the CSS :has() selector to style parent elements based on their children. Include examples like highlighting a parent when its child is hovered, toggling a container style when a checkbox inside is checked, and showing hidden siblings when a specific child exists. Use clean HTML, CSS, and minimal JS for toggle interactions.
CODE PREVIEW
AIとの対話に追加する一言
プロンプトと一緒にコピーして、AIがより正確に意図を汲み取れるようにします
このコードをそのまま使いたいので、CDNリンクも含めた完全なHTMLファイルとして出力してください。
初心者なので、各行にコメントを追加して、何をしているか説明してください。
このコードをWordPressのテーマに組み込む方法も教えてください。
動作確認済みのコードをお願いします。エラーが出る場合の対処法も含めて。
CSSの:has()セレクタを使うと、子要素の状態に応じて親要素をスタイリングできます。デモでは3つの例を用意。「Hover over me」をホバーすると親カードが緑に光る。チェックボックスをオンにすると隠れていた兄弟要素が現れ、背景も変化。3つ目のカードは「魔法のボタン」を持っているだけで自動で紫の枠線が付きます。これらはすべてCSSのみ。開発者ツールでセレクタを確認してみてください。
Prompt Overview
Did you know CSS can now style a parent based on its children? The :has() selector is a game-changer. Hover over the 'Hover over me' text — the parent card glows green. Check the checkbox inside the first card — a hidden sibling appears and the background changes. The third card automatically gets a purple border because it has a magic button. No JavaScript needed for these conditional styles. Open developer tools and inspect.
Was this prompt helpful?
Comments
Comments appear after moderation
この記事が役に立ったら投げ銭で応援
Apple Pay / Google Pay / カード (Visa/Mastercard/JCB/Amex) / Link / Alipay / WeChat Pay 対応 · Stripeで安全に決済
開発者が選ぶ最強ツール集
運営者が毎日使っているツール・ガジェット 6選