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.
* {color: blue;
font-style: italic;}
In this example, all text in all elements will be coloured blue (except where overridden by more specific rules).
h1 {align:center;
color:blue;}
In this example, every Heading 1 element will be centre aligned and the font colour changed to blue.
h1, h2, h3, {font-size:14pt;
font-weight:bold;}
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;}
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.
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>
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;}
li > em {color: red;}
Using the same example as above, this would now produce the following results:
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.
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>
input[type] {background-color: black; color: white}
This would set any input control to have a black background with a white font colour.
input[type="text"] {background-color: black; color: white}
Here the style is only applied to input text boxes.
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".
*[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">
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-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 ....
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 {color: green;}
This sets the colour of an unvisited link to green.
a:visited {color: yellow; text-decoration: none}
This rule sets a visited link to yellow and removes any underlining.
a:hover {color: yellow; text-decoration: none}
This rule sets the link to yellow and removes any underlining.
a:active {color: yellow;}
This rule sets an active link to yellow.
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.
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.