eHow launches Android app: Get the best of eHow on the go.

How To

How to Use CSS Descendant Selectors

Contributor
By Virginia DeBolt
eHow Contributing Writer
(3 Ratings)
This page has two different link styles and two different list styles.
This page has two different link styles and two different list styles.

Understanding CSS descendant selectors can help you target style rules for an exact area of your design.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • A basic understanding of HTML
  • A basic understanding of CSS

    It's all in the CSS syntax

  1. Step 1
    A simple HTML document tree
    A simple HTML document tree

    A web page is organized in a hierarchy. The document hierarchy begins with html and descends through a tree structure that could include many elements within the body. Here's a simple tree diagram.

  2. Step 2

    In the document tree, elements descend from the parent element that encloses them. In the example, there are lists that are the descendant elements of either the content div or the sidebar div. Furthermore, the lists are the ancestors of an A element (a hypertext link).

  3. Step 3

    The CSS syntax for a descendant selector is to list the selectors separated by a space, (but not a comma). For example:

    #content ul a {rule here}

    This selector targets an A element that is contained within a UL element that is contained within the CONTENT div. Other A elements on the page, perhaps one in a paragraph or in the SIDEBAR div, would not be affected by this selector.

  4. Step 4

    To style a list or list item in the sidebar, use a descendant selector like this:

    #sidebar ul {rule here}

    or

    #sidebar li {rule here}

    To style a hyperlink in the sidebar that is not nested in a list, use selectors like this:

    #sidebar a:link {rule here}
    #sidebar a:visited {rule here}

  5. Step 5

    To make P elements appear differently in the content area and the sidebar area, use descendant selectors like this:

    #content p {rule here}
    #sidebar p {rule here}

  6. Step 6

    Descendant selectors can also be based with classes. For example, if every post on your blog was assigned to the class "blogpost" you could write a descendant selector for an em (emphasized) element in an h3 element that was part of a div in the class blogpost like this:

    h3.blogpost em {rule here}

  7. Step 7

    Descendant selectors can become quite lengthy when you are targeting a very specific element on a complex page. For example:

    #blogpost blockquote p em {rule here}

    It is the ability to get completely focused on one and only one element on a page using descendant selectors that give you the power to style your page in any way you desire.

Tips & Warnings
  • Using descendant selectors can eliminate the need to assign a lot of classes to page elements. For example, you could make a CSS rule for a class called .sidetext and assign that class to every individual paragraph you put in a sidebar. However, you could eliminate every one of those class assignments in the HTML using a single rule for the selector #sidebar p.
Resources

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Internet
Virginia DeBolt,

Meet Virginia DeBolt eHow's Internet Expert.

Get Free Internet Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

Demand Media
eHow_eHow Technology and Electronics