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

Book HomeDynamic HTML: The Definitive ReferenceSearch this book

11.2. Pseudo-Elements and Pseudo-Classes

Most style sheet rules are associated with distinct HTML elements or groups of elements identified via style sheet selectors, such as classes, IDs, and contextual selectors (see Chapter 3). In rare instances, you might want to assign a style to a well-defined component of an element (pseudo-element) or to all elements that exhibit a particular state (pseudo-class).

11.2.1. Pseudo-Elements

A pseudo-element gets its name because the CSS declaration of this type causes the browser to act as if it has inserted an artificial element into an existing element. For example, CSS1 defines pseudo-elements for the first letter and first line of a block-level element. The HTML source code for the real element might be something simple:

<p>A mere paragraph.</p>

But a browser that implements the :first-letter and :first-line pseudo-elements would treat the p element as if it were structured as follows:

<p><p:first-line><p:first-letter>A</p:first-letter> mere 
paragraph.</p:first-line></p>

The location of the </p:first-line> end tag, of course, depends on the rendered version of the p element. If the paragraph were sized to fit a narrow column, and the first line word-wrapped after the word "mere," the :first-line pseudo-element's invisible end tag would follow the space after "mere." The point of all of this is that you can assign numerous style attributes to these very specific portions of a block-level element, such as a drop capital letter:

p:first-letter {font-size: 36pt; font-weight: 600; 
                font-family: Rune, serif; float: left}

or an all-uppercase first line:

p:first-line{text-transform:uppercase}

Regardless of the pseudo-element structure or style assignments, the document tree is unaffected. In the simple p element example, the element contains one text child node.

As of CSS2, four pseudo-elements have been defined, as shown in Table 11-1. Note that the :first-letter pseudo-element acknowledges style attributes only of the following types: background, border, clear, color, float, font, line-height, margin, padding, text-decoration, text-shadow, text-transform, and vertical-align (when float is none). The :first-line pseudo-element acknowledges style attributes only of the following types: background, clear, color, font, letter-spacing, line-height, text-decoration, text-shadow, text-transform, vertical-align, and word-spacing.

Table 11-1. CSS2 pseudo-elements

Name

NN

IE/Windows

IE/Mac

CSS

Description

:after

6

n/a

n/a

2

The space immediately after an element (see content attribute)

:before

6

n/a

n/a

2

The space immediately before an element (see content attribute)

:first-letter

6

5.5

5

1

The first letter of a block-level element

:first-line

6

5.5

5

1

The first line of a block-level element

11.2.2. Pseudo-Classes

The a element has readily distinguishable states: a link that has not been visited, a link being clicked on, a link that has been visited in recent history. These states are called pseudo-classes; they work like class selector definitions but don't have to be labeled as such in their element tags. A pseudo-class always operates as a kind of modifier to another selector. In the following example, notice how the :hover pseudo-class operates on all a elements in one rule, and applies an extra attribute to an a element singled out by its ID:

a {text-decoration:none}
a:hover {text-decoration:underline}
#specialA:hover {color:red}

The classness of a pseudo-class is not always based on an element's state. Document tree context, page position (right or left), and even language are examples of the possibilities that pseudo-classes afford. For example, the :first-child pseudo-class turns its associated element into a special class (i.e., a class capable of defining its own style attributes) whenever the element is a first child element in a document tree. Thus, the following style rule applies a different font size for every p element that is the first child of any container with the class name section:

.section > p:first-child {font-size:110%}

The use here of the > child selector limits the scope of the p:first-child pseudo-class to first children of specific containers. Removing the child selector would cause the rule to apply to any p element that is the first child of any other container.

Table 11-2 provides a summary of pseudo-classes supported by CSS2. Implementation in mainstream browsers is sporadic.

Table 11-2. CSS2 Pseudo-classes

Name

NN

IE/Windows

IE/Mac

CSS

Description

:active

6

4

4

1

An a element being clicked on by the user

:first

n/a

n/a

n/a

2

First page of a document (with @page declaration)

:first-child

6

n/a

5

2

Any element that is the first child of another element

:focus

6

n/a

5

2

Any element that has focus

:hover

6

4

4

2

An a element that has a cursor on top of it

:lang(code)

n/a

n/a

5

2

An element with the same language code

:left

n/a

n/a

n/a

2

A left-facing page (with @page declaration)

:link

6

4

4

1

An a element that has not yet been visited

:right

n/a

n/a

n/a

2

A right-facing page (with @page declaration)

:visited

6

4

4

1

An a element that has been visited within the browser's history



Library Navigation Links

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











??????????????@Mail.ru