brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: @scope limits styles to one part of the page

Web-Development
~/tips/tip-css-scope-at-rule

@scope (.card) to (.card-footer) {

Applies CSS rules only inside one element, and optionally stops at an inner boundary.

Naming conventions like BEM exist because CSS is global. @scope is the first feature that makes me think I could stop using them.

MDN describes @scope as selecting elements in specific DOM subtrees without overly specific selectors. You give a scope root and, optionally, a limit where the scope stops:

@scope (.card) to (.card-footer) {
  img { border-radius: 8px; }
  a { color: darkgreen; }
}

The img and a rules apply inside .card but not inside its footer, so you no longer write .card img and .card a everywhere. By default the root is included in the scope and the limit is excluded. Inside a <style> element placed in the card itself you can leave the prelude out, and the parent element becomes the scope root:

<section class="card">
  <style>
    @scope { img { border-radius: 8px; } }
  </style>
</section>

Inside the block, :scope refers to the root itself, which is how you style the card element.

Caveats: MDN lists @scope as newly available since March 2026, so check your audience if you must support older browsers. It scopes selectors, not inheritance, so properties like color and font-family still flow past the limit into the footer. And scoped rules add no specificity of their own, so when two scopes compete, the root that is closer to the element in the DOM wins. Importance, layers and specificity still come first.

Further reading: MDN: @scope

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link