A web designer might want to assign different styles to the same tags. For this reason, the CSS specifications introduced the concept of classes.
The definition of classes is as simple as that of styles. It involves specifying a selected tag (as with declaring the style), then adding a decimal point (.) and the name that you want to give the class. The name of the class may be composed of letter, digits, and dashes:
Tag_selector.Name-of-class {
style property: Value;
style property: Value;
...;
}
Where "Name-of-class" represents the name given to the class.
To invoke a class within the HTML code, just add the class attribute to a tag:
For the class red applied to tag b:
B.red {font: Verdana 12px; color: #FF0000; } To invoke this class within the code, use the following syntax: <B class="red">Text that will be red and in bold</B>
It is possible to not specify a tag, in which case the class may be used in any tag in which the style selected has meaning. These are called universal classes (sometimes independent classes). Such a class is defined by putting a decimal point before the name of a class:
.Name-of-class {style property: Value; style property: Value; ...}
For the class "major":
.major {font-type: arial; color: red; font-weight: bold}
Invoking this class can be done using any HTML element in which the definition is valid:
<h1 class="major">This is a warning</h1>
<i class="major">(Please read carefully)</i>
![]() |
Note the absence of a decimal point when invoking the class. |
Pseudo-classes are used to further improve the style applied to certain tags, by defining how they react to an event or their relative position within other tags.
Unlike classes, the names of pseudo-classes are predefined, so it is impossible to create your own pseudo-classes. There are several types of pseudo-classes :
Dynamic pseudo-classes are used to change the style of a tag when an event occurs (such as moving the mouse, clicking, or pressing a key). There are three of them:
A:hover {font-decoration: underline;}
TEXTAREA:focus {color: #FF0000;}
A:active {color: #FF0000;}
![]() |
Dynamic pseudo-classes are not rendered the same way by all browsers. |
Link pseudos-classes are pseudo-classes used with the <A> tag:
![]() |
Some browsers might consider a link to be unvisited if it has not been clicked on for a long period of time. |
A descendant pseudo-class is used to apply a style to the first tag within an element. This pseudo-class's syntax is as follows:
Parent_Element > Tag:first-child {style;} So the following declaration applies to the first tag within a<P> </P> set of tags, if that first tag is <A>: P > A:first-child {color: #00FF00;} Thus, this <A> tag will have the style: <P align="justify">This next <A> tag, on the other hand, will not, as it is not the first tag after the P tag <P>:
<A href="http://en.kioskea.net/">CCM</A>
</P>
<P align="justify">
<BR/>
<A href="http://en.kioskea.net/">CCM</A>
</P>
Text pseudo-classes are used to apply a style to a portion of text delimited by the appropriate tags. For this reason, text pseudo-classes are generally used in conjunction with the paragraph tag (<P>).
There are two text pseudo-classes:
P:first-line { text-transform: uppercase }
P:first-letter { font-size: 200%; font-weight: bold; }
A style can be defined based on the document's language (specified in the HTTP header or the metatags) or the language of an HTML or XML element (specified with the optional attribute LANG) if it is given.
A language pseudo-class uses the following notation:
HTML:lang(fr) { quotes: '« ' ' »' }The selector @page is used to change the layout settings of an HTML page when the page is printed, such as the margins (margin-left, margin-top, margin-right, margin-bottom) or the size (size). To do so, the web page must be imagined as a series of pages in a printed work.
The page pseudo-classes are used to select left-hand and right-hand pages, or the first page in a document.
There are different page pseudo-classes:
@page:left { size: landscape; margin-left: 2cm; }@page:right { size:landscape; margin-left: 2.5cm; }@page:first { size: portrait;
margin-left: 2.5cm;
margin-right: 2cm;
margin-bottom: 1cm;
margin-top: 4cm;}ID selectors (identifiers) are used to reference a single page element as indicated by its identifier. IDs are mainly used to isolate HTML elements with JavaScript.
An ID selector's syntax is:
#nom_ID { style }
This style is called in the following manner:
<BALISE ID="nom_ID" > ... </BALISE>
There can be only one ID per page! Also, note the absence of the # when invoking the ID selector.