Book HomeCascading Style Sheets: The Definitive GuideSearch this book Thursday 23rd of March 2017 09:04:42 PM

10.4. Generated Content

Generated content is a new way of adding things to existing content without having to alter the content itself. It's done by using the pseudo-elements :before and :after and the property content. Here's a basic example of how it works:

P:before, P:after {content: "\""; color: gray;}
<P>This is a quote.</P>

The browser will display what's shown in Figure 10-15.

Figure 10-15

Figure 10-15. Adding generated content

Note that the double-quote mark was escaped out -- that is, preceded by a backslash. This is necessary, since text values for content must be enclosed in double quotes. You could also place images before (or after) content, using something like P:before {content: url(para.gif);} to put a paragraph symbol at the beginning of each paragraph. You can even string multiple values together like this (shown in Figure 10-16):

P:before {content: url(para.gif) " -- ";}
Figure 10-16

Figure 10-16. Adding an image and text before a paragraph

This would cause each paragraph to be started with a paragraph symbol, a blank space, two dashes, and then another blank space. Note that all of this is considered part of the paragraph and is inlined within it. The spaces appear before and after the double dash because they're included in the string value. If these spaces were omitted, then space would not appear to either side of the dashes.

Let's say, though, that you want to do some real quoting, using real quotation marks -- you know, the curly double quotes that are so hard to specify in HTML and which often don't show up even if you do try to specify them. CSS2 has ways to handle this.

content has some other values you can use:

So if you wanted your quotations to begin and end with quotation marks, instead of typing in a literal quotation mark, you could let the browser insert "smart quotes" for you.

BLOCKQUOTE:before {content: open-quote;}
Figure 7-36

Figure 7-36. The relation of border-width keywords to each other

Let's suppose a paragraph has margins, a background color, and a border style set as shown in Figure 7-37:

P {margin: 5px; background-color: silver; border-style: solid;}
Figure 7-37

Figure 7-37. Margins, backgrounds, and borders

The border's width is, by default, medium , as we can see in Figure 7-37. We can change that to the result in Figure 7-38 as follows:

BLOCKQUOTE:after {content: close-quote;}

10.4.1. Automatic Numbering

In the same vein, CSS2 also includes properties for automatic numbering. First, you can specify a counter as a value of content. This can be a bit tricky, and it would take too long to run through all the possibilities, but here's an example. Say you wanted the chapters and sections of a document numbered 1, 1.1, 1.2, 1.3, and so on. In addition, you're using H1 for your chapters and H2 for your sections. Here are the declarations you would use:

H1:before {
content: "Chapter " counter(chapter) ". ";
counter-increment: chapter;   /* Add 1 to chapter */
counter-reset: section;       /* Set section to 0 */
H2:before {
content: counter(chapter) "." counter(section) " ";
counter-increment: section;

As we can see from Figure 10-17, the user agent will add the word "Chapter" and a number at the beginning of H1 text. This number is automatically incremented with each H1, due to the declaration counter-increment: chapter;. It also sets the section counter back to zero through counter-reset: section;. Then, for each section heading (H2), the browser uses the chapter number, followed by a period (.) followed by the current section number, which is also automatically incremented.

Figure 10-17

Figure 10-17. Adding counters to elements

You don't have to increment by one every time, either. You can define any integer as the increment value, including zero and negative numbers. If you want each section to have an even number, as we see in Figure 10-18, then you can declare the following:

H2:before {
content: "Section " counter(section) ". ";
counter-increment: section 2; /* Add 2 to chapter */
Figure 10-18

Figure 10-18. Changing a counter's incremental value

You can also keep an element from incrementing a counter by setting its display to none. Of course, that will cause the element to disappear altogether.

10.4.2. Markers

You can do even more by using the value marker for the property display, which enables you to define your own marker styles for any element at all. You're already familiar with markers, as it happens -- the bullets and numbers at the beginning of list items are markers.

Let's say we want to recreate the way unordered lists behave. For the purposes of this example, we'll use the image disc.gif to stand in for the normal bullets. Using marker properties, we would declare:

LI:before {display: marker;
content: url(disc.gif);
marker-offset: 1em;

This will insert the disc image before each list item, and set it to be offset from the left edge of the LI content by 1em, as shown in Figure 10-19.

Figure 10-19

Figure 10-19. Styling list markers

Marker properties are not restricted to list items, however. Let's say that, throughout a document, there are a few paragraphs with a class of aside. We wish to call attention to these paragraphs by inserting a small note to the side of each one. Here's one way to do it:

BODY {counter-reset: aside-ctr;}
P {margin-left: 10em;}
P.aside:before {display: marker;
content: "Aside " counter(aside-ctr) " --";
counter-increment: aside-ctr;
text-align: right;
marker-offset: 1em;
width: 9.5em;

The effect will be something like that seen in Figure 10-20.

Figure 10-20

Figure 10-20. Automatically numbered asides

This is yet another aspect of CSS2 that, once it's been properly implemented, will allow authors to do quite a bit with their documents.

Library Navigation Links

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

Figure 6-28:

BODY {background-image: url(yinyang.gif);
background-repeat: repeat;}
Figure 6-28

Figure 6-28. Tiling the background image in CSS

I've left out a background color in order to keep the rule short, but remember to include a background color any time you have a background image. And, of course, the effect shown in Figure 6-28 would have been the same if we'd left out the background-repeat property altogether, since repeat is its default value.

This should cause older browsers to completely ignore not only theSTYLE tags but the declarations as well, becauseHTMLcomments are not displayed. Meanwhile, those browsers that understandCSS will still be able to read the style sheet.


There is one drawback to this strategy. A few versions of olderbrowsers, such as very early versions of Netscape Navigator and NCSAhave uneven padding and H2 elements to have regular padding, as shownin Figure 7-57:

H1 {padding: 10px 0.25em 3ex 3cm;} /* uneven padding */H2 {padding: 0.5em 2em;} /* values replicate to the bottom and left sides */
Figure 7-57

Figure 7-57. Uneven padding

It's a little tough to see the padding, though, so let'sadd a background color, as shown in Figure 7-58:

H1 {padding: 10px 0.25em 3ex 3cm; background: silver;}text-decoration. First off is the fact thattext-decoration is notinherited. This implies a requirement that any decoration lines drawnwith the text -- either under, over, or through it -- should bethe same color as the parent element. This is the case even if thechild elements are different colors, as depicted in Figure 4-59:

P {text-decoration: underline; color: black;}B {color: gray;}<P>This paragraph, which is black and has a black underline, also contains

To see what this means, let's start with a paragraph that has a negative top margin and no margins on its other sides -- this will keep the example simple. In addition, we'll make the paragraph bold, so that it's easier to distinguish from its neighbors:

<P STYLE="margin: -1.75em 0 0 0; font-weight: bold;">
This paragraph has a negative top margin...

We can see in Figure 7-18 that the paragraph has