W3C home > Mailing lists > Public > www-style@w3.org > July 2008

Re: Scoped style sheets.

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Tue, 15 Jul 2008 11:06:02 -0500
Message-ID: <dd0fbad0807150906k2e5b928ch7e99ff069468196f@mail.gmail.com>
To: "Andrew Fedoniouk" <news@terrainformatica.com>
Cc: "Ian Hickson" <ian@hixie.ch>, "David Hyatt" <hyatt@apple.com>, "Lachlan Hunt" <lachlan.hunt@lachy.id.au>, www-style <www-style@w3.org>
Andrew Fedoniouk <news@terrainformatica.com> wrote:

> I propose to drop that <style scoped> feature then.
> As, it seems, no one have clear idea of their purpose.


No...  Their purpose is quite clear, and has been explained in several ways
by several people.  They allow you to create a set of styles which can only
affect a subset of the document.

Ian Hickson <ian@hixie.ch>

> I don't think we want to make them "stronger" than other rules. The normal
> specificity rules are enough to handle this. Otherwise we end up making
> authors of containing pages having to carefully use !important to override
> scoped styles, which seems like requiring excessive force.

I disagree.  In fact, your example is actually a reason *for* making scoped
styles stronger than global styles.  I'll state the two good reasons I see
for making scoped styles automatically stronger than global styles.

1)  In order to effectively target the desired element, a global style often
needs a bit of targetting preamble before it gets down to the useful bit of
the selector.  Frex, if I'm styling the content of an article on my page, I
need at the very least something like a #content preamble to ensure that my
styling doesn't mess with the page template.  That's a huge chunk of
specificity right away that all styles have to overcome if they want to
override my styles - if scoped styles compete on the same terms, then *all*
of them *must* include at least one #id in their selectors to even have a
hope of applying.  If they are targetting similar things the useful part of
the selectors are likely going to be the same, and so all of my scoped
selectors need a special preamble (which likely involves reaching outside of
their scope just to gather appropriate #ids) for the sole purpose of making
sure they have enough specificity.

That is, I need to include *useless* information *outside* of my scope for
*every* selector if I want to make sure they apply.  Or I can just make them
all !important, which makes it even *more* difficult (maybe impossible) for
the site author to override the scoped styles when he really wants to.

Just to make it completely clear, take this document:
<style>
  #content p { color: red; }
</style>
<body>
  <header>header content...</header>
  <article id="content">
    <style scoped>
      p { color: blue }
    </style>
    <p>article content...</p>
  </article>
</body>

Imagine the article author doesn't have access to the global stylesheet,
which is why he uses scoped styles to style his article.  Since his styles
are limited to the article itself, it makes sense that if he wants all <p>s
to be blue he'd just use a "p" selector to target them.  However, his rule
is overridden by the global style due to the specificity rules.  He has to
change his selector to "#content p" to make it work, *even though his rules
are already limited to the #content element*.  That is *completely*
unintuitive, and partially negates the purpose of having a scoped style -
that you don't have to worry about content outside of your scope.  (We keep
global matching for several reasons, but provide a ways to effectively
ignore that with :scope when you want.)  The style author wants all his <p>s
in scope to be blue, and he should be able to say that in a simple manner,
without having to look up the global stylesheet to see just how specific he
needs to make his selectors.

2)  Your argument is that the global author should have an easy way to
override the scoped styles.  Why is this?  Again, part of the purpose of
scoped stylesheets seems to be to allow an author without access to the
global sheets to still easily style their chunk of content.  If the scope
author is specifically styling a scoped section, why would the global author
*want* to easily override their styles?  This cripples a large chunk of the
obvious use cases for <style scoped> right off the bat, and it does so
without the global author needing to specifically intend to do so.  It's
very easy to strongly override scoped styles by accident, purely because of
the aforementioned targetting preamble that the global author is likely to
use.

If it's vitally important that the global author enforce certain styles
within a scoped block, I'd much rather it be an intentional decision.
Requiring an !important rule is exactly that.


Now, a few comments.  One might be tempted to say that scoped styles should
exist in the normal cascade, but that :scope kicks them into a higher weight
so that scoped authors can easily override if they wish.  I don't think this
is at all useful - really all you're saying is "always use :scope in your
selector", and completely negating any benefit you may see in allowing the
global author to easily override styles.

However, I can see the benefits of a two-tiered approach, where !important
rules follow the normal cascade.  As noted, a site author generally doesn't
use !important rules unless they *really* want to make sure that something
shows up the way they specify.  Thus, it should be relatively difficult to
override them.  I can see the logic, then, in enforcing the full cascade so
that global !important rules tend to be more powerful unless the scoped
author goes out of his way to make sure his rules are more specific.



So, that was a whole lot of text for something fairly simple.  I propose the
following:
Ordinary scoped rules are treated as having *just* greater weight than
author rules (thus, lower weight than inline rules and such).  !important
scoped rules are treated as having the same weight as !important author
rules.
Received on Tuesday, 15 July 2008 16:20:22 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 27 April 2009 13:55:10 GMT