- 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
Working with Pseudo-classes
Many HTML elements have special states or uses associated with them that can be styled independently. One prime example of this is the link tag, <a>, which has link (its normal state), a visited state (when the visitor has already been to the page represented by the link), hover (when the visitor has their mouse over the link), and active (when the visitor clicks the link). All four of these states can be styled separately.
A pseudo-class is a predefined state or use of an element that can be styled independently of the default state of the element .
- Links (Table 4.3)—Pseudo-classes are used to style not only the initial appearance of the anchor tag, but also how it appears after it has
been visited, while the visitor hovers their mouse over it, and when visitors are clicking it.
Table 4.3 Link and Dynamic Pseudo-Classes
Format
Name
Elements Are Styled If...
Compatibility
:link
Link
the value of href is not in history
IE4, FF1, O3.5, S1, CSS1
:visited
Visited Link
the value of href is in history
IE4, FF1, O3.5, S1, CSS1
:target
Targeted Link
a targeted anchor link
FF1.3, S1.3, C1, O9.5 CSS3
:active
Active
the element is clicked
IE7, FF1, O3.5, S1, CSS1
:hover
Hover
the pointer is over the element
IE4*, FF1, O3.5, S1, CSS2
:focus
Focus
the element has screen focus
IE7, FF1, O7, S1, CSS2
- Dynamic (Table 4.3)—Pseudo-classes can be applied to any element to define how it is styled when the user hovers over it, clicks it, or selects it.
- Structural (Table 4.4)—Pseudo-classes are similar to the sibling combinatory selectors but allow you to specifically style elements based on an
exact or computed numeric position.
Table 4.4 Structural/Other Pseudo-Classes
Format
Name
Elements Are Styled If...
Compatibility
:root
Root
is the top level element in a document
FF1.5, O9.5, S3.1, C3, CSS3
:empty
Empty
has no children
FF1.5, O9.5, S3.1, C3, CSS3
:only-child
Only Child
has no siblings
FF1.5, O9.5, S3.1, C3, CSS3
:only-of-type
Only of Type
has its unique selector among its siblings
FF1.5, O9.5, S3.1, C3, CSS3
:first-child
First-Child
is the first child of another element
FF1.5, O9.5, S3.1, C3, CSS2
:nth-of-type(n)
Nth of Type
is the nth element with that selector
FF1.5, O9.5, S3.1, C3, CSS3
:nth-last-of-type(n)
Nth From Last of Type
is the nth element with that selector from the last element with that selector
FF1.5, O9.5, S3.1, C3, CSS3
:last-child
Last Child
is the last child in the parent element
FF1.5, O9.5, S3.1, C3, CSS3
:first-of-type
First of Type
is the first of its selector type in the parent element
FF1.5, O9.5, S3.1, C3, CSS3
:last-of-type
Last of Type
is the last of its selector type in the parent element
FF1.5, O9.5, S3.1, C3, CSS3
:lang()
Language
has a specified language code defined
IE8, FF1.5, O9.5, S3.1, C3, CSS2.1
:not(s)
Negation
is not using specific selectors
FF1.5, O9.5, S3.1, C3, CSS3
- Other (Table 4.4)—Pseudo-classes are available to style elements based on language or based on what tag they are not.
Styling links
Although a link is a tag, its individual states are not. To set properties for these states, you must use the pseudo-classes associated with each state that a link can have (in this order):
- :link lets you declare the appearance of hypertext links that have not yet been selected.
- :visited lets you set the appearance of links that the visitor selected previously—that is, the URL of the href attribute in the tag is part of the browser’s history.
- :hover lets you set the appearance of the element when the visitor’s pointer is over it.
- :active sets the style of the element when it is clicked or selected by the visitor.
For ideas on which styles to use with links, see the sidebar “Picking Link Styles.”
To set contrasting link appearances:
- Style the anchor tag.
a {...}
Although not required, it’s best to first define the general anchor style (Code 4.6). This differs from setting the :link pseudo-class in that these styles are applied to all the link pseudo-classes. So, you want to declare any styles that will remain constant or are changed in only one of the states.
Code 4.6. The link styles are set for the default and then all four link states, creating color differentiation . Notice also that I’ve turned off underlining with text decoration but added an underline effect using border bottom.
<!-- 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">
a {
display: block;
text-decoration: none;
padding: 5px;
width: 200px; }
a:link {
color: rgb(255,102,102);
border-bottom: 1px dotted rgb(255,51,5,51); }
a:visited {
color: rgb(255,153,153);
border-bottom: 1px dotted rgb(255,235,235); }
a:hover {
color: rgb(255,0,0);
border-bottom: 1px solid rgb(255,0,0); }
a:active {
color: rgb(0,0,255);
border-bottom: 1px dotted rgb(102,102,102); }
</style> </head> <body> <navigation> <a href=""><strong>Chapter 1. </strong>Down The Rabbit-Hole</a> <a href=""><strong>Chapter 2. </strong>The Pool of Tears</a> <a href=""><strong>Chapter 3. </strong>A Caucus-Race and a Long Tale</a> <a href=""><strong>Chapter 4. </strong>The Rabbit Sends in a Little Bill</a> <a href=""><strong>Chapter 5. </strong>Advice from a Caterpillar</a> <a href=""><strong>Chapter 6. </strong>Pig and Pepper</a> <a href=""><strong>Chapter 7. </strong>A Mad Tea-Party</a> <a href=""><strong>Chapter 8. </strong>The Queen's Croquet-Ground</a> <a href=""><strong>Chapter 9. </strong>The Mock Turtle's Story</a> <a href=""><strong>Chapter 10. </strong>The Lobster Quadrille</a> <a href=""><strong>Chapter 11. </strong>Who Stole The Tarts?</a> <a href=""><strong>Chapter 12. </strong>Alice's Evidence</a> </navigation> </body> </html> - Style the default link state. Type the selector (anchor tag, class, or ID) of the element you want to style, followed by a colon (:), and then link.
a:link {...}
You can override styles set for the anchor tag, but this rule should always come before the :visited pseudo-class.
- Style the visited link style. Type the selector (anchor, class, or ID) of the element you want to style, followed by a colon (:), and then visited.
a:visited {...}
- Style the hover link state. Type the selector (anchor, class, or ID) of the element you want to style, followed by a colon (:), and then hover.
a:hover {...}
- Style the active link state. Type the selector (anchor, class, or ID) of the element you want to style, followed by a colon (:), and then active.
a:active {...}
- Style is applied to the link state as needed.
<a href="chapater01.html">...</a>
All links on the page will obey the rules you lay down here when styling the various link states. You can—and should—use selective styling to differentiate link types.
Styling for interaction
Once loaded, Web pages are far from static. Users will start interacting with the page right away, moving their pointers across the screen and clicking hither and yon. The dynamic pseudo-classes allow you to style elements as the user interacts with them, providing visual feedback:
-
:hover—Same as for links, but sets the appearance of the element when the pointer is hovering over it.
-
:focus—Applied to elements that can receive focus, such as form text fields.
-
:active—Same as for links, but sets the style of the element when it is clicked or selected.
To define a dynamic pseudo-class:
- Style the default element.
input {...}
Although optional, it’s generally a good idea to set the default, nondynamic style for the elements receiving dynamic styles (Code 4.7).
Code 4.7. The input elements are set to change style when the user interacts with them by hovering, selecting (focus), or clicking (active) .
<!-- 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">
input {
border: 3px solid rgb(153,153,153);
background-color: rgb(204,204,204);
color: rgb(153,153,153);
padding: 0 5px;
font-size: 2em; }
input:hover {
border-color: rgb(204,153,153);
color: rgb(102,102,102); }
input:focus {
border-color: rgb(255,0,0);
background-color: rgb(255,255,255);
color: rgb(0,0,0);
outline: none; }
input:active {
color: rgb(255,0,0);
border-color: rgb(255,0,0);
background-color: rgb(0,0,0); }
</style> </head> <body> <form> <input type="text" value="First Name"> <input type="text" class="hover" value="Last Name"> <input type="text" class="focus" value="eMail"> <input type="button" class="active" value="Search"> </form> </body> </html> - Style the hover state of the element. Type the selector (HTML, class, or ID), a colon (:), and then hover.
input:hover {...}
As soon as the pointer enters the element’s box (see Chapter 10 for details about the box model), the style change will occur.
- Style the focus state of the element. Type the selector (HTML, class, or ID), a colon (:), and then focus.
input:focus {...}
As soon as the element receives focus (is clicked or tabbed to), the style change occurs and then reverts to the hover or default style when the element loses focus (called blur).
- Style the active state of the element. Type the selector (HTML, class, or ID), a colon (:), and then active.
input:active {...}
As soon as the user clicks within the element’s box (explained in Chapter 10), the style change will occur and then revert to either the hover or default style when released.
- The styles are applied to the elements’ states as necessary in reaction to the user.
<input type="button" value="Search">
All the tags using the specific selector will have their states styled.
NEW IN CSS3: Styling specific children with pseudo-classes
Designers often want to apply a style to an element that is the first element to appear within another element, such as a parent’s first child.
The first-child pseudo-element has been available since CSS2; however, CSS3 offers an assortment of new structural pseudo-elements for styling an element’s child element exactly (Table 4.4):
-
:first-child—Sets the appearance of the first instance of a selector type if it is the first child of its parent.
-
:first-of-type—Sets the appearance of an element the first time its selector type appears within the parent.
-
:nth-child(#)—Sets the appearance of the specific occurrence of the specified child element. For example, the third child element of a paragraph would be p:nth-child(3).
-
:nth-of-type(#)—Sets the appearance of the specific occurrence of a selector type within the parent. For example, the seventh paragraph would be p:nth-of-type(7).
-
:nth-last-of-type(#)—Sets the appearance of the specific occurrence of a selector type within the parent, but from the bottom. For example, the third paragraph from the bottom would be p:nth-last=of-type(3).
-
:last-child—Sets the appearance of the element of the indicated selector type if it is the last child of the parent.
-
:last-of-type—Sets the appearance of the last instance of a particular selector type within its parent.
To style the children of an element:
- Style the children based on their positions in the parent. Type the selector (HTML, class, or ID) of the element you want to style, a colon (:), and one of the structural pseudo-elements from Table 4.4 (Code 4.8).
li:first-child {...} li:first-of-type {...} li:nth-of-type(3) {...} li:nth-last-of-type(2) {...} li:last-child {...} li:last-of-type {...}
Code 4.8. The list has styles set for it based on location within the list .
<!-- 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">
li:first-child { font-size: .875em; }
li:first-of-type { color: red; }
li:nth-of-type(3) { font-size: 1.5em }
li:nth-last-of-type(2) { font-size: 2em; }
li:last-of-type { color: red; }
li:last-child { font-size: 2.5em; }
</style> </head> <body> <ol> <li>Alice</li> <li>The White Rabbit</li> <li>The Mad Hatter</li> <li>The Queen of Hearts</li> <li>The Door Mouse</li> </ol> </body> </html> - Elements will be styled if they match the pattern.
<li>...</li>
Set up your HTML with the selectors from step 1 in mind.
Styling for a particular language
The World Wide Web is just that, all around the world, which means that anyone, anywhere can see your pages. It also means that Web pages are created in many languages.
The :lang() pseudo-class lets you specify styles that depend on the language specified by the language property.
To set a style for a specific language:
- Style an element based on its language code. Type the selector (HTML, class, or ID) of the element you want to style, a colon (:), lang, and enter the letter code for the language you are defining within parentheses (Code 4.9).
p:lang(fr) {...}
Code 4.9. Styles are set to turn paragraphs red if they are in French (fr) .
<!-- HTML5 --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Alice's Adventure's in Wonderland</title> <style type="text/css" media="all">
p:lang(fr) {
color: red;
font-style: italic; }
</style> </head> <body> <p>It sounded an excellent plan,...</p><p lang="fr">On aurait dit un excellent plan,...</p>
</body> </html> - The element is styled if it has a matching language code. Set up your tag in the HTML with the language attributes as necessary.
<p lang="fr">...</p>
If the indicated selector has its language attribute equal to the same value that you indicated in parentheses in step 1, the style is applied.
NEW IN CSS3: Not styling an element
So far you’ve looked at ways to style a tag if it is something. The negation selector, :not, allows you to not style something for a particular selector.
To not set a style for a particular element:
- Style elements to exclude certain selectors. Type the selector (HTML, class, or ID) of the element you want to style, a colon (:), not, and enter the selectors you want excluded from this rule in parentheses (Code 4.10).
p:not(.dialog) {...}
Code 4.10. If the element is a paragraph that does not use the dialog class, it will be displayed in red and italics .
<!-- 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:not(.dialog) {
color: red;
font-style: italic; }
</style> </head> <body> <p class='dialog'>"Why?" said the Caterpillar.</p> <p>Here was another puzzling question,...</p> <p class='dialog'>"Come back!" the Caterpillar,..."</p> </body> </html> - The element is not styled if it contains the indicated selector.
<p class='dialog'>...</p><p>...</p>
The styles are applied to elements that match the initial selector but not the selector in parentheses.