Descendant Selector
Descendant selectors target elements that are
contained within (therefore descendants of) another element. They are
indicated in a list separated by a character space (the combinator for
descendant selectors ), starting with the higher-level element. For example,
the following rule specifies that em
elements should be olive, but only when they are descendants of a list
item (li). All other em elements are unaffected by this
rule.
li em {color: olive;}Like simple type selectors , contextual selectors can be grouped together in
comma-separated lists. The following code makes emphasized (em) text red only when it appears in the
context of a first-, second-, or third-level heading:
h1 em, h2 em, h3 em {color: red;}Descendant selectors may also be nested several layers deep, as
shown in this example that targets only emphasized text (em) within anchors (a) within ordered lists (ol).
ol a em {font-weight: bold;}Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access