Introduction Basics Selectors Values Cascade Rules CSS Box Model Margins, Padding & Borders Float Example Position Example Further Reading Site Home

CSS2 Reference Guide

CSS2 Selectors

Selectors

There are a variety of ways in which you can specify the selector in a rule declaration. Not all of these are currently supported by the major browsers so they may or may not work in yours, but we've cheated a little to show them as they should work. Those indicated by an * are not supported by Internet Explorer 6.

Universal selector
This is used to apply a style to all elements.
* {color: blue;
   font-style: italic;}

In this example, all text in all elements will be coloured blue (except where overridden by more specific rules).

Element selector
This type of selector is used to select a particular element.
h1 {align:center;
    color:blue;}

In this example, every Heading 1 element will be centre aligned and the font colour changed to blue.

Group selector
Selectors which have the same declarations can be grouped together.
h1, h2, h3, {font-size:14pt;
             font-weight:bold;}
Class selector
This selector comprises an element which is qualified by the value of a class attribute which has been specified for that element.

For example, if a paragraph element has a class attribute of "banana", <p class="banana">, the following style rule would be applied to all <p> elements which have that class attribute value.

p.banana {color: yellow;}

If the element is not specified in the selector, then the rule will be applied to any element which has been assigned that class. The class name must be preceeded by the period (.).

.banana {color: yellow;}
Id selector
An id selector is where the selector matches the value of an id attribute specified for an element. An id value is unique and may only be specified once in a document.
p#para1 {color: blue;}

The paragraph specified as <p id="para1"> would be selected by this rule.

The id value must be preceded by a hash (#) sign. In practice, the "p" need not be included before the # sign as the id is a unique value. However it is useful for anyone looking at the style sheet to show that this rule is being applied to an instance of that element.

Descendant Selector
This is used to select all elements which are the descendant of another, that is, they are nested within the other element's tags. It covers any relationship, from child to great, great ... grandchild.
li em {color: red;}

This rule will be applied to all <em> elements which are in a list. For example:

<p>In this list of items, some are <em>italicised</em> and <span style="color: red;">red</span>.
</p>
<ul>
<li>Item1</li>
<li><em>Item2</em></li>
<li><span style="font-size: 1.5em;"><em>Item3</em></span></li>
</ul>
In this list of items, some are italicised and red.
  • Item1
  • Item2
  • Item3

Further elements can be added to the selector to limit the relationship. In the above example, the rule can be made to apply to the third item only by introducing the <span> element into the hierarchy.

li span em {color: red;}

This type of selector can be combined with an id selector to create a rule which is only applied to descendants of an element with a particular id. For example:

li#list1 em {color: red;}

This would only be applied to any <em> element within a <li> element which has an id of "list1". This particular example is a little restricted as it can only ever apply to the one list item. However by introducing a <ul> element and assigning the id to that, we can make the rule apply to the whole of the particular list in question.

ul#list1 li em {color: red;}
Child selector *
This is similar to the Descendant selector but in this case the relationship only extends to a direct child element and no further.
li > em {color: red;}

Using the same example as above, this would now produce the following results:

This is a list of items, some are italicised but not red.
  • Item1
  • Item2
  • Item3

The third item is no longer displaying red, as <em> on this line is a child descendant of <span> not <li>. Spaces around the '>' sign are optional.

Adjacent sibling selector *
This selector is concerned with the sequence of elements. The rule is applied to an element which immediately follows another in a document.
h5 + h6 {color: red;}

In this example the rule will only be applied to <h6> elements which immediately follow an <h5> element. Spaces around the '+' sign are optional.

<h5>This is a heading 5</h5>
<h6>This heading 6 follows a heading 6</h6>
<h6>This heading 6 follows a heading 6</h6>
This is a heading 5
This heading 6 follows a heading 5
This heading 6 follows a heading 6
Attribute selector *
Rules can also be applied by matching on attributes. There are four ways on which this can be done.
element[attribute]
This matches when the specified attribute is set for that element regardless of its value.
input[type] {background-color: black; color: white}

This would set any input control to have a black background with a white font colour.

element[attribute="value"]
This matches when the specified element's attribute is set to a particular value for that attribute.
input[type="text"] {background-color: black; color: white}

Here the style is only applied to input text boxes.

element[attribute~="value"]
This matches when the specified value is any one of a space-separated list of "words" for that attribute.
img[alt~="Figure"] { border-color: white}

would match all of the following except the last example.

<img src="myImage1.gif" alt="Figure 1">
<img src="myImage2.gif" alt="Figure 2">
<img src="myImage1.gif" alt="1st Figure">
<img src="myImage3.gif" alt="Fig 3">

This is useful for matching on classes. A class name may consist of multiple words, e.g. class="first para only".

element[attribute|="value"]
This is a little similar to the previous selector but will select the element where the specified attribute value matches the beginning "word" of a hyphen separated list. The "word" may be on its own or immediately followed by a hyphen and subsequent text.
*[img|="Figure"] {border-color: white}

would match the first three examples but not the last three.

<img src="myImage1.gif" alt="Figure-1">
<img src="myImage2.gif" alt="Figure-2">
<img src="myImage1.gif" alt="Figure">
<img src="myImage3.gif" alt="Figure 3">
<img src="myImage3.gif" alt="Figure-">
<img src="myImage3.gif" alt="Figure -3">

It is particularly useful for the lang attribute.

*[lang|="en"] {font-style: italic;}

would match the following:

<body lang="en">
<body lang="en-uk">
Pseudo-Element selector
Pseudo-element selectors are so called because they don't refer to an actual HTML element. They refer to a specific part or parts of the document.
:first-line
This selector refers to the first line of text in an element.

For instance, applying the following rule to this current <dd> element, which has an id of "first" results in the first line of each paragraph being italicised as can be seen.

dd#first p:first-line  {font-style: italic;}

In some browsers this may be interpreted as the first sentence. Other factors such as font-size and the width also come into play.

As can be seen in this example, pseudo selectors may be combined with other selector types.

:first-letter
This is similar to :first-line but refers to the first letter of an element.
p:first-letter { font: bold italic 200% "Times New Roman"; color: blue;}

If applied to the above line would render it as follows:

This is similar ....

Pseudo-Class selector
These are used to match an element based on some characteristic of that element.
:first-child *
This selector refers to an element which is the first child of its parent element.
p.fchild strong:first-child {font-style: italic}

This would render the text within <strong> tags emphasised in paragraphs with a class of fchild providing that the <strong> element is the first element in that paragraph.

<p class="fchild"><strong>This</strong> will be italicised but <strong>this</strong> will not</p>

This will be italicised but this will not.

a:link
This pseudo-class can be used to apply styles to a unvisited hyperlink.
a:link {color: green;}

This sets the colour of an unvisited link to green.

a:visited
This applies styles to a hyperlink that has already been visited.
a:visited {color: yellow; text-decoration: none}

This rule sets a visited link to yellow and removes any underlining.

a:hover
This is used to apply styles to a hyperlink when the mouse cursor is placed over the link but the mouse button not clicked.
a:hover {color: yellow; text-decoration: none}

This rule sets the link to yellow and removes any underlining.

a:active
This applies styles to an active link. This is the time between when the mouse button is pressed and is released.
a:active {color: yellow;}

This rule sets an active link to yellow.

element:focus *
This applies styles to the specified element if it is in focus.
input:focus {background-color: yellow; color: blue}

This rule sets the background colour of the input control which has focus to yellow with a blue font colour.

A Word of Warning

The order in which the anchor selectors appears is important. The a:link must appear first so that it does not override any special effect set by the special action of the others. Also a:visited should precede a:hover so that any hover effect will still be seen on a link already visited. Similarly, the a:active selector should appear after any a:hover selector. You have to hover to activate! There is a mnemonic to help remember the order of these selectors - LoVe HAte.

Top

Valid XHTML 1.0! Valid CSS2