{"data":{"slug":"github-thedaviddias-front-end-checklist-skills-has-selector-5dc874b94648","title":"Replace JavaScript Class Toggling with :has() Selector","summary":"Use the :has() CSS selector to style parent elements based on the presence of specific descendants, eliminating the need for JavaScript class toggling and reducing coupling between styling and application logic.","url":"https:\/\/skillshunter.net\/skills\/github-thedaviddias-front-end-checklist-skills-has-selector-5dc874b94648","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-thedaviddias-front-end-checklist-skills-has-selector-5dc874b94648","markdown_url":"https:\/\/skillshunter.net\/skills\/github-thedaviddias-front-end-checklist-skills-has-selector-5dc874b94648.md","download_url":"https:\/\/skillshunter.net\/skills\/github-thedaviddias-front-end-checklist-skills-has-selector-5dc874b94648\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Ensure browser support is checked before implementing :has(), as baseline support began in late 2023. Avoid using complex :has() selectors in hot reflow paths to prevent layout performance issues.","instructions":"---\nname: has-selector\ndescription: \"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 \u2014 :has() may replace the JavaScript entirely.\"\nmetadata:\n  category: css\n  priority: low\n  difficulty: intermediate\n  estimatedTime: \"20\"\n  source: frontendchecklist.io\n  url: https:\/\/frontendchecklist.io\/rules\/css\/has-selector\n---\n\n# Use :has() to style parent elements based on their descendants\n\nBefore :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.\n\n## Quick Reference\n\n- :has() selects an element when a matching descendant exists inside it\n- It is the parent selector CSS has needed for decades\n- Baseline support since late 2023 \u2014 all major browsers include it\n- Avoid complex :has() selectors in hot reflow paths \u2014 they can affect layout performance\n\n## Check\n\nLook 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().\n\n## Fix\n\nReplace 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.\n\n## Explain\n\nExplain 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.\n\n## Code Review\n\nReview 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 \u2014 and show the equivalent :has() rule.\n\n---\n\nFor full implementation details, code examples, and framework-specific guidance,\nsee `references\/rule.md`.\n\nRule page: https:\/\/frontendchecklist.io\/rules\/css\/has-selector\n","use_cases":["Replace JavaScript logic that toggles a class on a parent element based on the presence of a child element.","Style a parent element when a specific form input value is present in a child element.","Update styles dynamically based on content presence in a child element without JavaScript."],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/thedaviddias\/Front-End-Checklist\/blob\/e8d14d049010e2ce25afcd5e8609dfcbbe63e47e\/skills\/has-selector\/SKILL.md","source_commit":"e8d14d049010e2ce25afcd5e8609dfcbbe63e47e","license":null,"version":null,"listing_approved_at":"2026-11-23","created_at":"2026-11-22T16:21:05.000000Z","updated_at":"2026-11-23T04:55:09.000000Z","seo_title":"Replace JavaScript Class Toggling with :has() Selector","seo_description":"Use the :has() CSS selector to style parent elements based on their descendants, eliminating the need for JavaScript class toggling.","seo_keywords":["CSS :has() selector","Replace JavaScript class toggling","Parent element styling with :has()","CSS selector for descendant state"]}}