@scope for Scoped Styles
Limit the reach of CSS rules to specific subtrees using the @scope at-rule.
@scope for Scoped Styles is a free CSS Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is @scope?
The @scope at-rule (Chrome 118+, Safari 17.4+) limits CSS rules to a specific subtree of the DOM. Rules inside @scope only apply to elements within the declared scope root, preventing style leakage across components.
Basic Syntax
Declare a scope with a scope root selector. All rules inside apply only to descendants of matching scope roots — not to the entire document.
@scope (.card) {
.title { font-size: 1.25rem; }
.body { padding: 1rem; }
}Scope Root
The scope root (@scope (.card)) defines the entry point. Elements inside the scope that match .title are styled. Elements outside .card with class .title are unaffected — true style encapsulation without Shadow DOM.
Scope Limit (Scope Boundary)
Optionally define a scope boundary with to: @scope (.card) to (.nested-card). Rules apply inside .card but stop at .nested-card descendants — scoping styles to one level of component nesting, excluding deeper instances.
:scope Pseudo-class Inside @scope
Inside an @scope block, :scope refers to the scope root element itself. @scope (.card) { :scope { border-radius: 8px; } } styles the .card element directly, not its descendants.
Proximity Specificity
@scope introduces proximity-based specificity: when multiple scopes match an element, the nearest ancestor scope root wins — regardless of selector specificity. This prevents deeply nested component styles from being overridden by styles from distant ancestor scopes.
@scope vs CSS Modules
CSS Modules generate unique hashed class names to achieve scope at build time — a JavaScript-bundler concern. @scope achieves scope natively in the browser without build tools, hashed names, or framework-specific syntax — pure CSS.
@scope vs Shadow DOM
Shadow DOM creates complete style isolation — external styles cannot penetrate. @scope creates partial isolation — styles from outside can still reach scoped elements if their specificity is high enough. @scope is a lighter-weight alternative for component scoping without full isolation.
Layering with @scope
Combine @scope with @layer for fine-grained cascade control: define component-scoped styles in a component layer. This manages both cascade order (via layers) and style scope (via @scope) in a composable way without class naming gymnastics.
Practical Use Cases
Design system components: scope all button styles to @scope (.button-group) so inner button styles never leak out. Widget isolation: scope a third-party widget's styles to its container, preventing conflicts with the page's global styles.
Browser Support and Fallback
@scope is supported in Chrome 118+, Safari 17.4+, Firefox 128+. For unsupported browsers, styles inside @scope are ignored — the cascade falls back to global styles. Use @scope as progressive enhancement where component scoping is beneficial but not critical.
Knowledge Check
What problem does @scope solve that is difficult to address with traditional CSS selectors alone?
Summary
@scope limits CSS rule application to a declared DOM subtree, enabling native style encapsulation without Shadow DOM or build-tool class hashing. Scope limits, proximity specificity, and integration with @layer make it a powerful primitive for component-scoped design systems in pure CSS.
Frequently asked questions
Is the “@scope for Scoped Styles” lesson free?
Yes — the full text of “@scope for Scoped Styles” is free to read here on the web, and the CSS Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO.
What will I learn in “@scope for Scoped Styles”?
Limit the reach of CSS rules to specific subtrees using the @scope at-rule. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start CSS Academy?
No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “@scope for Scoped Styles” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this CSS Academy lesson?
Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Native CSS Nesting
- :has() Relational Pseudo-class
- @scope for Scoped Styles
- View Transitions API