This site is designed for accessibility. Content is obtainable and functional to any browser or Internet device. This site's full visual experience is available in a graphical browser that supports web standards. Please consider upgrading your web browser.

Targeting selectors to accomplish precise tasks (part 1)

Assume the following markup, which cannot be changed in any way:

<body>
<h1>Hel<strong>lo</strong> there!</h1>
<p>Welcome to my Web site. I'm <strong>so</strong> glad
you're here! On this site you can find:
</p>
<ul>
<li><a href="mepics.html">Pictures of me</a>...
<ul>
<li>...with <a href="friendpics.html">friends</a></li>
<li>...with <a href="fampics.html">family</a></li>
<li>...with <a href="beerpics.html">beer</a></li>
</ul>
</li>
<li><a href="fluffpics.html">Pictures of my cat Fluffy</a></li>
<li><a href="poetry.html">My poetry</a>...
<ul>
<li>...written to <a href="seriouspoetry.html">be serious</a></li>
<li>...written to <a href="funnypoetry.html">be funny</a></li>
</ul>
</li>
<li><a href="favlinks.html">My <em>favorite</em> Web sites</a></li>
</ul>
<div class="rant">
<p>
The other day someone wrote telling my site was:
</p>
<ul>
<li>lame</li>
<li>stupid</li>
<li>narcissistic</li>
<li><em>really</em> lame</li>
</ul>
<p>
I <strong>hate</strong> that! Why do people do things like that?
After all, if you don't like the Web site, don't come here. I'm proud of
<a href="fluffpics.html">my cat</a> and there's nothing wrong with
naming a mynx Fluffy anyway.
</p>
<p>
So <em>there</em>.
</p>
</div>
<p>
Sorry about that little bit of ranting. I promise to be more cheerful in the
future. If you want, e-mail me at <a href="mailto:me@my.web.site">me@my.web.site</a>,
but <em>not</em> if you're going to complain.
</p>
<p>Have a nice day!</p>
<div class="footer">
All content copyright <a href="mepics.html">me</a>, on the date I
wrote it.
</div>
</body>

Example 1:
To put a border around the "rant" use:

div.rant {border : thin solid #000000;}

This selects any division with a class that contains the word "rant" and gives it a thin solid black border.

Example 2:
To italicize all of the em elements found in the "rant" use:

div.rant * em {font-style: italic;}

This selects any em element that is a descendant of any element that is a descendant of a division with a class attribute that contains the word "rant". and makes the text italic.

Example 3:
To make the word "really" in "really lame" red use:

div.rant p + ul > li > em {color : #ff0000;}

This selects any em that is a child of a list item element that is a child of an unordered list that immediately follows (is sibling to) a paragraph that is a descendant of a division with a class that contains the word "rant" and makes it red #ff0000.

Example 4:
To underline the word "there" in "So there" use:

div.rant > ul + p + p > em {text-decoration: underline;}

This selects any em that is a child of a paragraph that immediately follows (is sibling to) a paragraph that immediately follows (is sibling to) an unordered list that is a child of a division with a class that contains the word "rant" and makes the text underlined.

Example 5:
To boldface the mailto: link near the end of the page use:

h1 + p + ul + div.rant + p > a {font-weight: bold;}

This selects any "a" element that is a child of a paragraph that immediately follows (is sibling to) a division with a class that contains the word "rant" that immediately follows (is sibling to) an unordered list that immediately follows (is sibling to) a paragraph that immediately follows (is sibling to) a heading 1 and makes it bold.

Example 6:
To boldface the paragraph "Have a nice day!" use:

h1 + p + ul + div.rant + p + p {font-weight: bold;}

This selects any paragraph that immediately follows a paragraph that immediately follows (is sibling to) a division with a class that contains the word "rant" that immediately follows (is sibling to) an unordered list that immediately follows (is sibling to) a paragraph that immediately follows (is sibling to) a heading 1 element and makes it bold.

Example 7:
To set a left margin of 1.5em for the sub-category lists for both pictures and poetry use:

ul > li > ul li {margin-left: 1.5em;}

This selects any list item that is a descendant of an unordered list that is a child of a list item that is a child of an unordered list element and sets a left margin of 1.5em.

Example 8:
To set a left margin of 2em for only the poetry sub-category list use:

ul > li + li > ul {margin-left: 2em;}

This selects any unordered list that is a child of a list item that immediately follows (is sibling to) a list item that is a child of an unordered list and sets a left margin of 2em.

Example 9:
To uppercase the word "beer" use:

ul > li > ul > li + li + li > a {text-transform: uppercase;}

This selects any a element that is a child of a list item that immediately follows (is sibling to) a list item that immediately follows (is sibling to) a list item that is a child of an unordered list that is a child of a list item that is a child of an unordered list and transforms the text to uppercase.

Example 10:
Set a square bullet for the last three category list items near the beginning of the page, without having that change carry into the sub-category lists.

body > ul > li + li {list-style-type: square;}

This selects any list item that immediately follows a list item that is a child of an unordered list that is a child of a body element and sets the list style type to square.