Descendant Selectors : selector « Style Basics « HTML / CSS






Descendant Selectors

  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en'>
    <head>
        <title>Descendant Selectors</title>
<style type='text/css'>
body {
    font-face: sans-serif;
}
h1 {
    margin: 5px;
}
p {
    border: 1px solid rgb(200, 200, 200);
    background: rgb(234, 234, 234);
    padding: 5px;
    margin: 5px;
}
p.note {
    background: yellow;
    border: 1px solid gold;
}
span.code {
    font-family: monospace;
    padding: 0 10px;
}
p span.code {
    background: yellow;
}
p.note span.code {
    background: lightyellow;
}
</style>
    </head>
    <body>
        <p>
            Descendant selectors apply styles based on ancestral relationships.
            <span class='code'>&lt;span&gt;</span> element named <em>code</em>,
            which is a descendant of <span class='code'>&lt;p&gt;</span> elements.
            To do this, the selector <span class='code'>p span.code</span> is used.
        </p>
        <p class='note'>
            The note text is given different styles.  To do this another descendant
            selector is used.  This time the selector is <span class='code'>p.note
            span.code</span>
        </p>
    </body>
</html>

   
    
  








Related examples in the same category

1.Universal Selector
2.The Type Selector
3.The Class Selector
4.The ID Selector
5.The Child Selector
6.The Descendent Selector
7.The Adjacent Sibling Selector
8.Attribute Selectors
9.Using Attribute Selectors to select different textfields
10.Using Attribute Selectors to choose anchor by link type
11.Using Attribute Selectors to choose only mailto anchor type
12.Using Attribute Selectors to choose linked file type for anchor
13.Using Attribute Selectors to choose php links
14.Using Dynamic Pseudo-Classes to change color
15.Using Dynamic Pseudo-Classes to add text decoration
16.Dynamic Pseudo-Classes: hover effect
17.Selectors and Grouping
18.Class Selectors: A class represents something that you can have more than one of.
19.chain together class names within the class attribute.
20.An id is used but once per document.
21.Universal selectors are wildcard selectors.
22.Next Sibling Selectors
23.Attribute Substring Selectors
24.Pseudo-Element Selectors
25.Dynamic Pseudo-Class Selectors
26.Using Different Selectors to Apply Styles
27.First Child Pseudo-Selector
28.Before and After Pseudo Selectors
29.Selector Basics
30.Style sheet for example demonstrating adjacent sibling and child selectors
31.paragraph with id selector
32.Direct Child Selectors
33.A specific selector wins, the last one wins
34.Select one item
35.direct adjacent sibling combinator