8000 [css-cascade-6][editorial] Examples of relative syntax in scope · xfq/csswg-drafts@206c2e1 · GitHub
Skip to content

Commit 206c2e1

Browse files
committed
[css-cascade-6][editorial] Examples of relative syntax in scope
1 parent 6ead17c commit 206c2e1

1 file changed

Lines changed: 91 additions & 8 deletions

File tree

css-cascade-6/Overview.bs

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ Scoping Styles: the ''@scope'' rule</h3>
267267
while the nested selectors only match elements
268268
that are [=in scope|in a resulting scope=]:
269269

270-
<pre class=lang-css>
270+
<pre highlight=css>
271271
@scope (.light-scheme) {
272272
/* Only match links inside a light-scheme */
273273
a { color: darkmagenta; }
@@ -290,7 +290,7 @@ Scoping Styles: the ''@scope'' rule</h3>
290290
an author can limit matching more deeply nested descendants.
291291
For example:
292292

293-
<pre class=lang-css>
293+
<pre highlight=css>
294294
@scope (.media-object) to (.content > *) {
295295
img { border-radius: 50%; }
296296
.content { padding: 1em; }
@@ -346,7 +346,7 @@ Effects of ''@scope''</h4>
346346
<div class=example>
347347
The following selectors have the same specificity (0,0,1):
348348

349-
<pre class=lang-css>
349+
<pre highlight=css>
350350
@scope (#hero) {
351351
img { border-radius: 50%; }
352352
}
@@ -386,7 +386,7 @@ Effects of ''@scope''</h4>
386386
Those custom scope attributes are then
387387
appended to every single selector in CSS:
388388

389-
<pre class=lang-css>
389+
<pre highlight=css>
390390
p[data-scope~='main-component'] { color: red; }
391391
p[data-scope~='sub-component'] { color: blue; }
392392

@@ -417,7 +417,7 @@ Effects of ''@scope''</h4>
417417
are excluded from the resulting scope,
418418
which allows authors to create non-overlapping scopes by default:
419419

420-
<pre class=lang-css>
420+
<pre highlight=css>
421421
@scope ([data-scope='main-component']) to ([data-scope]) {
422422
p { color: red; }
423423

@@ -437,7 +437,7 @@ Effects of ''@scope''</h4>
437437
and universal selector to create scope boundaries that overlap,
438438
such that the inner scope root is part of both scopes:
439439

440-
<pre class=lang-css>
440+
<pre highlight=css>
441441
@scope ([data-scope='main-component']) to ([data-scope] > *) {
442442
p { color: red; }
443443

@@ -490,6 +490,59 @@ Scoped Style Rules</h4>
490490
is interpreted as a non-[=relative selector=]
491491
(but the [=subject=] must still be [=in scope=] to match).
492492

493+
<div class="example">
494+
By default, selectors in a [=scoped style rule=]
495+
are [=relative selectors=],
496+
with the [=scoping root=] and [=descendant combinator=]
497+
implied at the start.
498+
The following selectors will match the same elements:
499+
500+
<pre highlight=css>
501+
@scope (#my-component) {
502+
p { color: green; }
503+
:scope p { color: green; }
504+
}
505+
</pre>
506+
507+
Authors can adjust the implied relationship
508+
by adding an explicit combinator:
509+
510+
<pre highlight=css>
511+
@scope (#my-component) {
512+
> p { color: green; }
513+
:scope > p { color: green; }
514+
}
515+
</pre>
516+
517+
Authors can also target or explicitly position
518+
the [=scoping root=] in a selector
519+
by including either '':scope'' or ''&'' in a given selector:
520+
521+
<pre highlight=css>
522+
@scope (#my-component) {
523+
:scope { border: thin solid; }
524+
& { border: thin solid; }
525+
526+
main :scope p { color: green; }
527+
main & p { color: green; }
528+
}
529+
</pre>
530+
531+
While the '':scope'' or ''&'' selectors
532+
can both refer to the [=scoping root=],
533+
they have otherwise different meanings in this context:
534+
535+
: Differences in selector matching
536+
:: The '':scope'' selector will only match the [=scoping root=] itself,
537+
while the ''&'' selector is able to match any element
538+
that is matched by the <<scope-start>> selector list.
539+
: Differences in selector specificity
540+
:: The '':scope'' selector has a specificity
541+
equal to other pseudo-classes,
542+
while the ''&'' selector has the specificity
543+
equal to the most specific selector in <<scope-start>>.
544+
</div>
545+
493546
<h4 id="scope-limits">
494547
Identifying Scoping Roots and Limits</h4>
495548

@@ -549,7 +602,7 @@ Identifying Scoping Roots and Limits</h4>
549602
[=Scoping limits=] can use the '':scope'' pseudo-class
550603
to require a specific relationship to the [=scoping root=]:
551604

552-
<pre class=lang-css>
605+
<pre highlight=css>
553606
/* .content is only a limit when it is a direct child of the :scope */
554607
@scope (.media-object) to (:scope > .content) { ... }
555608
</pre>
@@ -558,7 +611,7 @@ Identifying Scoping Roots and Limits</h4>
558611
by using '':scope''.
559612
For example:
560613

561-
<pre class=lang-css>
614+
<pre highlight=css>
562615
/* .content is only a limit when the :scope is inside .sidebar */
563616
@scope (.media-object) to (.sidebar :scope .content) { ... }
564617
</pre>
@@ -574,6 +627,36 @@ Scope Nesting</h4>
574627
are [=scoped selectors|scoped by=]
575628
the selectors of the outer one.
576629

630+
<div class=example>
631+
When nesting ''@scope'' rules inside other ''@scope'' rules,
632+
or inside other selectors,
633+
the <<scope-start>> selector is
634+
[=relative selector|relative to=] the nesting context,
635+
while the <<scope-end>> and any [=scoped style rules=]
636+
are [=relative selector|relative to=] the [=scoping root=]
637+
For example, the following code:
638+
639+
<pre highlight=css>
640+
@scope (.parent-scope) {
641+
@scope (:scope > .child-scope) to (:scope .limit) {
642+
:scope .content {
643+
color: red;
644+
}
645+
}
646+
}
647+
</pre>
648+
649+
is equivalent to:
650+
651+
<pre highlight=css>
652+
@scope (.parent-scope > .child-scope) to (.parent-scope > .child-scope .limit) {
653+
.parent-scope > .child-scope .content {
654+
color: red;
655+
}
656+
}
657+
</pre>
658+
</div>
659+
577660
Global name-defining [=at-rules=]
578661
such as ''@keyframes'' or ''@font-face'' or ''@layer''
579662
that are defined inside ''@scope'' are valid,

0 commit comments

Comments
 (0)