generated contentIf you cast your mind back you might recall our golden style sheets rule, separate appearance from content. Now, suddenly we are talking about generated content. You might be thinking, doesn't this introduce content into our style sheets? What about the golden rule? Well, it's a good question, and this area is one of controversy among style sheets experts. I won't buy into that debate here, but I want to cover the issue of generated content in CSS2 in this section. Firstly, note that some kinds of generated content already exist in CSS1. Think about bulleted and numbered lists. These add numbering or bullets, so they do generate content to an extent. CSS2 adds more powerful ways of generating content. Here we learn how. There are two aspects to generating content with CSS2.
Where is the content to be inserted? To answer this question we use the two new CSS2 pseudo classes we saw two sections back in../selectors/p_element_selectors.html. The What content is to be inserted? To do this we use a new property, The
Let's take a look at the content property itself in detail, then we'll look at each of these different types of content as well. contentWhat it doesThe Possible valuesAs mentioned above
Let's look at each of these values in turn. Strings of textWhat it doesUsing this value will insert a string of text before or after the selected element. SyntaxSay we wanted to generate the content "Chapter " before every heading level 1. h1:before All you need do then is place the string of text you want to be generated between double quote marks. Browser supportDetailed browser support information for this feature can be found in the full version of the Westciv CSS Guide, or in our CSS Browser Support Table. URLsWhat it doesYou can use this value to insert an image, or other type of content that can be linked to with a URL. SyntaxIf you wanted to use a small decorative image after every heading level 2 h2:after URLs are defined as they are in other CSS properties that can take a URL value. Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. counter and countersWhat it doescounter values are more than a little involved. Think of them as super-charged versions of numbered lists. Counters have a name, and a style. You create a counter using the counter function, then use them in conjunction with the content property to insert markers into a document. This isn't quite as confusing as it initially seems. There are two forms of the counter function, the The The Keyword defined elementsWhat it doesAt the moment, keyword values are limited to allowing the insertion of various types of quote marks.
Basically it works like this. You use the Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. The attr() functionWhat it doesThe last possible value for a:after { content: attr(title) } This might be used in conjunction with an @media print {a:after{ content: attr(href) } } Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. How is content added?Now, after that long diversion, where we looked at the possible values that the content property can take, let's go back to just a couple more things about the content property itself. Content is added as inline, block or marker elements. However the statement that creates the content can set the display of the content to be a block, or other kind of element, rather than an inline element. For example body:after { content: "The End"; display: block; margin-top: 2em; text-align: center; } places the text string "The End" at the foot of the page as a block, rather than as an inline element. Default valuesIf no Is it inherited?An element does not inherit the Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. quotesWhat it doesThe actual quotation marks that will be drawn are defined by the new The Possible values
/* Specify pairs of quotes for two levels in two languages */ q:lang(en) { quotes: '"' '"' "'" "'" } q:lang(fr) { quotes: "«" "»" "<" ">" } In English, according to this statement, first level quotes are double quoted, while second level quotes are single quoted. In French, first level quotes use "«" and "»" while second level quotes use "<" and ">". Default valuesIf no value for Is it inherited?An element inherits the Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. Counters and counter propertiescounterCounters are sophisticated and powerful, and not surprisingly more than a little complex. But be not afraid, a little thinking and they are manageable. Here's how they work. Counters are used exclusively by the h1:before {content: counter(chapter)} This tells the browser to add the value of the counter called "chapter" before every heading of level one. But what value is that? Well, if we don't set the value using the properties we will discuss in a minute, the counter value will be 0. So every heading of level one will be preceded by a 0. We don't really want that. We want something along the lines of the first heading to be preceded by a 1, the second by a 2, and so on. To do this, we use the To use h1 {counter-increment: chapter 1} You can of course combine the two rules into this simple rule h1:before {content: counter(chapter); counter-increment: chapter 1 } This increments the counter then adds it before the What does this let us achieve? Well, now, instead of marking up the headings of each chapter in our HTML based document like this <h1>1. Abstract</h1> ... <h1>2. Introduction</h2> We leave out the numbering and it is done automatically. This aids the editing and writing processes, because moving sections, subsections and so on, or deleting them, or inserting new sections doesn't necessitate re-entering every number! It's particularly useful where a document might be served up in pieces based on the user's needs, or the medium they are using. Let's look at a more complex example, using subsections. Here we will also take a look at the Here is what we do h1:before {content: counter(chapter); counter-increment: chapter 1 /* this is the same as the example above */ counter-reset: subsection 0 } In this example, we reset the counter called subsection to 0 each time we start a new section (assuming the h2:before {content: counter(chapter) "." counter(subsection); counter-increment: subsection 1 } This increments the counter called subsection, then inserts numbering of the form "2.5" before the text of the heading. Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. countersThe last thing we need to learn about counters is a little more involved again. Think about lists in HTML. These can contain other lists, which can contain others, and so on ad infinitum. It's not possible in advance to know how many levels there will be, so how do we take care of this situation? Fortunately those smart people at the W3C thought of this, and provide a solution. It's a little tricky, but not rocket science. Counters have a particular "scope". When a counter is created its scope is the level at which it was created, and all that level's children. However, where a counter of the same name is created inside the scope of the initial counter, then this is treated as a separate counter, which now takes over the counting. Confusing? Let's take a look at an example, taken from the CSS2 specification. Here's the style sheet rule that creates the counter ol { counter-reset: item } li { display: block } li:before { content: counter(item) ". "; counter-increment: item } And here is the HTML <ol> <!-- (set item[0] to 0 --> <li>item <!-- increment item[0] (= 1) --> <li>item <!-- increment item[0] (= 2) --> <ol> <!-- (set item[1] to 0 --> <li>item <!-- increment item[1] (= 1) --> <li>item <!-- increment item[1] (= 2) --> <li>item <!-- increment item[1] (= 3) --> <ol> <!-- (set item[2] to 0 --> <li>item <!-- increment item[2] (= 1) --> </ol> <!-- ) --> <ol> <!-- (set item[3] to 0 --> <li> <!-- increment item[3] (= 1) --> </ol> <!-- ) --> <li>item <!-- increment item[1] (= 4) --> </ol> <!-- ) --> <li>item <!-- increment item[0] (= 3) --> <li>item <!-- increment item[0] (= 4) --> </ol> <!-- ) --> <ol> <!-- (reset item[4] to 0 --> <li>item <!-- increment item[4] (= 1) --> <li>item <!-- increment item[4] (= 2) --> </ol> <!-- ) --> Each < Here's one last string to the To cut a long story short, it's as simple as Browser SupportSee the Westciv CSS Guide, or our CSS Browser Support Table. counter-resetWhat it doesThe Possible values
h1 { counter-reset: counter1 -1 } h1 { counter-reset: counter2 99 } In order to reset both, the following must be used. h1 { counter-reset: counter1 -1 counter2 99 }
Default valuesIf no Is it inherited?An element does not inherit the Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. counter-incrementWhat it doesThe Possible values
h1 { counter-increment: counter1 -1 } h1 { counter-increment: counter2 99 } In order to reset both, the following must be used. h1 { counter-increment: counter1 -1 counter2 99 }
Default valuesIf no Is it inherited?An element does not inherit the Browser supportSee the Westciv CSS Guide, or our CSS Browser Support Table. next: media(C)1997-2001 Western Civilisation Pty. Ltd. |