Sub topic 1
Why global CSS needs scoping
CSS has one namespace for the whole page. Every rule you write can match any element in the document. That works well for small pages. As a project grows, it causes problems that this tutorial's @scope rule is designed to reduce. This chapter explains those problems. The later chapters show the fix.
Prerequisites
This chapter assumes you already know:
- Basic HTML structure and the DOM tree.
- CSS selectors and combinators, such as
.card imgandul > li. - The cascade: how specificity and order of appearance decide which rule wins.
- CSS nesting basics, such as writing
&:hoverinside a rule.
One namespace for every rule
A stylesheet does not know which component a rule was written for. The browser compares each selector against every element and applies whatever matches. Nothing in a plain selector like .title says "only inside the product card."
The diagram shows two components that share a class name. A rule written for one component also reaches into the other.
Failure mode 1: name collisions
Two authors pick the same class name for different purposes. Neither rule is wrong on its own. Together they conflict.
/* card.css */
.title {
font-size: 1.5rem;
color: navy;
}
/* banner.css, written months later */
.title {
color: white;
background: black;
}An element with class="title" now gets properties from both files. The text color depends on which file loads last, not on which component the element belongs to. Reordering <link> tags can change the page.
Failure mode 2: specificity escalation
When a rule does not win, developers raise its specificity instead of fixing the cause. Specificity is the weight the cascade gives a selector.
.title { color: navy; }
.card .title { color: navy; }
#products .card .title { color: navy; }
#products .card .title { color: navy !important; }Each line exists because the line above lost a conflict somewhere. Later rules must now beat the escalated ones. The stylesheet gets harder to override and harder to read.
Failure mode 3: leaking and fragile deletion
A broad selector such as a { color: rebeccapurple; } was written for one section. It also recolors links in the footer, in embedded widgets, and in markup added later. The effect is also hard to trace backward. When you look at a rule, you cannot easily tell which parts of the page depend on it. Teams therefore avoid deleting old CSS and let dead rules pile up.
How teams cope today
Teams have used several workarounds. Each one trades one cost for another.
| Approach | What it does | Cost |
|---|---|---|
| Naming conventions (BEM) | Long unique class names such as `card__title` | Relies on discipline, with verbose markup |
| CSS Modules | A build step rewrites class names to unique ones | Needs build tooling |
| Shadow DOM | The browser isolates styles inside a shadow tree | Requires web components and blocks most outside styling |
| Deep specificity | Selectors like `#products .card .title` | Hard to override later |
Chapter 10 compares these with @scope in detail. The point here is that each workaround adds a convention or a tool, because plain CSS has no way to say "this rule applies only inside this region."
What scoping would give you
A scoped rule states its region in the stylesheet itself. You can then:
- Reuse short names like
.titlein different components without collisions. - Keep specificity low, since the region does the narrowing instead of long selectors.
- Read a rule and know where it can have an effect.
The @scope at-rule provides this. A preview, which the next chapter explains:
@scope (.card) {
img {
width: 100%;
}
}The img rule applies only to images inside a .card element. Images elsewhere on the page are not affected.
Summary
Global CSS causes three recurring problems: name collisions, specificity escalation, and leaking or hard-to-delete rules. Conventions and build tools reduce them but do not remove their cause. @scope addresses the cause by letting a rule declare the part of the DOM it belongs to.
Exercise
A page loads card.css and then banner.css. Both define .title, with different color values. A card's <h3 class="title"> renders with the banner's color. Name the problem, explain why it happens, and say which workaround adds a build step.
Show answer
This is a name collision in the global namespace. Both rules match the same element and have equal specificity, so the later one (banner.css) wins by order of appearance. CSS Modules adds a build step, because it rewrites class names to unique ones during the build. BEM avoids the collision with naming discipline and no tooling.