Early Days for CSS Scoping

There is a working draft spec for CSS scoping now, a newsworthy event for the W3C. Other than a weird period where <style scoped shipped and then was subsequently removed from the spec (and browsers), this is the furthest a …


The post Early Days for CSS Scoping appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier

There is a working draft spec for CSS scoping now, a newsworthy event for the W3C. Other than a weird period where <style scoped> shipped and then was subsequently removed from the spec (and browsers), this is the furthest a scoping proposal has gotten (the Level 1 spec never got anywhere).

This one comes from Miriam Suzanne.

The basics:

<div class="media">
  <img alt="Proper alt." src="...">
  <div class="content">
    <p>...</p>
  </div>
</div>

If I’m thinking of this bit of HTML as a “component”, it’s nice to be able to write styles for it that are very explicitly just for it. That’s what @scope is for, so…

@scope (.media) {
  :scope {
    display: grid;
    grid-template-columns: 50px 1fr;
  }
  img {
    filter: grayscale(100%);
    border-radius: 50%;
  }
  .content { ... }
}

What I like about that is:

  1. This bit of CSS is very explicitly for this media component. It reads like that and can be maintained like that.
  2. I didn’t have to come up with a name and class for the <img>. I’m applying styling there without it “leaking out” to other images.

But wait, isn’t this just like prepending selectors with the parent class?

It kind of is… like we could also write:

.media {
}
.media img {
}
.media content {
}

And now we’ve scoped things internal to the media component. That’s rather repetitive, but with native CSS nesting on the way, it’s just this:

.media {
  img {
  }
  .content {
  }
}

So yes, I’d say nesting takes care of some basic types of scoping, but there are some things that are very unique to this new scope proposal.

One unique feature is “donut scope”, meaning I stop the scoping where I want to. Maybe I want my scoping to stop at a particular class:

@scope (.media) to (.content) {
  p { }
}

Now I can write styles that won’t mess with areas that I don’t want them to mess with, perhaps:

<div class="media">
  <img alt="Proper alt." src="...">
  <p>This is stylable in scope.</p>
  <div class="content">
    <p>This is NOT styleable in scope.</p>
  </div>
</div>

But that’s not the only unique problem this new spec solves. I think the “nearest ancestor” situation that Miriam lays out is perhaps the most interesting thing. I’ll send you over to the blog post to read about that — it’s pretty wild that we don’t have a good tool for that yet.

There is a lot to wrap your mind around here, especially as you think of more complex situations like multiple overlapping scopes and how the nesting syntax might interplay with scoping. Fortunately, Miriam is blogging these things very clearly.


The post Early Days for CSS Scoping appeared first on CSS-Tricks. You can support CSS-Tricks by being an MVP Supporter.


This content originally appeared on CSS-Tricks and was authored by Chris Coyier


Print Share Comment Cite Upload Translate Updates
APA

Chris Coyier | Sciencx (2021-09-06T18:47:19+00:00) Early Days for CSS Scoping. Retrieved from https://www.scien.cx/2021/09/06/early-days-for-css-scoping/

MLA
" » Early Days for CSS Scoping." Chris Coyier | Sciencx - Monday September 6, 2021, https://www.scien.cx/2021/09/06/early-days-for-css-scoping/
HARVARD
Chris Coyier | Sciencx Monday September 6, 2021 » Early Days for CSS Scoping., viewed ,<https://www.scien.cx/2021/09/06/early-days-for-css-scoping/>
VANCOUVER
Chris Coyier | Sciencx - » Early Days for CSS Scoping. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/06/early-days-for-css-scoping/
CHICAGO
" » Early Days for CSS Scoping." Chris Coyier | Sciencx - Accessed . https://www.scien.cx/2021/09/06/early-days-for-css-scoping/
IEEE
" » Early Days for CSS Scoping." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/09/06/early-days-for-css-scoping/. [Accessed: ]
rf:citation
» Early Days for CSS Scoping | Chris Coyier | Sciencx | https://www.scien.cx/2021/09/06/early-days-for-css-scoping/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.