CSS

Structure

and

Rules

 
 

Structure and Rules

Table of contents:

Rules
Declarations
Pseudo-classes and Pseudo-elements
Cascading Order


Basic Syntax

Rules

Selectors

Any HTML element is a possible CSS1 selector. The selector is simply the element that is linked to a particular style. For example, the selector in:


P { text-indent: 3em }


is P .

Class Selectors

A simple selector can have different classes, thus allowing the same element to have different styles. For example, an author may wish to display code in a different color depending on its language:


code.html { color: #191970 }
code.css { color: #4b0082 }


The above example has created two classes, css and html for use with HTML's CODE element. The CLASS attribute is used in HTML to indicate the class of an element, e.g.,


<P CLASS=warning>Only one class is allowed per selector. For example, code.html.proprietary is invalid.</p>

Classes may also be declared without an associated element:


.note { font-size: small }


In this case, the note class may be used with any element.

A good practice is to name classes according to their function rather than their appearance. The note class in the above example could have been named small, but this name would become meaningless if the author decided to change the style of the class so that it no longer had a small font size.

ID Selectors

ID selectors are individually assigned for the purpose of defining on a per-element basis. This selector type should only be used sparingly due to its inherent limitations. An ID selector is assigned by using the indicator "#" to precede a name. For example, an ID selector could be assigned as such:


#svp94O { text-indent: 3em }

This would be referenced in HTML by the ID attribute:


<P ID=svp94O>Text indented 3em</P>

Contextual Selectors

Contextual selectors are merely strings of two or more simple selectors separated by white space. These selectors can be assigned normal properties and, due to the rules of cascading order, they will take precedence over simple selectors. For example, the contextual selector in


P EM { background: yellow }

is P EM . This rule says that emphasized text within a paragraph should have a yellow background; emphasized text in a heading would be unaffected.

 

 

BreBru.Com Extra Information Techonology HTML