| PreviousCascading Style Sheets | Style syntax (CSS) | NextDeclaring a style sheet |
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:
As an example, the following syntax defines the style to apply to hyperlinks (<A> tag), specifically Verdana font, 18 pixels in size, bold and in yellow:
A {
font-family: Verdana;
font-size: 18px;
font-style: bold;
color: yellow
}
"Type selectors" (or "type element selectors") are the word(s) before the brackets, which indicate the documents tag(s) to which the style between the brackets apply.
To define the style of a particular HTML tag, simply use the name of the tag (without the characters < and >). For example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<STYLE type="text/css">
<!--
tag {properties}
-->
</STYLE>
</HEAD>
<BODY>
<tag> ... </tag>
...
</BODY>
</HTML>
You can also apply a style to multiple tags, by separating the names of these tags with a comma (,). The syntax of such a selector, called a multiple selector, is:
type-selector1, type-selector2 { /* style */ }
With the universal selector ("*") you can define a style that will apply to all HTML elements. The universal selector's syntax is:
* { /* style */ }
![]() |
The universal selector is not commonly implemented in browsers! |
You can select a tag within a given context, i.e. depending on what the surrounding elements are, using contextual selectors.
There are several types of contextual selectors.
selector_X selector_Y { /* style; */ }
For the following HTML code:
<p> <i>Note</i>, this is a <b>warning</b> </p>The following rule only affects the word "warning" (the only one surrounded by <B> tags, which themselves are nested within a set of <P> tags):
<b> Please read carefully </b>
P B { font-weight: bold; color: red; }
selector_X + selector_Y { /* style; */ }
For the following HTML code:
<p> <i>Note</i>, this is a <b>warning</b> </p>The following rule only affects the word "warning" (the only one surrounded by <B> tags, which themselves follow a set of <I> tags):
<b> Please read carefully </b>
I + B { font-weight: bold; color: red; }
selector_X > selector_Y { /* style; */ }
For the following HTML code:
<p> <b><i> Note </i></b>, this is a <i><b> warning </b></i> </p>The following rule only affects the element "<i> Note </i>" (the only one surrounded by <B> tags, which are themselves embedded within a set of <P> tags):
<b> Please read carefully </b>
P > B { font-weight: bold; color: red; }
It is possible (and recommended) to document style sheets by incorporating comments that give additional information (a reason for choosing one style or another, the type of document it applies to, context, etc.) CSS comments are set off by the signs /* and */:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<STYLE type="text/css">
<!--
/* This is a comment */
tagA {properties}
tagB {properties}
tagC {properties}
-->
</STYLE>
</HEAD>
<BODY>
...
</BODY>
</HTML>
![]() |
A style can be applied "in-line" to any HTML tags, except for the following: BASE, BASEFONT, HEAD, HTML, META, PARAM, SCRIPT, STYLE, TITLE |