Le classi di stile (CSS) As classes de estilo (CSS) Die Stilklassen (CSS) Les classes de style (CSS) Clases de estilos (CSS)

Classes and IDs

Classes

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.

Invoking a 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>

Universal classes

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: Note the absence of a decimal point when invoking the class.

Pseudo-classes

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

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:

  • The pseudo-class :hover is used to apply a style to a selected tag when the mouse cursor hovers over the text:
    A:hover {font-decoration: underline;}
  • The pseudo-class :focus is used to put a selected tag into a certain style when it is being focused on (such as when an element in a form is clicked on):
    TEXTAREA:focus {color: #FF0000;}
  • The pseudo-class :active is used for putting a selected tag into a certain style when the user clicks on the element (between the moment the user clicks on the mouse button and the moment they release it):
    A:active {color: #FF0000;}
Note: Dynamic pseudo-classes are not rendered the same way by all browsers.

Link pseudo-classes

Link pseudos-classes are pseudo-classes used with the <A> tag:

  • The pseudo-class :link is used for defining the style of hyperlinks which the user has not yet clicked on.
  • The pseudo-class :visited is used to define the style of hyperlinks which the client has already visited.
Note: Some browsers might consider a link to be unvisited if it has not been clicked on for a long period of time.

The descendant pseudo-class

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">
<A href="http://en.kioskea.net/">CCM</A>
</P>
This next <A> tag, on the other hand, will not, as it is not the first tag after the P tag <P>:
<P align="justify">
<BR/>
<A href="http://en.kioskea.net/">CCM</A>
</P>

Text pseudo-classes

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:

  • :first-line: used for applying a style to the first line of a paragraph.
    P:first-line { text-transform: uppercase }
  • :first-letter: used for applying a style to the first letter of a paragraph in order to produce a typographical effect. The following example doubles the font size and bolds the first letter of a paragraph:
    P:first-letter { font-size: 200%; font-weight: bold; }

Language pseudo-classes

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:

  • :lang(Langue). The following language pseudo-class is used if, for whatever reason, you wish to use French-style quotation marks:
    HTML:lang(fr) { quotes: '&laquo; ' ' &raquo;' }

Page pseudo-classes

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: used to set properties for left-hand pages.
    @page:left { size: landscape; margin-left: 2cm; }
  • @page:right: used to set properties for right-hand pages.
    @page:right { size:landscape; margin-left: 2.5cm; }
  • @page:first: used for defining the properties of the first page in a document.
    @page:first { size: portrait;
    margin-left: 2.5cm;
    margin-right: 2cm;
    margin-bottom: 1cm;
    margin-top: 4cm;}

ID selectors

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.

Last update on Thursday October 16, 2008 02:43:13 PM.This document entitled « Style classes (CSS) » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the licence, as this note appears clearly.

Best answers for « Style classes (CSS) » in :
Declaring a style sheet (CSS) Show Declaring a style sheet Style sheets are not directly integrated into the W3C's HTML recommendations. For this reason, you need to include elements in the HTML code indicating both the type of document, meaning the version of the HTML and CSS...
CSS - Style sheets Show Introduction to style sheets The concept of style sheets first appeared in 1996 when the W3C published a new recommendation entitled "Cascading Style Sheets", or CSS for short. The principle behind style sheets involves using a single document to...
Style syntax (CSS) Show Defining a style Defining a style is done using simple text rules for describing the aspects of page elements. A CSS rule is characterised by two main elements: A type selector, for specifying which tags in the document the style applies to; A...
Remove the underline under the links in HTML/CSS ShowRemove the underline under the links in HTML/CSS Using CSS Using CSS via a class Using CSS online AS you may have noticed, hyperlinks are underlined in HTML (by default) It is possible to remove the underline by using the CSS Using...
Create CSS easily ShowCreate CSS easily Easy links CSS is a short term for Cascading Style Sheets. CSS is used to alter the display of the HTML coded environment. For example, using CSS can change the appearance of your scroll bar, the color, headings...
Using visual styles (themes) on Windows XP ShowUsing visual styles (themes) on Windows XP A theme in principle includes a visual style (the appearance of windows, the taskbar and menus), desktop icons, wallpaper, the mouse, and a screen saver. If Microsoft has provided the...
Download Amaya ShowAmaya is a Web editor, developed collectively by the INRIA and the W3C, to edit and publish very simply pages containing text (in HTML or XHTML), graph (in SVG) and mathematical expressions (in MathML), the whole with style sheets CSS. Amaya is a...
Download Classic Style Menus for Word 2007 ShowClassic Style Menus for Word 2007 is an add-in of Microsoft Word which has been designed to allow you to use several features of Microsoft Word 2003 on Microsoft Word 2007. With this program, you can have access to different menus of MS 2003...
Download Media Player Classic ShowDescription: Media Player Classic is a video reader taking back the appearance of the first version of Windows Media Player.Its very simple appearance hides a very successful reader recognizing a very large number of video and audio...
CSS - Style sheets ShowFont properties Property Value Description font-family Specific font (Arial, Times, Verdana) Familly (serif, sans-serif, fantasy, monospace, cursive) Defines one or more font names or font families. If multiple fonts are defined,...
Units in style sheets (CSS) ShowBlock elements DIV tags define blocks of elements This is a structure created to define boxes such as menus or windows. CSS offers several properties for specifying the characteristics of margins. With a good working knowledge of these properties,...
Units in style sheets (CSS) ShowUnits With style sheets, you can use numeric values to set style properties in multiple ways: absolutely, meaning in units which are independent of the form of output (such as in centimetres); relatively, meaning in units relative to an element....