- The Element Family Tree
- Defining Styles Based on Context
- Working with Pseudo-classes
- Working with Pseudo-elements
- Defining Styles Based on Tag Attributes
- NEW IN CSS3: Querying the Media
- Inheriting Properties from a Parent
- Making a Declaration !important
- Determining the Cascade Order
- Putting It All Together
Defining Styles Based on Context
Contextual styles allow you to specify how a particular element should appear based on its parents and siblings. For example, you may want an emphasis tag to appear one way when it’s in the main header of the page and differently when it appears in the sub-header. You may want still another appearance in a paragraph of text. These combinatory selectors (Table 4.1) are among the most used and useful CSS.
Table 4.1 Combinator Selectors
Format |
Selector Name |
Elements Are Styled If... |
Compatibility |
a b c |
Descendent |
c descendent of b descendent of a |
IE4, FF1, O3.5, S1, C1, CSS1 |
a * b |
Universal |
b within a regardless of b’s parents |
IE7, FF1, O4, S1, C1, CSS2 |
a > b |
Direct Child |
b direct child of a |
IE7 FF1, O3.5, S1, C1, CSS1 |
a + b |
Adjacent Sibling |
sibling b immediately after a |
IE7, FF1, O5, S1, C1, CSS2 |
a ~ b |
General Sibling |
sibling b anywhere after a |
IE8, FF1, O5, S1, C1, CSS2 |
Styling descendents
You can style individual descendent elements depending on their parent selector or selectors in a space-separated list. The last selector will receive the style if and only if it is the descendent of the preceding selectors .
When you want to indicate that the exact selector does not matter at any given level, you can use the universal selector (*) described in Chapter 3 .
To style descendent elements:
- Set up a list of descendent selectors. Type the HTML selector of the parent tag, followed by a space, and then the final child or another parent (Code 4.1).
article.copy h1 em {...}
You can type as many HTML selectors as you want for as many parents as the nested tag will have, but the last selector in the list is the one that receives all the styles in the rule.
- Styles will be used if the pattern is matched.
<article class="copy"><h1><em>...</em></h1></article>
The style will be applied if and only if the final selector occurs as a descendent nested within the previous selectors.
Code 4.1. The style is set for the emphasis tag if its parents are the h1 tag and the article tag using the copy class .
<!-- HTML5 --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Alice's Adventures in Wonderland</title> <style type="text/css" media="all">article.copy h1 em {
color: red;
font-weight: bold;
font-style: italic;
} </style> </head> <body> <article class="copy"> <h1>Alice's Adventures in <em>Wonderland</em></h1> <h2><em>Chapter 2.</em> The Pool of Tears</h2> <p>'Curiouser and curiouser!' <em>cried</em> Alice,...</p> <p>And she went on <em>planning</em>,...</p> <p>Poor <em>Alice!</em></p> <blockquote>ALICE'S RIGHT FOOT, <em>ESQ.</em></blockquote> <p>Oh dear, what <em>nonsense</em> I'm talking!',...</p> </article> </body> </html>
To style descendents universally:
- Set up a list of descendent selectors including a universal selector. Type the HTML selector of the parent tag, followed by a space, and then an asterisk (*) or other selectors (Code 4.2).
article.copy * {...}
- Styles will be used if the pattern is matched. Generally, the universal selector is used at the end of a list of selectors so that the style is applied to all of a parent’s
children.
<article class="copy"><h1>Alice's Adventures in <em>Wonderland</em></h1><h2><em>Chapter 2.</em> The Pool of Tears</h2><p>...<em>...</em>...</p></article>
Code 4.2. The style is set for the emphasis tag with any parent that’s in an article tag using the copy class .
<!-- HTML5 --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Alice's Adventures in Wonderland</title> <style type="text/css" media="all">article.copy * em {
color: red;
font-weight: bold;
} </style> </head> <body> <article class="copy"> <h1>Alice's Adventures in <em>Wonderland</em></h1> <h2><em>Chapter 2.</em> The Pool of Tears</h2> <p>'Curiouser and curiouser!' <em>cried</em> Alice,...</p> <p>And she went on <em>planning</em>,...</p> <p>Poor <em>Alice!</em></p> <blockquote>ALICE'S RIGHT FOOT, <em>ESQ.</em></blockquote> <p>Oh dear, what <em>nonsense</em> I'm talking!',...</p> </article> </body> </html>
Styling only the children
If you want to style only a parent’s child elements (not a grandchild descendent), you must specify the parent selector and child selector, separated by a close angle bracket (>) .
To define child selectors:
- Set up a list of direct child selectors. Type the selector for the parent element (HTML, class, or ID), followed by a right angle bracket (>) and the child selector
(HTML, class, or ID).
article.copy > p > em {...}
You can repeat this as many times as you want with the final selector being the target to which you apply the styles (Code 4.3). You can have one space between the selector and the greater-than sign or no spaces.
- Styles will be used if the pattern is matched.
<article class="copy"><p>...<em>...</em>...</p></article>
The styles from step 1 are applied if and only if the final selector is an immediate child element nested in the preceding element. Placing the tag within any other HTML tags will disrupt the pattern.
Code 4.3. The style is applied to the emphasis tag only if it is a child of a paragraph that is in turn the child of an article tag using the copy class .
<!-- HTML5 --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Alice's Adventures in Wonderland</title> <style type="text/css" media="all">article.copy > p > em {
color: red;
font-weight: bold;
} </style> </head> <body> <article class="copy"> <h1>Alice's Adventures in <em>Wonderland</em></h1> <h2><em>Chapter 2.</em> The Pool of Tears</h2> <p>'Curiouser and curiouser!' <em>cried</em> Alice,...</p> <p>And she went on <em>planning</em>,...</p> <p>Poor <em>Alice!</em></p> <blockquote>ALICE'S RIGHT FOOT, <em>ESQ.</em></blockquote> <p>Oh dear, what <em>nonsense</em> I'm talking!',...</p> </article> </body> </html>
Styling siblings
Siblings are elements that have the same parent. You can style a sibling that is immediately adjacent to another or occurs anywhere after that sibling .
To define adjacent sibling selectors:
- Set up a list of adjacent sibling selectors. Type the selector for the first element (HTML, class, or ID), a plus sign (+), and then the selector (HTML, class, or ID)
for the adjacent element to which you want the style applied (Code 4.4).
p + p {...}
- Styles will be used if the pattern is matched.
<p>...</p><p>...</p><p>...</p>
The styles will be applied to any sibling that occurs immediately after the preceding selector with no other selectors in the way. Placing any element between them (even a break tag) will disrupt the pattern.
Code 4.4. The style is applied to the emphasis tag only if it is in a paragraph that is immediately after another paragraph .
<!-- HTML5 --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Alice's Adventures in Wonderland</title> <style type="text/css" media="all">p + p em {
color: red;
font-weight: bold;
} </style> </head> <body> <article class="copy"> <h1>Alice's Adventures in <em>Wonderland</em></h1> <h2><em>Chapter 2.</em> The Pool of Tears</h2> <p>'Curiouser and curiouser!' <em>cried</em> Alice,...</p> <p>And she went on <em>planning</em>,...</p> <p>Poor <em>Alice!</em></p> <blockquote>ALICE'S RIGHT FOOT, <em>ESQ.</em></blockquote> <p>Oh dear, what <em>nonsense</em> I'm talking!',...</p> </article> </body> </html>
To define general sibling selectors:
- Set up a list of general sibling selectors. Type the selector for the first sibling element (HTML, class, or ID), a tilde sign (~), and then another selector (HTML, class, or ID) (Code 4.5).
p ~ p {...}
You can repeat this as many times as necessary, but the last selector in the list is the one you are targeting to be styled.
- Styles will be used if the pattern is matched.
<p>...</p><p>...</p><p>...</p><blockquote>...</blockquote><p>...</p>
Code 4.5. The style is applied to the emphasis tag if it is in a paragraph with any preceding sibling that is a paragraph .
<!-- HTML5 --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Alice's Adventures in Wonderland</title> <style type="text/css" media="all">p ~ p em {
color: red;
font-weight: bold;
} </style> </head> <body> <article class="copy"> <h1>Alice's Adventures in <em>Wonderland</em></h1> <h2><em>Chapter 2.</em> The Pool of Tears</h2> <p>'Curiouser and curiouser!' <em>cried</em> Alice,...</p> <p>And she went on <em>planning</em>,...</p> <p>Poor <em>Alice!</em></p> <blockquote>ALICE'S RIGHT FOOT, <em>ESQ.</em></blockquote> <p>Oh dear, what <em>nonsense</em> I'm talking!',...</p> </article> </body> </html>