Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
This chapter is from the book

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 circle-a.jpg.

  • 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:

  1. 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 circle-b.jpg. 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>
  2. 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.

  3. 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 {...}
  4. 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 {...}
  5. 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 {...}
  6. 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:

  • :hoverSame as for links, but sets the appearance of the element when the pointer is hovering over it.

  • :focusApplied to elements that can receive focus, such as form text fields.

  • :activeSame as for links, but sets the style of the element when it is clicked or selected.

To define a dynamic pseudo-class:

  1. 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) circle-c.jpg.

    <!-- 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>
  2. 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.

  3. 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).

  4. 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.

  5. 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 star.jpg

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-childSets the appearance of the first instance of a selector type if it is the first child of its parent.

  • :first-of-typeSets 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-childSets the appearance of the element of the indicated selector type if it is the last child of the parent.

  • :last-of-typeSets the appearance of the last instance of a particular selector type within its parent.

To style the children of an element:

  1. 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 circle-d.jpg.

    <!-- 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>
  2. 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:

  1. 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) circle-e.jpg.

    <!-- 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>
  2. 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 star.jpg

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:

  1. 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 circle-f.jpg.

    <!-- 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>
  2. 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.

  • + Share This
  • 🔖 Save To Your Account

Peachpit Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about Peachpit products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask@peachpit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by Adobe Press. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.peachpit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020