@@ -561,94 +561,101 @@ Nesting Conditional Rules {#conditionals}
561
561
Mixing Nesting Rules and Declarations {#mixing}
562
562
-----------------------------------------------
563
563
564
- A style rule can have any number of <a>nested style rules</a> inside of it,
565
- of either type,
566
- intermixed with any number of declarations,
567
- in any order .
564
+ A style rule can have any number of declarations inside of it,
565
+ followed by any number of rules.
566
+ Declarations occuring <em> after </em> a nested rule
567
+ are invalid .
568
568
569
- The relative ordering of <a>nested style rules</a> , <a>nested conditional rules</a> and
570
- other declarations <strong> is</strong> important;
571
- it's possible for a given style rule and a <a>nested style rule</a> within it to match the same element,
572
- and if the specificity of the two rules is otherwise equivalent,
573
- the relative order in the stylesheet of the applicable declarations
574
- determines which declaration "wins" the <a>cascade</a> .
575
-
576
- <div class="example">
577
- For example, consider the following where the specificity is the same and the
578
- cascade is used for value resolution:
569
+ <div class=example>
570
+ For example,
571
+ in the following code:
579
572
580
- <pre class="lang- css" >
573
+ <pre highlight= css>
581
574
article {
582
- & .blue { color: blue; } /* (0,1,1) */
583
- & .red { color: red; } /* (0,1,1) */
575
+ color: green;
576
+ & { color: blue; }
577
+ color: red;
584
578
}
585
-
586
- /* equivalent to
587
- article .blue { color: blue; }
588
- article .red { color: red; }
589
- */
590
- </pre>
591
-
592
- <pre class="lang-html">
593
- <article>
594
- <div class="red blue"></div>
595
- </article>
596
-
597
- <!-- div text color is red -->
598
579
</pre>
599
580
600
- The specificity of ''article .blue'' and ''article .red'' is (0,1,1).
581
+ The ''color: red'' declaration is invalid and ignored,
582
+ since it occurs after the [=nested style rule=] .
601
583
602
- Now consider this follow up which adds an ID selector:
584
+ However, further nested rules are still valid,
585
+ as in this example:
603
586
604
- <pre class="lang- css" >
587
+ <pre highlight= css>
605
588
article {
606
- & .blue, & #gotcha { color: blue; } /* (1,1,1) */
607
- & .red { color: red; } /* (0,1,1) */
589
+ color: green;
590
+ & { color: blue; }
591
+ color: red;
592
+ &.foo { color: yellow; } /* valid! */
608
593
}
609
-
610
- /* equivalent to
611
- article :is(.blue, #gotcha) { color: blue; }
612
- article .red { color: red; }
613
- */
614
594
</pre>
595
+ </div>
615
596
616
- <pre class="lang-html">
617
- <article>
618
- <div class="red blue"></div>
619
- </article>
597
+ The relative ordering of <a>nested style rules</a> and <a>nested conditional rules</a>
598
+ <strong> is</strong> important;
599
+ it's possible for a given style rule and a <a>nested style rule</a> within it to match the same element,
600
+ and if the specificity of the two rules is otherwise equivalent,
601
+ the relative order in the stylesheet of the applicable declarations
602
+ determines which declaration "wins" the <a>cascade</a> .
603
+ For this purpose,
604
+ a nested rule is considered to come <em> after</em> its parent rule.
620
605
621
- <!-- div text color is blue -->
622
- </pre>
606
+ <div class="example">
607
+ For example, consider the following where the specificity is the same and the
608
+ cascade is used for value resolution:
623
609
624
- ''article :is(.blue, #gotcha)'' now has a specificity score of (1,1,1).
625
- Nesting uses '':is()'' which assumes the specificity of it's highest selector.
610
+ <xmp class="lang-css">
611
+ article {
612
+ & .blue { color: blue; } /* (0,1,1) */
613
+ & .red { color: red; } /* (0,1,1) */
614
+ }
626
615
616
+ /* equivalent to
617
+ article .blue { color: blue; }
618
+ article .red { color: red; }
619
+ */
620
+ </xmp>
627
621
628
- To work around it, the following could be written instead:
622
+ <xmp class="lang-html">
623
+ <article>
624
+ <div class="red blue"></div>
625
+ </article>
629
626
630
- <pre class="lang-css">
631
- article {
632
- & .blue { color: blue; } /* (0,1,1) */
633
- & .red { color: red; } /* (0,1,1) */
634
- & #gotcha { color: blue; } /* (1,0,1) */
635
- }
627
+ <!-- div text color is red -->
628
+ </xmp>
636
629
637
- /* equivalent to
638
- article .blue { color: blue; }
639
- article .red { color: red; }
640
- article #gotcha { color: blue; }
641
- */
642
- </pre>
630
+ The specificity of ''article .blue'' and ''article .red'' are both (0,1,1).
631
+ </div>
643
632
644
- <pre class="lang-html">
645
- <article>
646
- <div class="red blue"></div>
647
- </article>
633
+ <div>
634
+ In this example:
648
635
649
- <!-- div text color is red -->
650
- </pre>
636
+ <xmp highlight=css>
637
+ article {
638
+ color: blue;
639
+ & { color: red; }
640
+ }
641
+ </xmp>
642
+
643
+ Both declarations have the same specificity (0,0,1),
644
+ but the nested rule is considered to come <em> after</em> its parent rule,
645
+ so the ''color: red'' declarations wins.
646
+
647
+ On the other hand, in this example:
648
+
649
+ <xmp highlight=css>
650
+ article {
651
+ color: blue;
652
+ :where(&) { color: red; }
653
+ }
654
+ </xmp>
651
655
656
+ The '':where()'' pseudoclass reduces the specificity of the [=nesting selector=] to 0,
657
+ so the ''color: red'' declaration now has a specificity of (0,0,0),
658
+ and loses to the ''color: blue'' declaration.
652
659
</div>
653
660
654
661
0 commit comments