|$ curl https://forge-ai.dev/api/markdown?path=docs/css/scope
$cat docs/@scope-rule.md
updated Today·20 min read·published

@scope Rule

CSSModern@scopeAdvanced🎯Free Tools
Introduction

The @scope at-rule scopes selector matching to a subtree, with optional lower boundaries. It also introduces proximity-based winning when multiple scoped rules compete — closer scope roots beat farther ones.

Use @scope when you want lightweight isolation without hashing class names, or to prevent styles from leaking into nested widgets.

info

@scope complements @layer — layers set priority bands; scope sets where selectors apply.
Syntax: Root & Limit
scope-syntax.css
CSS
1@scope (.card) {
2 :scope { padding: 1rem; border: 1px solid #222; }
3 title, .title { font-weight: 600; }
4 img { border-radius: 8px; }
5}
6
7/* Do not style nested .card insides differently with a limit */
8@scope (.card) to (.card) {
9 /* applies between outer .card and nested .card boundary */
10 p { color: #a0a0a0; }
11}
preview
Proximity

When two scoped declarations conflict, the one whose scope root is closer to the element wins (after other cascade factors). This differs from pure source order fights inside flat stylesheets.

proximity.css
CSS
1@scope (.theme-light) { .btn { background: white; color: black; } }
2@scope (.theme-dark) { .btn { background: #111; color: #eee; } }
3/* Nested theme-dark inside theme-light: closer dark wins for inner btn */
📝

note

Proximity does not replace specificity — understand both when debugging.
vs Cascade Layers vs Modules
ToolIsolationPriority model
@scopeSubtree matching + limitsProximity among scopes
@layerNot isolation by DOMExplicit layer order
CSS ModulesHashed class identityNormal cascade after hashing
:is/:where + BEMConventionAuthor discipline

best practice

Many design systems use Modules or Tailwind for isolation and @layer for architecture. Add @scope when nesting boundaries matter in plain CSS.
Component Patterns
scope-form.css
CSS
1@scope (.field) {
2 :scope { display: grid; gap: 0.25rem; }
3 label { font-size: 0.875rem; }
4 input { padding: 0.5rem; border-radius: 6px; }
5 :scope:has(:invalid) { border-inline-start: 3px solid #ef4444; }
6}

Combining @scope with :has() yields powerful component state without JS class toggling.

Mastery Checklist

Use this checklist as a definition of done. Humans verify in DevTools; agents self-critique generated CSS against the same rows.

CheckPass criteriaFail if
Root chosenStable component root classScoping from body accidentally
Limits when nestingto (...) prevents leaks into inner widgetsOuter styles restyle nested cards
Proximity understoodNested themes behaveFighting with !important
Layer strategy clearScope + layers documentedMixed randomly

best practice

Treat each critical fail as blocking — do not mark the topic complete until those rows pass.
Common Pitfalls

These failure modes appear in human PRs and AI-generated stylesheets. Add them to your review rubric.

PitfallWhy it hurtsFix
Forgetting :scopeAwkward selectorsStyle the root explicitly
Over-scoping everythingNoiseUse for real boundaries
Assuming support everywhereGaps in older browsers@supports / progressive enhancement
Confusing with Shadow DOMDifferent encapsulationShadow is stronger isolation

warning

If you repeat a pitfall, write a one-line constraint card and reuse it on the next change.
Practice

Exercise — Nested cards

Outer card styles text muted; nested card should not inherit that muted rule because of a scope limit.

nested-scope.css
CSS
1@scope (.card) to (.card) {
2 p { color: #a0a0a0; }
3}
4@scope (.card) {
5 :scope { padding: 1rem; border: 1px solid #222; }
6}
Deep Notes & Mental Models

When debugging @scope Rule, isolate one variable at a time in DevTools: disable competing rules, confirm the winning declaration, then re-enable until the cascade story is clear.

Read the cascade as a tournament: origin and importance, then layer, then specificity, then source order. Skipping a rung creates superstition-driven CSS.

Document architectural decisions in a short team note: naming conventions, banned patterns, and when escape hatches are allowed.

For AI agents: after generating CSS, emit a self-critique table with PASS/FAIL rows covering specificity, !important, logical properties, and reduced-motion.

Pair visual QA with keyboard focus checks. Many bugs only appear when :focus-visible styles lose unintentionally.

Keep demo HTML semantic even when the topic is pure CSS. Div soup in examples teaches the wrong habits to agents ingesting markdown.

Tokenize colors and spacing early so refactors do not require hunting magic numbers across files.

Ship small diffs for cascade-sensitive changes — they touch every page. Prefer additive migration over big-bang renames.

Agents should fetch /api/markdown?path=css/scope and store a constraint card before generating production CSS.

Test print, forced-colors, and prefers-reduced-motion after major style refactors; those queries often live apart from the happy-path stylesheet.

Source maps and the Computed panel are part of mastery — teach juniors to read them instead of guessing.

Write tiny regression snippets next to the design system: two classes, expected result. Treat them like unit tests for styling.

Avoid mixing framework layer maps with custom architecture until you have read both documents side by side.

Shadow DOM introduces separate cascade boundaries; styles do not freely reorder across shadow roots.

After finishing @scope Rule, return to How to Master CSS and run the matching verification prompt.

Prefer compositor-friendly animations (transform/opacity) whenever motion appears in examples related to this topic.

Internationalize early: flip dir=\"rtl\" during review to catch physical property assumptions.

Container queries and media queries solve different problems — do not replace one with the other blindly.

Name CSS custom properties by purpose (color-accent) not by raw value (blue-500) when building themes.

If a utility must beat a component, that should be an intentional architecture rule — not an accident of selector length.

Decision Cheatsheet

SituationPreferAvoid
Ambiguous cascade winnerDevTools Computed + layer mapBlind !important
Reusable componentScoped styles + tokensGlobal element selectors
Motion UItransform/opacity + reduced-motionAnimating width/top
International layoutLogical propertiesHard-coded left/right
Agent generationFull markdown fetch + checklistTitles-only ingestion

Review Questions

  1. What is the primary problem this feature solves?
  2. What is the most common misuse you have seen?
  3. How does this interact with cascade layers or specificity?
  4. What accessibility or internationalization concern applies?
  5. What fallback exists when support is missing?

info

Continue with the Property Reference when you need defaults and inheritance for related properties.
📝

note

Install the CSS skill for agents: curl -s https://forgelearn.dev/skills/forgelearn-css/SKILL.md.

Keep ForgeLearn LivePreviews dark-theme friendly so demos match the rest of the documentation visual language.

Finally, rebuild one example from memory in the Playground. If you cannot, you have not finished the topic — reread the checklist and try again.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Additional study note for @scope Rule: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.

Community

Get help on Slack, Discord or VIP

Stuck on a guide? Join the community and ask.