Using sibling selectors (span + span
) ...
Quick brown fox jumps over the lazy dog.
But by making the elements nest, and
using the parent selector (span > span
) ...
Quick brown fox jumps over the lazy dog.
... we get an interesting telescoping effect.
This example has boxes drawn around each span
within
a span
to show the nesting more clearly:
Quick brown fox jumps over the lazy dog.
Why do we need the CSS rule
span {font-variant: normal}
to cancel the effect of the
p.example > span:first-child {font-variant:small-caps}
on all of the words after the first?
What would happen if we used this CSS rule:
instead of
span:first-child {font-size:550%}
p.example > span:first-child {font-size:550%}
?
If you have trouble remembering what the selectors mean then see the following chart and the excellent Selectoracle website.
Example | |||
---|---|---|---|
Pattern | Means | Selector | Matches |
(blank) | ancestor | p.example span |
span s that are between <p
class="example"> and its </p> |
> |
parent | p.example > span |
span s that are immediate children of <p
class="example"> |
+ |
sibling | h2 + p |
any p that immediately follows a
h2 |
. |
class attribute | p.example |
any p that has example as a word in
its class attribute |
# |
id attribute |
span#warning |
only the span that includes id
= "warning" in its attributes |
:first-child |
itself a first child | span:first-child |
any span that is the first child of its
parent |
See