forked from w3c/csswg-drafts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOverview.bs
1200 lines (997 loc) · 31.3 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: CSS Nesting Module
Shortname: css-nesting
Level: 1
Status: ED
Work Status: Exploring
Group: CSSWG
ED: https://drafts.csswg.org/css-nesting/
TR: https://www.w3.org/TR/css-nesting-1/
Editor: Tab Atkins-Bittner, Google, http://xanthir.com/contact/, w3cid 42199
Editor: Adam Argyle, Google, https://nerdy.dev, w3cid 112669
Abstract: This module introduces the ability to nest one style rule inside another, with the selector of the child rule relative to the selector of the parent rule. This increases the modularity and maintainability of CSS stylesheets.
</pre>
<pre class=link-defaults>
spec:css-color-4; type:property; text:color
spec:css-values-4; type:dfn; text:identifier
spec:cssom-1; type:dfn;
text:child css rules
text:specified order
</pre>
<h2 id="intro">
Introduction</h2>
<em>This section is not normative.</em>
This module describes support for nesting a style rule within another style rule,
allowing the inner rule's selector to reference the elements matched by the outer rule.
This feature allows related styles to be aggregated into a single structure within the CSS document,
improving readability and maintainability.
<h3 id="placement">
Module Interactions</h3>
This module introduces new parser rules that extend the [[!CSS21]] parser model.
It introduces selectors that extend the [[SELECTORS-4]] module.
It extends and modifies some IDL and algorithms defined in the [[CSSOM-1]] module.
<h3 id="values">
Values</h3>
This specification does not define any new properties or values.
<h3 id="motivation">
Motivation</h3>
The CSS for even moderately complicated web pages
often include lots of duplication
for the purpose of styling related content.
For example, here is a portion of the CSS markup for one version of the [[CSS-COLOR-3]] module:
<div class='example'>
<pre class=lang-css>
table.colortable td {
text-align:center;
}
table.colortable td.c {
text-transform:uppercase;
}
table.colortable td:first-child, table.colortable td:first-child+td {
border:1px solid black;
}
table.colortable th {
text-align:center;
background:black;
color:white;
}
</pre>
</div>
Nesting allows the grouping of related style rules, like this:
<div class='example'>
<pre class=lang-css>
table.colortable {
& td {
text-align:center;
&.c { text-transform:uppercase }
&:first-child, &:first-child + td { border:1px solid black }
}
& th {
text-align:center;
background:black;
color:white;
}
}
</pre>
</div>
Besides removing duplication,
the grouping of related rules improves the readability and maintainability of the resulting CSS.
Nesting Style Rules {#nesting}
==============================
Style rules can be nested inside of other styles rules.
These <dfn export lt="nested style rule|nesting style rule">nested style rules</dfn>
act exactly like ordinary style rules--
associating properties with elements via selectors--
but they "inherit" their parent rule's selector context,
allowing them to further build on the parent's selector
without having to repeat it,
possibly multiple times.
A [=nested style rule=] is exactly like a normal style rule,
with the exception that its [=selector=]
cannot start with an [=identifier=] or [=functional notation=].
Additionally, [=nested style rules=]
can use [=relative selectors=].
<div class=example>
That is,
a nested style rule like:
<pre class=lang-css>
.foo {
color: red;
.bar {
color: blue;
}
}
</pre>
is valid,
and equivalent to:
<pre class=lang-css>
.foo {
color: red;
}
.foo .bar {
color: blue;
}
</pre>
The nested rule can also use the [=nesting selector=]
to directly refer to the parent rule's matched elements,
or use [=relative selector=] syntax
to specify relationships other than "descendant".
<pre class=lang-css>
.foo {
color: red;
&:hover {
color: blue;
}
}
/* equivalent to: */
.foo { color: red; }
.foo:hover { color: blue; }
</pre>
<pre class=lang-css>
.foo {
color: red;
+ .bar {
color: blue;
}
}
/* equivalent to: */
.foo { color: red; }
.foo + .bar { color: blue; }
</pre>
</div>
<div class=example>
However, starting the nested selector with an identifier
(a [=type selector=], in other words)
is invalid:
<pre class=lang-css class=invalid>
div {
color: red;
input {
margin: 1em;
}
}
/* Invalid, because "input" is an identifier. */
</pre>
Such selectors can still be written,
they just need to be slightly rephrased:
<pre class=lang-css>
div {
color: red;
& input { margin: 1em; }
/* valid, no longer starts with an identifier */
:is(input) { margin: 1em; }
/* valid, starts with a colon,
and equivalent to the previous rule. */
}
</pre>
</div>
<details class=note>
<summary>Why are there restrictions on nested rule selectors?</summary>
Nesting style rules naively inside of other style rules is, unfortunately, ambiguous--
the syntax of a selector overlaps with the syntax of a declaration,
so an implementation requires unbounded lookahead
to tell whether a given bit of text is a declaration or the start of a style rule.
For example, if a parser starts by seeing ''color:hover ...'',
it can't tell whether that's the 'color' property
(being set to an invalid value...)
or a selector for a <code><color></code> element.
It can't even rely on looking for valid properties to tell the difference;
this would cause parsing to depend on which properties the implementation supported,
and could change over time.
Forbidding nested style rules from starting with an [=identifier=]
works around this problem--
all [=declarations=] start with an identifier
giving the property name,
so the parser can immediately tell whether it's parsing a [=declaration=] or [=style rule=].
Some non-browser implementations of nested rules do not impose this requirement.
It <em>is</em>, in most cases, <em>eventually</em> possible
to tell properties and selectors apart,
but doing so requires unbounded lookahead in the parser;
that is, the parser might have to hold onto an unknown amount of content
before it can tell which way it's supposed to be interpreting it.
CSS to date requires only a small, known amount of lookahead in its parsing,
which allows for more efficient parsing algorithms,
so unbounded lookahead is generally considered unacceptable among browser implementations of CSS.
</details>
<!--
██████ ██ ██ ██ ██ ████████ ███ ██ ██
██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██
██ ████ ████ ██ ██ ██ ██ ██ ██
██████ ██ ██ ██ ██ ██ ██ ██ ███
██ ██ ██ ████ ██ █████████ ██ ██
██ ██ ██ ██ ███ ██ ██ ██ ██ ██
██████ ██ ██ ██ ██ ██ ██ ██ ██
-->
Syntax {#syntax}
------------------------
The contents of [=style rules=]
now accepts [=nested style rules=]
and [=at-rules=],
in addition to the existing [=declarations=].
[=Nested style rules=] differ from non-nested rules
in the following ways:
* The selector of [=nested style rules=]
must not start with an [=identifier=]
or a [=functional notation=].
* A [=nested style rule=] accepts a <<relative-selector-list>>
as its prelude
(rather than just a <<selector-list>>).
Any [=relative selectors=]
are relative to the elements represented by the [=nesting selector=].
* If a selector in the <<relative-selector-list>>
does not start with a [=combinator=]
but does [=contain the nesting selector=],
it is interpreted as a non-[=relative selector=].
The precise details of how nested style rules are parsed
are defined in [[!CSS-SYNTAX-3]].
ISSUE(7961): The CSSWG is currently exploring the consequences of parsing lookahead,
and may adjust the allowed syntax as a result.
An invalid [=nested style rule=] is ignored,
along with its contents,
but does not invalidate its parent rule.
<div class="example">
For example, the following nestings are valid:
<pre class=lang-css>
<b>/* & can be used on its own */</b>
.foo {
color: blue;
& > .bar { color: red; }
> .baz { color: green; }
}
/* equivalent to
.foo { color: blue; }
.foo > .bar { color: red; }
.foo > .baz { color: green; }
*/
<b>/* or in a compound selector,
refining the parent's selector */</b>
.foo {
color: blue;
&.bar { color: red; }
}
/* equivalent to
.foo { color: blue; }
.foo.bar { color: red; }
*/
<b>/* multiple selectors in the list are all
relative to the parent */</b>
.foo, .bar {
color: blue;
+ .baz, &.qux { color: red; }
}
/* equivalent to
.foo, .bar { color: blue; }
:is(.foo, .bar) + .baz,
:is(.foo, .bar).qux { color: red; }
*/
<b>/* & can be used multiple times in a single selector */</b>
.foo {
color: blue;
& .bar & .baz & .qux { color: red; }
}
/* equivalent to
.foo { color: blue; }
.foo .bar .foo .baz .foo .qux { color: red; }
*/
<b>/* & doesn't have to be at the beginning of the selector */</b>
.foo {
color: red;
.parent & {
color: blue;
}
}
/* equivalent to
.foo { color: red; }
.parent .foo { color: blue; }
*/
.foo {
color: red;
:not(&) {
color: blue;
}
}
/* equivalent to
.foo { color: red; }
:not(.foo) { color: blue; }
*/
<b>/* But if you use a <l>[=relative selector=]</l>,
an initial & is implied automatically */</b>
.foo {
color: red;
+ .bar + & { color: blue; }
}
/* equivalent to
.foo { color: red; }
.foo + .bar + .foo { color: blue; }
*/
<b>/* Somewhat silly, but & can be used all on its own, as well. */</b>
.foo {
color: blue;
& { padding: 2ch; }
}
/* equivalent to
.foo { color: blue; }
.foo { padding: 2ch; }
// or
.foo {
color: blue;
padding: 2ch;
}
*/
<b>/* Again, silly, but can even be doubled up. */</b>
.foo {
color: blue;
&& { padding: 2ch; }
}
/* equivalent to
.foo { color: blue; }
.foo.foo { padding: 2ch; }
*/
<b>/* The parent selector can be arbitrarily complicated */</b>
.error, #404 {
&:hover > .baz { color: red; }
}
/* equivalent to
:is(.error, #404):hover > .baz { color: red; }
*/
.ancestor .el {
.other-ancestor & { color: red; }
}
/* equivalent to
.other-ancestor :is(.ancestor .el) { color: red; }
<b>/* As can the nested selector */</b>
.foo {
& :is(.bar, &.baz) { color: red; }
}
/* equivalent to
.foo :is(.bar, .foo.baz) { color: red; }
*/
<b>/* Multiple levels of nesting "stack up" the selectors */</b>
figure {
margin: 0;
> figcaption {
background: hsl(0 0% 0% / 50%);
> p {
font-size: .9rem;
}
}
}
/* equivalent to
figure { margin: 0; }
figure > figcaption { background: hsl(0 0% 0% / 50%); }
figure > figcaption > p { font-size: .9rem; }
*/
<b>/* Example usage with Cascade Layers */</b>
@layer base {
html {
block-size: 100%;
& body {
min-block-size: 100%;
}
}
}
/* equivalent to
@layer base {
html { block-size: 100%; }
html body { min-block-size: 100%; }
}
*/
<b>/* Example nesting Cascade Layers */</b>
@layer base {
html {
block-size: 100%;
@layer support {
& body {
min-block-size: 100%;
}
}
}
}
/* equivalent to
@layer base {
html { block-size: 100%; }
}
@layer base.support {
html body { min-block-size: 100%; }
}
*/
<b>/* Example usage with Scoping */</b>
@scope (.card) to (> header) {
:scope {
inline-size: 40ch;
aspect-ratio: 3/4;
> header {
border-block-end: 1px solid white;
}
}
}
/* equivalent to
@scope (.card) to (> header) {
:scope { inline-size: 40ch; aspect-ratio: 3/4; }
:scope > header { border-block-end: 1px solid white; }
}
*/
<b>/* Example nesting Scoping */</b>
.card {
inline-size: 40ch;
aspect-ratio: 3/4;
@scope (&) to (> header > *) {
:scope > header {
border-block-end: 1px solid white;
}
}
}
/* equivalent to
.card { inline-size: 40ch; aspect-ratio: 3/4; }
@scope (.card) to (> header > *) {
:scope > header { border-block-end: 1px solid white; }
}
*/
</pre>
But these are not valid:
<pre class=lang-css>
<b>/* Selector starts with an identifier */</b>
.foo {
color: blue;
div {
color: red;
}
}
</pre>
</div>
<div class=note>
Some CSS-generating tools that preprocess nesting
will concatenate selectors as strings,
allowing authors to build up a <em>single</em> [=simple selector=]
across nesting levels.
This is sometimes used with hierarchical name patterns
like <a href="https://en.wikipedia.org/wiki/CSS#:~:text=bem%20(block%2C%20element%2C%20modifier)">BEM</a>
to reduce repetition across a file,
when the selectors themselves have significant repetition internally.
For example, if one component uses the class ''.foo'',
and a nested component uses ''.fooBar'',
you could write this in <a href="https://sass-lang.com/">Sass</a> as:
<pre class="lang-css">
.foo {
color: blue;
&Bar { color: red; }
}
/* In Sass, this is equivalent to
.foo { color: blue; }
.fooBar { color: red; }
*/
</pre>
Unfortunately, this string-based interpretation is ambiguous with
the author attempting to add a type selector in the nested rule.
''Bar'', for example,
<em>is</em> a valid <a href="https://html.spec.whatwg.org/multipage/custom-elements.html">custom element name</a> in HTML.
CSS doesn't do this:
the nested selector components are interpreted atomically,
and not string-concatenated:
<pre class="lang-css">
.foo {
color: blue;
&Bar { color: red; }
}
/* In CSS, this is instead equivalent to
.foo { color: blue; }
Bar.foo { color: red; }
*/
</pre>
</div>
<div algorithm>
A selector is said to <dfn export>contain the nesting selector</dfn>
if, when it was [=CSS/parsed=] as any type of selector,
a <<delim-token>> with the value "&" (U+0026 AMPERSAND) was encountered.
Note: This is phrased in this explicit manner
so as to catch cases like '':is(&:unknown, .bar)'',
which discards its first argument during parsing
(since it contains an unknown pseudo-class,
and thus is an [=invalid selector=]).
A selector like this still [=contains the nesting selector=].
</div>
If a <<forgiving-selector-list>> has an item that [=contains the nesting selector=]
but is invalid,
it is preserved exactly as-is
rather than being discarded.
(This does not change the matching behavior of the selector--
an invalid selector still fails to match anything--
just the serialization of the selector.)
Issue: The preceding paragraph needs to move to Selectors
when we move ''&'' itself to Selectors;
I'm monkey-patching for convenience here.
<!--
██████ ███████ ██ ██ ████████
██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ████ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ███ ██ ██
██████ ███████ ██ ██ ████████
-->
Nesting Other At-Rules {#conditionals}
--------------------------------------
In addition to [=nested style rules=],
this specification allows <dfn export>nested group rules</dfn>
inside of [=style rules=]:
any at-rule whose body contains [=style rules=]
can be nested inside of a [=style rule=] as well.
When nested in this way,
the contents of a [=nested group rule=] are parsed as <<style-block>>
rather than <<stylesheet>>:
* [=Style rules=] are [=nested style rules=],
with their [=nesting selector=] taking its definition
from the nearest ancestor [=style rule=].
* Properties can be directly used,
acting as if they were nested in a <code>& {...}</code> block.
<div class=note>
Specifically, these rules are capable of being [=nested group rules=]:
* all the [=conditional group rules=] (''@media'', ''@supports'')
* ''@layer''
* ''@scope''
* ''@container''
</div>
The meanings and behavior of such [=nested group rules=]
is otherwise unchanged,
unless otherwise specified.
<div class="example">
For example, the following conditional nestings are valid:
<pre class="lang-css">
<b>/* Properties can be directly used */</b>
.foo {
display: grid;
@media (orientation: landscape) {
grid-auto-flow: column;
}
}
/* equivalent to
.foo {
display: grid;
@media (orientation: landscape) {
& {
grid-auto-flow: column;
}
}
}
*/
/* finally equivalent to
.foo { display: grid; }
@media (orientation: landscape) {
.foo {
grid-auto-flow: column;
}
}
*/
<b>/* Conditionals can be further nested */</b>
.foo {
display: grid;
@media (orientation: landscape) {
grid-auto-flow: column;
@media (min-width > 1024px) {
max-inline-size: 1024px;
}
}
}
/* equivalent to
.foo { display: grid; }
@media (orientation: landscape) {
.foo {
grid-auto-flow: column;
}
}
@media (orientation: landscape) and (min-width > 1024px) {
.foo {
max-inline-size: 1024px;
}
}
*/
<b>/* Example nesting Cascade Layers */</b>
html {
@layer base {
block-size: 100%;
@layer support {
& body {
min-block-size: 100%;
}
}
}
}
/* equivalent to
@layer base {
html { block-size: 100%; }
}
@layer base.support {
html body { min-block-size: 100%; }
}
*/
<b>/* Example nesting Scoping */</b>
.card {
inline-size: 40ch;
aspect-ratio: 3/4;
@scope (&) {
:scope {
border: 1px solid white;
}
}
}
/* equivalent to
.card { inline-size: 40ch; aspect-ratio: 3/4; }
@scope (.card) {
:scope { border-block-end: 1px solid white; }
}
*/
</pre>
</div>
All directly-nested properties are treated
as if they were collected together, in order,
and nested in a [=nested style rule=]
with the selector ''&'',
and placed before all other child rules.
This includes in the OM.
(That is,
the {{CSSContainerRule/childRules}} attribute
actually starts with this [=nested style rule=],
containing all the directly-nested properties.)
<div class=example>
For example, the earlier example:
<pre highlight=css>
.foo {
display: grid;
@media (orientation: landscape) {
grid-auto-flow: column;
}
}
/* equivalent to
.foo {
display: grid;
@media (orientation: landscape) {
& {
grid-auto-flow: column;
}
}
}
*/
</pre>
is in fact <em>exactly</em> equivalent,
producing the exact same CSSOM structure.
The {{CSSMediaRule}} object
will have a single {{CSSStyleRule}} object
in its <code highlight=js>.childRules</code> attribute,
containing the 'grid-auto-flow' property.
</div>
Note: This does mean that the serialization of such rules will differ
from how they were originally written,
with <em>no</em> directly-nested properties in the serialization.
<h4 id=nesting-at-scope>
Nested ''@scope'' Rules</h4>
When the ''@scope'' rule is a [=nested group rule=],
an ''&'' in the <<scope-start>> selector
refers to the elements matched
by the nearest ancestor style rule.
For the purposes of the style rules in its body
and its own <<scope-end>> selector,
the ''@scope'' rule is treated as an ancestor style rule,
matching the elements matched by its <<scope-start>> selector.
<div class=example>
That is, the following code:
<pre highlight=css>
.parent {
color: blue;
@scope (& > .scope) to (& .limit) {
& .content {
color: red;
}
}
}
</pre>
is equivalent to:
<pre highlight=css>
.parent { color: blue; }
@scope (.parent > .scope) to (.parent > .scope .limit) {
.parent > .scope .content {
color: red;
}
}
</pre>
</div>
<!--
██ ██ ████ ██ ██ ████ ██ ██ ██████
███ ███ ██ ██ ██ ██ ███ ██ ██ ██
████ ████ ██ ██ ██ ██ ████ ██ ██
██ ███ ██ ██ ███ ██ ██ ██ ██ ██ ████
██ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ███ ██ ██
██ ██ ████ ██ ██ ████ ██ ██ ██████
-->
Mixing Nesting Rules and Declarations {#mixing}
-----------------------------------------------
When a style rule contains both declarations
and [=nested style rules=] or [=nested conditional group rules=],
all three can be arbitrarily mixed.
However, the relative order of declarations
vs other rules
is not preserved in any way.
<div class=example>
For example,
in the following code:
<pre class=lang-css>
article {
color: green;
& { color: blue; }
color: red;
}
/* equivalent to */
article {
color: green;
color: red;
& { color: blue; }
}
</pre>
</div>
For the purpose of determining the [[css-cascade-4#cascade-sort|Order Of Appearance]],
[=nested style rules=] and [=nested conditional group rules=]
are considered to come <em>after</em> their parent rule.
<div>
For example:
<xmp class=lang-css>
article {
color: blue;
& { color: red; }
}
</xmp>
Both declarations have the same specificity (0,0,1),
but the nested rule is considered to come <em>after</em> its parent rule,
so the ''color: red'' declarations wins the cascade.
On the other hand, in this example:
<xmp class=lang-css>
article {
color: blue;
:where(&) { color: red; }
}
</xmp>
The '':where()'' pseudoclass reduces the specificity of the [=nesting selector=] to 0,
so the ''color: red'' declaration now has a specificity of (0,0,0),
and loses to the ''color: blue'' declaration
before "Order Of Appearance" comes into consideration.
</div>
Note: While one <em>can</em> freely intermix declarations and nested rules,
it's harder to read and somewhat confusing to do so,
since all the properties <em>act as if</em> they came before all the rules.
For readability's sake,
it's recommended that authors put all their properties first in a style rule,
before any nested rules.
(This also happens to act slightly better in older user agents;
due to specifics of how parsing and error-recovery work,
properties appearing after nested rules can get skipped.)
Note: Like with other types of rules,
the serialization of style rules in the presence of nesting
can vary from how they were originally written.
Notably, all directly-nested properties
will be serialized before any nested rules,
which is <em>another</em> reason to write properties before rules.
<!--
████
██ ██
████
████
██ ██ ██
██ ██
████ ██
-->
Nesting Selector: the ''&'' selector {#nest-selector}
=====================================================
When using a <a>nested style rule</a>,
one must be able to refer to the elements matched by the parent rule;
that is, after all, <em>the entire point of nesting</em>.
To accomplish that,
this specification defines a new selector,
the <dfn export>nesting selector</dfn>,
written as <dfn selector>&</dfn> (U+0026 AMPERSAND).
When used in the selector of a <a>nested style rule</a>,
the <a>nesting selector</a> represents the elements matched by the parent rule.
When used in any other context,
it represents the same elements as '':scope'' in that context
(unless otherwise defined).
<div class="note">
The <a>nesting selector</a> can be desugared
by replacing it with the parent style rule's selector,
wrapped in an '':is()'' selector.
For example,
<pre class=lang-css>
a, b {
& c { color: blue; }
}
</pre>
is equivalent to
<pre class=lang-css>
:is(a, b) c { color: blue; }
</pre>
</div>
The [=nesting selector=] cannot represent pseudo-elements
(identical to the behavior of the '':is()'' pseudo-class).
<div class=example>
For example, in the following style rule:
<pre class=lang-css>
.foo, .foo::before, .foo::after {
color: red;
&:hover { color: blue; }
}
</pre>
the ''&'' only represents the elements matched by ''.foo'';
in other words, it's equivalent to:
<pre class=lang-css>
.foo, .foo::before, .foo::after {
color: red;
}
.foo:hover {
color: blue;
}
</pre>
</div>