home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeWebmaster in a Nutshell, 3rd EditionSearch this book

9.2. Style Syntax

The syntax of a style, as you may have gleaned from our previous examples, is fairly straightforward. A style rule is made up of at least three basic parts: a tag selector, which identifies the name of the tag that the style rule affects, followed by a curly brace ({ }) enclosed, semicolon-separated list of one or more style property:value declaration pairs:

tag-selector {property1:value1; property2:value1 value2 value3; ...}

Properties require at least one value but may include two or more values. Separate multiple values with a space, as is done for the three values that define property2 in the example. Some properties require that multiple values be separated with commas.

Styles-conscious browsers ignore letter case in any element of a rule. Hence, H1 and h1 are the same selector, and COLOR, color, ColOR, and cOLor are equivalent properties. Convention dictates, however, that tag names be all uppercase, and that properties and values be lowercase. We'll abide by those conventions throughout this book.

Any valid HTML tag name (a tag minus its enclosing < and > characters and attributes) can be a selector. You may include more than one tag name in the list of selectors, as we explain in the following sections.

9.2.2. Contextual Selectors

Normally, the browser applies styles to tags wherever they appear in your document, without regard to context. However, the CSS standard does define a way to have a style applied only when a tag occurs within a certain context within a document, such as when it is nested within other tags.

To create a contextual selector, list the tags in the order in which they should be nested in your document, outermost tag first, with no commas separating them. When that nesting order is encountered by the browser, the style property is applied to the last tag in the list.

For example, here's how you might use contextual styles to define the classic numbering sequence used for outlines: capital letters for the outer level, uppercase Roman numerals for the next level, lowercase letters for the next, and Arabic numerals for the innermost level:

OL LI {list-style: upper-alpha}
OL OL LI {list-style: upper-roman}
OL OL OL LI {list-style: lower-alpha}
OL OL OL OL LI {list-style: decimal}

According to the example style sheet, when the styles-conscious browser encounters the <li> tag nested within one <ol> tag, it uses the upper-alpha value for the list-style property of the <li> tag. When it sees an <li> tag nested within two <ol> tags, the same browser uses the upper-roman list style. Nest an <li> tag within three and four <ol> tags, and you'll see the lower-alpha and decimal list-style used, respectively.

Similarly, you may impose a specific style on tags related only by context. For instance, this contextual style definition colors the emphasis tag's (<em>) contents red only when it appears inside a level-one header tag (<h1>), not elsewhere in the document:

H1 EM {color: red}

If there is potential ambiguity between two contextual styles, the more specific context prevails. Like individual tags, you may also have several contextual selectors mixed with individual selectors, each and all separated by commas, sharing the same list of style declarations. For example:

H1 EM, P STRONG, ADDRESS {color: red}

means that you'll see red whenever the <em> tag appears within an <h1> tag, or when the <strong> tag appears within a <p> tag, and for the contents of the <address> tag.

The nesting need not be exact to match the rule. For example, if you nest the <strong> tag within a <ul> tag within a <p> tag, you'll still match the rule for P STRONG that we defined above. If a particular nesting matches several style rules, the most specific rule is used.

9.2.3. Style Classes

There is one more feature of style sheets that we haven't mentioned yet: classes. Classes let you create, at the document level or in an external style sheet, several different styles for a single tag, each distinguished by a class name. To apply the style class, name it as the value of the class attribute in the tag. Regular classes

In a technical paper you might want to define one paragraph style for the abstract, another for equations, and a third for centered quotations. None of the paragraph tags may have an explicit context in the HTML document so you could distinguish it from the others. Rather, you may define each as a different style class:

<style type="text/css">

P.abstract {font-style: italic; 
            left-margin: 0.5cm; 
            right-margin: 0.5cm}

P.equation {font-family: Symbol; 
            text-align: center}

H1, P.centered {text-align: center; 
                left-margin: 0.5cm; 
                right-margin: 0.5cm}


Notice first in the example that defining a class is simply a matter of appending a period-separated class name as a suffix to the tag name as the selector in a style rule. The class name can be any sequence of letters, numbers, and hyphens but must begin with a letter. And classes, like all selectors, may be included with other selectors, separated by commas, as in the third example. The only restriction on classes is that they can't be nested: e.g., P.equation.centered is not allowed.

Accordingly, the first rule in the example creates a class of paragraph styles named "abstract" whose text is italic and indented from the left and right margins by a half centimeter. Similarly, the second paragraph style class "equation" instructs the browser to center the text and to use the Symbol typeface to display the text. The last style rule creates a style with centered text and half-centimeter margins, applying this style to all level-one headers, as well as creating a class of the <p> tag named centered with that style.

To use a particular class of a tag, you add the class attribute to the tag, as in this example:

<p class="abstract">
This is the abstract paragraph.  See how the margins are indented?

<h3>The equation paragraph follows</h3>

<p class="equation">
a = b + 1

<p class="centered">
This paragraph's text should be centered.

For each paragraph, the value of the class attribute is the name of the class to be used for that tag. Style pseudo-classes

In addition to conventional style classes, the CSS standard defines pseudo-classes, which allow you to define the display style for certain tag states. Pseudo-classes are like regular classes, with two notable differences: they are attached to the tag name with a colon instead of a period, and they have predefined names, not arbitrary ones you may give them.

There are five pseudo-classes, three of which are associated with the <a> tag. The other two can be used on any text element.

The browsers distinguish three special states for the hyperlinks created by the <a> tag: not visited, being visited, and visited. The browser may change the appearance of the tag's contents to indicate its state, such as underlining or changing the colors. Through pseudo-classes, the HTML author can control how these states get displayed by defining styles for A:link, A:active, and A:visited. The link pseudo-class controls the appearance of links that are not selected by the user and have not yet been visited. The active pseudo-class defines the appearance of links that are currently selected by the user and are being processed by the browser. The visited pseudo-class defines those links that have already been visited by the user.

To completely define all three states of the <a> tag, you might write:

A:link {color: blue}
A:active {color: red; font-weight: bold}
A:visited {color: green}

The two other pseudo-classes usually apply to the <p> element, and are named first-letter and first-line. As you might expect, these pseudo-classes control the appearance of the first letter and first line, respectively, of a paragraph and create effects commonly found in printed media, such as initial drop-caps and bold first lines. For example:

P:first-line {font-style: small-caps}

converts the first line of a paragraph to small capital letters. Similarly:

P:first-letter {font-size: 200%; float: left}

tells the browser to make the first letter of a paragraph twice as large as the remaining text and float the letter to the left, allowing the first two lines of the paragraph to float around the larger initial letter.

You may mix pseudo-classes with regular classes by appending the pseudo-class name to the selector's class name.

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.