Scopped CSS
Scopped CSS has been a hot topic in front-end development. It is designed to give you tighter control over your styles and how they interact with each other. ## The @scope at-rule The `@scope` root at-rule declares a scoping root and optional scoping limits associated with a set of style rules. Using the `@scope` at-rule we can specify: * Rules that apply to all elements in the scope using the `:scope` seelector * Rules that apply to specific elements in the scope that will override the default rules This example defines a scope of `.card`. All elements with a the class will be in scope. Inside the `@scope (.card)` at rule we define the rules for all the elements in scope inside the `:scope` slector. We also define a title element that will override the defaults and add its own styles. ```css @scope (.card) { :scope { padding: 1rem; background-color: white; font-family: Raleway, sans-serif; } .title { font-size: 1.2rem; font-family: Georgia, serif; } } ``` ### Inner scope boundaries Scoped styles won’t target anything inside elements with `slot` class. This gives us a way to bypass the scoped styles. ```css @scope (.card) { /* Scope the following styles to inside `.card` */ :scope { padding: 1rem; background-color: white; } .title { font-size: 1.2rem; font-family: Georgia, serif; } } ``` Using the styles above and the HTML below. The elements inside the `slot` class element will not get the default styles. ```html
Moon lander
Content here is not affected by scopped style.
I’m green
I’m blue
I’m blue
But I’m green