CSS @scope Generator
Write component styles that stay inside the component. The CSS @scope rule limits styles to part of the page: everything inside a scope root such as .card, optionally stopping at a scope limit such as a content slot, so you can use short selectors like p and a without them leaking out or reaching into nested content.
Choose a preset or enter your own root and limit, add your rules, and the live preview outlines the scope root and limit so you can see exactly which elements are styled. Each rule shows its specificity, and you can paste your own HTML to test on. Copy the CSS, or switch to Inline <style> to scope styles to the element that contains them.
Preview
Your browser doesn’t support @scope yet, so the preview can’t show the scoping. The generated CSS is still correct.
About this tool
How @scope works
The scope root is the element the styles start from, and the optional scope limit, after to, is where they stop. Together they make a “donut”: the styles reach everything inside the root except the limit and what’s inside it.
@scope (.card) to (.card__content) {
:scope { padding: 1rem; } /* the card itself */
p { color: #475569; } /* paragraphs in the card */
}
Inside the block, :scope matches the root itself and & repeats the root selector. Any other selector only matches elements inside the scope.
Why use it instead of nesting?
- Limits:
.card palso styles paragraphs in any content placed inside the card. A scope limit stops at the slot. - Low specificity:
pinside@scope (.card)has the specificity ofp(0,0,1), not.card p(0,1,1), so it’s easy to override. Only:scope(0,1,0) and&add to it. - Proximity: when two scopes style the same element with equal specificity, the one whose root is closest wins, whatever the order in the stylesheet.
Closest scope wins
Proximity is what makes nested themes work. A link inside .dark inside .light gets the dark colour, because .dark is the nearer root:
@scope (.light) { a { color: #1d4ed8; } }
@scope (.dark) { a { color: #93c5fd; } }
Without @scope, .light a and .dark a have the same specificity, so whichever comes last wins.
Browser support
@scope works in Chrome and Edge 118, Safari 17.4 and Firefox 146 or later, so every current major browser supports it. In older browsers the whole @scope block is ignored, so test that a page still works without the scoped styles, or keep the critical ones outside it.
Frequently Asked Questions
What is CSS @scope?
@scope is a CSS at-rule that applies styles only to part of the page. You give it a scope root, such as .card, and optionally a scope limit after to, and the rules inside only match elements within the root and outside the limit.
What is a scope limit, or donut scope?
A scope limit is where the styles stop. In @scope (.card) to (.card__content), the styles reach everything in the card except the content slot and what’s inside it. The styled area has a hole in the middle, which is why it’s called donut scope.
What does :scope mean inside @scope?
:scope matches the scope root itself, so :scope { padding: 1rem; } styles the card, not its contents. Use :scope > li to match direct children only. & also refers to the root, but repeats the root selector, so it has the root selector’s specificity.
Does @scope change specificity?
Only a little. Selectors inside @scope keep their own specificity, and the scope root doesn’t add to it, so p has a specificity of 0,0,1 instead of 0,1,1 for .card p. Using :scope adds 0,1,0, and & adds the specificity of the root selector. The generator shows the specificity of each rule.
What is scoping proximity?
When rules from two scopes match the same element with the same specificity, the rule from the scope whose root is closest to the element wins, even if the other rule comes later. This makes nested themes, such as a dark panel inside a light page, work as you’d expect.
How do I use @scope in an inline style element?
Put a <style> element inside the element you want to style and write @scope { … } with no selector. The parent of the <style> becomes the scope root, so the styles only apply to that element and its contents. Choose Inline <style> in the generator to get this code.
Is @scope the same as Shadow DOM?
No. Shadow DOM fully isolates a component, so outside styles can’t reach in and inside styles can’t leak out. @scope only limits where your selectors match: global styles still apply inside the scope, and you don’t need JavaScript or web components to use it.
Which browsers support @scope?
Chrome and Edge from version 118, Safari from 17.4 and Firefox from 146. Browsers that don’t support it ignore the whole @scope block.
Can I test @scope on my own HTML?
Yes. Open the Demo HTML section, paste your markup and set the scope root and limit to match it. The preview outlines every scope root in blue and every limit in orange. Scripts and event attributes are removed from pasted HTML, and nothing is uploaded.