<span>/* default black */ </span><span>p { </span> <span>color: #000; </span><span>} </span> <span>/* gray in <article>, <section>, or <aside> */ </span><span>article p<span>, </span></span><span>section p<span>, </span></span><span>aside p { </span> <span>color: #444; </span><span>} </span>
- jusqu'à ce que la nidification native arrive, vous aurez besoin d'un outil de construction CSS. Vous voudrez peut-être utiliser une option comme SASS, mais cela peut introduire des complications pour certaines équipes de développement.
- La nidification peut causer d'autres problèmes. Il est facile de construire des sélecteurs profondément imbriqués qui deviennent de plus en plus difficiles à lire et à sortir Verbose CSS.
<span>article, section, aside { </span> <span>p { </span> <span>color: #444; </span> <span>} </span> <span>} </span>
, et
qui sont des enfants d'un Voir le stylo
Utilisation du: est sélecteur par SitePoint (@SitePoint)
sur Codepen. <span><span>:is(article, section, aside) p</span> {
</span> <span>color: #444;
</span><span>}
</span>
<span>article section<span>:not(:first-child):is(.primary, .secondary) :is(h1, h2, p)</span> {
</span> <span>color: green;
</span><span>}
</span>
<span>article section<span>.primary:not(:first-child) h1,
</span></span><span>article section<span>.primary:not(:first-child) h2,
</span></span><span>article section<span>.primary:not(:first-child) p,
</span></span><span>article section<span>.secondary:not(:first-child) h1,
</span></span><span>article section<span>.secondary:not(:first-child) h2,
</span></span><span>article section<span>.secondary:not(:first-child) p</span> {
</span> <span>color: green;
</span><span>}
</span>
Le CSS: où le sélecteur de pseudo-classe
: où () la syntaxe du sélecteur est identique à: est () et est également prise en charge dans tous les navigateurs modernes (pas IE). Cela entraînera souvent un style identique. Par exemple:
<span>/* NOT VALID - selector will not work */
</span><span>div<span>:is(::before, ::after)</span> {
</span> <span>display: block;
</span> <span>content: '';
</span> <span>width: 1em;
</span> <span>height: 1em;
</span> <span>color: blue;
</span><span>}
</span>
<span><span>:where(article, section, aside) p</span> {
</span> <span>color: #444;
</span><span>}
</span>
<span>article p { color: #444; }
</span><span>p { color: #000; }
</span>
<span>article p {
</span> <span>color: black;
</span><span>}
</span>
<span><span>:is(article, section, aside) p</span> {
</span> <span>color: red;
</span><span>}
</span>
<span><span>:where(article, section, aside) p</span> {
</span> <span>color: blue;
</span><span>}
</span>
les en-têtes à moins que ne soit le premier enfant d'un élément
<span>p {
</span> <span>font-weight: bold;
</span><span>}
</span>
personnalisée plus tard dans la feuille de style n'a aucun effet, car l'article: First-enfant a une spécificité plus élevée:
<span>/* default black */
</span><span>p {
</span> <span>color: #000;
</span><span>}
</span>
<span>/* gray in <article>, <section>, or <aside> */
</span><span>article p<span>,
</span></span><span>section p<span>,
</span></span><span>aside p {
</span> <span>color: #444;
</span><span>}
</span>
<span>article, section, aside {
</span>
<span>p {
</span> <span>color: #444;
</span> <span>}
</span>
<span>}
</span>
<span><span>:is(article, section, aside) p</span> {
</span> <span>color: #444;
</span><span>}
</span>
<span>article section<span>:not(:first-child):is(.primary, .secondary) :is(h1, h2, p)</span> {
</span> <span>color: green;
</span><span>}
</span>
<span>article section<span>.primary:not(:first-child) h1,
</span></span><span>article section<span>.primary:not(:first-child) h2,
</span></span><span>article section<span>.primary:not(:first-child) p,
</span></span><span>article section<span>.secondary:not(:first-child) h1,
</span></span><span>article section<span>.secondary:not(:first-child) h2,
</span></span><span>article section<span>.secondary:not(:first-child) p</span> {
</span> <span>color: green;
</span><span>}
</span>
Le CSS: a un sélecteur de pseudo-classe
Le sélecteur: a () utilise une syntaxe similaire pour: est () et: où (), mais il cible un élément qui contient un ensemble d'autres. Par exemple, voici le CSS pour ajouter une bordure bleue à deux pixels à n'importe quel lien qui contient une ou plusieurs balises <span>/* NOT VALID - selector will not work */
</span><span>div<span>:is(::before, ::after)</span> {
</span> <span>display: block;
</span> <span>content: '';
</span> <span>width: 1em;
</span> <span>height: 1em;
</span> <span>color: blue;
</span><span>}
</span>
En supposant que les fournisseurs ont résolu les problèmes de performance, l'introduction de: a () permet des possibilités qui auraient été impossibles sans JavaScript dans le passé. Par exemple, vous pouvez définir les styles d'une forme extérieure