What Is Generated Content?
In technical terms, generated content is a simple abstraction created by CSS in the document tree. As such, in practical terms, generated content exists only in the layout of the Web document. Accessing generated content via JavaScript is possible by reading the textual value of thecontent
property:
var test = document.querySelector('#test');
var result = getComputedStyle(test, ':before').content;
var output = document.querySelector('#output');
output.innerHTML = result;
Inserting Generated Content
Generated content may be inserted before and after the actual content of an element, using the:before
and :after
pseudo-elements, respectively. To represent the pseudo-elements, we can use the following pseudo-markup.
<p>
<before>Start</before>
Actual content
<after>End</after>
</p>
And our CSS would be:
p:before {
content: "Start";
}
p:after {
content: "End";
}
Bear in mind that if you are validating the CSS file against the CSS3 specifications, the:before
and :after
pseudo-elements should be written as ::before
and ::after
. Otherwise, the CSS validator will call an error.
As you can see, the property that inserts the two strings is content
. This
property accepts the following values:
none
,normal
The pseudo-content would not be generated.<string>
This would be a textual string enclosed in quotation marks.url()
This function enables us to insert an external resource (usually an image), as with thebackground-image
property.counter()
,counters()
These functions insert counters (see below for details).attr(attribute)
This function enables us to insert the value ofattribute
of a given element.open-quote
,close-quote
,no-open-quote
,no-close-quote
These values automate the generation of quotation marks.
Inserting Strings
In the previous example, we inserted two simple strings before and after the actual content of the element. Generated content also enables us to insert more complex symbols, through escaping:p:before {
content: "\00A7";
padding-right: 0.2em;
}
The escaped sequence between the double quotation marks is the hexadecimal Unicode value of the paragraph symbol. We can also combine simple strings with Unicode symbols:
p:before {
content: "( " "\00A7" " )";
padding-right: 0.2em;
}
In case you need it, a comprehensive list of all Unicode characters is available on Alan Wood’s website.
Note that all textual content inside the content
property is treated literally. So, spaces and tabs inserted via the keyboard will be inserted on the page as well.
Inserting Icons Using Web Fonts
Web fonts can be used to insert graphical icons through generated content. Depending on the Web font family, you can insert either simple letters or Unicode sequences:@import url(http://weloveiconfonts.com/api/?family=brandico);
p:before {
content: "\f303";
padding-right: 0.3em;
font-family: 'brandico', sans-serif;
font-size: 22px;
}
In this example, we have inserted a Twitter icon. Our code could be rewritten as follows:
.icon-twitter:before {
content: "\f303";
padding-right: 0.3em;
font-family: 'brandico', sans-serif;
font-size: 22px;
}
Inserting Images
We can insert images through theurl()
function.
a:before {
content: url(link.png);
padding-right: 0.2em;
}
As you can see, this function has the same syntax as the background-image
property.
Inserting Attribute Values
An attribute value of an element can be inserted through theattr()
function.
a[href]:after {
content: "( " attr(href) " )";
padding-left: 0.2em;
color: #000;
font: small "Courier New", Courier, monospace;
}
We’ve just inserted the value of the href
attribute, which is a simple text string.
Inserting Counters
The automatic numbering of CSS is controlled by two properties,counter-reset
and counter-increment
. Counters defined by these properties are then used with the counter()
and counters()
functions of the content
property.
The counter-reset
property may contain one or more names of counters (i.e. “identifiers”), optionally followed by an integer. The integer sets the value that will be incremented by thecounter-increment
property for any occurence of the given element. The default value is 0. Negative values are allowed.
The counter-increment
property is similar. The basic difference is that this one increments a counter. Its default increment is 1. Negative values are allowed.
Now we are ready for an example. Take the following markup:
<dl>
<dt>term</dt>
<dd>description</dd>
<dt>term</dt>
<dd>description</dd>
<dt>term</dt>
<dd>description</dd>
</dl>
We want to add progressive numbering (1, 2, 3, etc.) to each definition term (dt
) in the list. Here is the CSS:
dl {
counter-reset: term;
}
dt:before {
counter-increment: term;
content: counter(term);
}
The first rule here sets a counter for the definition list. This is called a “scope.” The name (or identifier) of the counter is term
. Whatever name we choose for our counter must be identical to the one in the counter-increment
property (of course, the name should be meaningful).
In the second rule, we attach the :before
pseudo-element to the dt
element, because we want to insert the counter precisely before the actual content of the element. Let’s take a closer look at the second declaration of the second rule. The counter()
function accepts our identifier (term
) as its argument, and the content
property generates the counter.
There’s no space between the number and the content of the element. If we want to add a space and, say, a period after the number, we could insert the following string in the content
property:
dt:before {
content: counter(term) ". ";
}
Note that the string between the quotation marks is treated literally; that is, the space after the period is inserted just as we’ve typed it on the keyboard. In fact, the content
property can be regarded as the CSS counterpart of the JavaScript document.write()
method, except that it doesn’t add real content to the document. Simply put, the content
property creates a mere abstraction in the document tree but doesn’t modify it.
In case you’re wondering, we can add more styles to counters by applying other properties to the attached pseudo-element. For example:
dt:before {
content: counter(term);
padding: 1px 2px;
margin-right: 0.2em;
background: #ffc;
color: #000;
border: 1px solid #999;
font-weight: bold;
}
We’ve just set a background color, added some padding and a right margin, made the font bold, and outlined the counters with a thin solid border. Now our counters are a little more attractive.
Furthermore, counters may be negative. When dealing with negative counters, we should adhere to a little math — namely, the part about adding and subtracting negative and positive numbers. For example, if we need progressive numbering starting from 0, we could write the following:
dl {
counter-reset: term -1;
}
dt:before {
counter-increment: term;
content: counter(term) ". ";
}
By setting the counter-reset
property to -1 and incrementing it by 1, the resulting value is 0, and the numbering will start from that value. Negative counters may be combined with positive counters to interesting effect. Consider this example:
dl {
counter-reset: term -1;
}
dt:before {
counter-increment: term 3;
content: counter(term) ". ";
}
As you can see, adding and subtracting negative and positive numbers yield a wide range of combinations between counters. With just a simple set of calculations, we get complete control over automatic numbering.
Another interesting feature of CSS counters lies in their ability to be nested. In fact, numbering may also be ordered by progressive sublevels, such as 1.1, 1.1.1, 2.1 and so on. To add a sublevel to the elements in our list, we would write the following:
dl {
counter-reset: term definition;
}
dt:before {
counter-increment: term;
content: counter(term) ". ";
}
dd:before {
counter-increment: definition;
content: counter(term) "." counter(definition) " ";
}
This example is similar to the first one, but in this case we have two counters, term
anddefinition
. The scope of both counters is set by the first rule and “lives” in the dl
element. The second rule inserts the first counter before each definition term in the list. This rule is not particularly interesting because its effect is already known. Instead, the last rule is the heart of our code because it does the following:
- increments the second counter (
definition
) ondd
elements; - inserts the first counter (
term
), followed by a period; - inserts the second counter (
definition
), followed by a space.
content
property used on the :before
pseudo-element that is attached to the definition term.
Another interesting point is that counters are “self-nesting,” in the sense that resetting a counter on a descendant element (or pseudo-element) automatically creates a new instance of the counter. This is useful in the case of (X)HTML lists, where elements may be nested with arbitrary depth. However, specifying a different counter for each list is not always possible because it might produce rather redundant code. For this reason, the counters()
function is useful. This function creates a string that contains all of the counters with the same name of the given counter in the scope. Counters are then separated by a string. Take the following markup:
<ol>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
<li>item
<ol>
<li>item</li>
<li>item</li>
</ol>
</li>
</ol>
</li>
</ol>
The following CSS will number the nested list items as 1, 1.1, 1.1.1, etc.
ol {
counter-reset: item;
list-style: none;
}
li {
display: block;
}
li:before {
counter-increment: item;
content: counters(item, ".") " ";
}
In this example, we have only the item
counter for each nested level. Instead of writing three different counters (such as item1
, item2
, item3
) and thus creating three different scopes for each nested ol
element, we can rely on the counters()
function to achieve this goal. The second rule is important and deserves further explanation. Because ordered lists have default markers (i.e. numbers), we’d get rid of these markers by turning the list items into block-level elements. Remember that only elements with display: list-items
have markers.
Now we can look carefully at the third rule, which does the actual the work. The first declaration increments the counter previously set on the outermost list. Then, in the second declaration, the counters()
function creates all of the counter’s instances for the innermost lists. The structure of this function is as follows:
- Its first argument is the name of the given counter, immediately followed by a comma.
- Its second argument is a period between double quotation marks.
counters()
function to keep the numbers separate from the actual contents of the list items.
Counters are formatted with decimal numbers by default. However, the styles of the list-style-type
property are also available for counters. The default notation is counter(name)
(i.e. no styling) or counter(name, 'list-style-type')
to change the default formatting. In practice, the recommended styles are these:
decimal
decimal-leading-zero
lower-roman
upper-roman
lower-greek
lower-latin
upper-latin
lower-alpha
upper-alpha
lower-latin
after 26 list items is undefined. Thus, numerals are recommended for long lists:
dl {
counter-reset: term definition;
}
dt:before {
counter-increment: term;
content: counter(term, upper-latin) ". ";
}
dd:before {
counter-increment: definition;
content: counter(definition, lower-latin) ". ";
}
We can also add styles to the counters()
function:
li:before {
counter-increment: item;
content: counters(item, ".", lower-roman) " ";
}
Note that the counters()
function also accepts a third argument (lower-roman
) as the last item in its arguments list, separated from the preceding period by a second comma. However, the counters()
function doesn’t allow us to specify different styles for each level of nesting.
Conclusion
With the new generation of browsers, we can use CSS-generated content to embellish our layouts with strings and graphics. Generated content, then, is surely an excellent tool that every developer should learn.Revisions
- April 16, 2013 @ 11:08:57 [Current Revision] by PeterLugg
- April 16, 2013 @ 11:14:02 [Autosave] by PeterLugg
- April 16, 2013 @ 11:07:52 by PeterLugg
Revision Differences
April 16, 2013 @ 11:07:52 | Current Revision | ||
---|---|---|---|
Content | |||
Added: This amazing content is taken from here: <a href="http:// coding.smashingmagazine.com/ 2013/04/12/css-generated- content-counters/" target="_blank">Smashing Magazine</a> | |||
Added: <h3>What Is Generated Content?</h3> | |||
Added: In technical terms, generated content is a simple abstraction created by CSS in the document tree. As such, in practical terms, generated content exists only in the layout of the Web document. | |||
Added: Accessing generated content via JavaScript is possible by reading the textual value of the<code>content< /code> property: | |||
Added: <pre><code>var test = document.querySelector('#test'); | |||
Added: var result = getComputedStyle(test, ':before').content; | |||
Added: var output = document.querySelector('#output'); | |||
Added: output.innerHTML = result;</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/QSNeJ/">See example</a></li> | |||
Added: </ul> | |||
Added: <h3>Inserting Generated Content</h3> | |||
Added: Generated content may be inserted before and after the actual content of an element, using the <code>:before< /code> and < code>:after< /code> pseudo-elements, respectively. To represent the pseudo-elements, we can use the following pseudo-markup. | |||
Added: <pre><code><p> | |||
Added: <before> Start</before> | |||
Added: Actual content | |||
Added: <after> End</after> | |||
Added: </p></code></pre> | |||
Added: And our CSS would be: | |||
Added: <pre><code>p:before { | |||
Added: content: "Start"; | |||
Added: } | |||
Added: p:after { | |||
Added: content: "End"; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/d9863/">See example</a></li> | |||
Added: </ul> | |||
Added: Bear in mind that if <strong>you are validating the CSS file against the CSS3 specifications</strong>, the<code>:before< /code> and < code>:after< /code> pseudo-elements should be written as <code>::before< /code> and < code>::after</code>. Otherwise, the CSS validator will call an error. | |||
Added: As you can see, the property that inserts the two strings is <code>content</code>. This | |||
Added: property accepts the following values: | |||
Added: <ul> | |||
Added: <li><code>none< /code>, <code> normal</code> | |||
Added: The pseudo-content would not be generated.</li> | |||
Added: <li><code>< string></code> | |||
Added: This would be a textual string enclosed in quotation marks.</li> | |||
Added: <li><code>url()</code> | |||
Added: This function enables us to insert an external resource (usually an image), as with the<code>background- image</code> property.</li> | |||
Added: <li><code>counter( )</code>, <code> counters()</code> | |||
Added: These functions insert counters (see below for details).</li> | |||
Added: <li><code>attr( attribute)</code> | |||
Added: This function enables us to insert the value of <code>attribute</code> of a given element.</li> | |||
Added: <li><code>open- quote</code>, <code>close- quote</code>, < code>no-open- quote</code>, < code>no-close- quote</code> | |||
Added: These values automate the generation of quotation marks.</li> | |||
Added: </ul> | |||
Added: <strong>Keep in mind that generated content takes up space on the page</strong>, and its presence affects the browser’s computation of the parent element. | |||
Added: <h3>Inserting Strings</h3> | |||
Added: In the previous example, we inserted two simple strings before and after the actual content of the element. Generated content also enables us to insert more complex symbols, through escaping: | |||
Added: <pre><code>p:before { | |||
Added: content: "\00A7"; | |||
Added: padding-right: 0.2em; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/69XSC/">See example</a></li> | |||
Added: </ul> | |||
Added: The escaped sequence between the double quotation marks is the hexadecimal Unicode value of the paragraph symbol. <strong>We can also combine simple strings with Unicode symbols:</strong> | |||
Added: <pre><code>p:before { | |||
Added: content: "( " "\00A7" " )"; | |||
Added: padding-right: 0.2em; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/umgSs/">See example</a></li> | |||
Added: </ul> | |||
Added: In case you need it, a comprehensive list of all Unicode characters is <a href="http:// www.alanwood.net/ unicode/">available on Alan Wood’s website</a>. | |||
Added: Note that all textual content inside the <code>content< /code> property is treated literally. So, spaces and tabs inserted via the keyboard will be inserted on the page as well. | |||
Added: <h3>Inserting Icons Using Web Fonts</h3> | |||
Added: Web fonts can be used to insert graphical icons through generated content. Depending on the Web font family, you can insert either simple letters or Unicode sequences: | |||
Added: <pre><code>@import url(http://weloveiconfonts.com/ api/?family=brandico); | |||
Added: p:before { | |||
Added: content: "\f303"; | |||
Added: padding-right: 0.3em; | |||
Added: font-family: 'brandico', sans-serif; | |||
Added: font-size: 22px; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/F786N/">See example</a></li> | |||
Added: </ul> | |||
Added: In this example, we have inserted a Twitter icon. Our code could be rewritten as follows: | |||
Added: <pre><code>.icon- twitter:before { | |||
Added: content: "\f303"; | |||
Added: padding-right: 0.3em; | |||
Added: font-family: 'brandico', sans-serif; | |||
Added: font-size: 22px; | |||
Added: }</code></pre> | |||
Added: <h3>Inserting Images</h3> | |||
Added: We can insert images through the <code>url( )</code> function. | |||
Added: <pre><code>a:before { | |||
Added: content: url(link.png); | |||
Added: padding-right: 0.2em; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/WSwWJ/">See example</a></li> | |||
Added: </ul> | |||
Added: As you can see, this function has the same syntax as the <code>background- image</code> property. | |||
Added: <h3>Inserting Attribute Values</h3> | |||
Added: An attribute value of an element can be inserted through the <code>attr( )</code> function. | |||
Added: <pre><code>a[href]:after { | |||
Added: content: "( " attr(href) " )"; | |||
Added: padding-left: 0.2em; | |||
Added: color: #000; | |||
Added: font: small "Courier New", Courier, monospace; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/jjuae/">See example</a></li> | |||
Added: </ul> | |||
Added: We’ve just inserted the value of the <code>href< /code> attribute, which is a simple text string. | |||
Added: <h3>Inserting Counters</h3> | |||
Added: The automatic numbering of CSS is controlled by two properties, | |||
Added: <code>counter- reset</code> and <code> counter-increment</code>. Counters defined by these properties are then used with the <code>counter( )</code> and < code>counters( )</code> functions of the <code>content< /code> property. | |||
Added: The <code>counter- reset</code> property may contain one or more names of counters (i.e. “identifiers”), optionally followed by an integer. The integer sets the value that will be incremented by the<code>counter- increment</code> property for any occurence of the given element. The default value is 0. Negative values are allowed. | |||
Added: The <code>counter- increment</code> property is similar. The basic difference is that this one increments a counter. Its default increment is 1. Negative values are allowed. | |||
Added: <strong>Now we are ready for an example.</strong> Take the following markup: | |||
Added: <pre><code><dl> | |||
Added: <dt>term</dt> | |||
Added: <dd>description</dd> | |||
Added: <dt>term</dt> | |||
Added: <dd>description</dd> | |||
Added: <dt>term</dt> | |||
Added: <dd>description</dd> | |||
Added: </dl></code></pre> | |||
Added: We want to add progressive numbering (1, 2, 3, etc.) to each definition term (<code>dt</code>) in the list. Here is the CSS: | |||
Added: <pre><code>dl { | |||
Added: counter-reset: term; | |||
Added: } | |||
Added: dt:before { | |||
Added: counter-increment: term; | |||
Added: content: counter(term); | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/hBmVA/">See example</a></li> | |||
Added: </ul> | |||
Added: <strong>The first rule</strong> here sets a counter for the definition list. This is called a “scope.” The name (or identifier) of the counter is <code>term</code>. Whatever name we choose for our counter must be identical to the one in the <code>counter- increment</code> property (of course, the name should be meaningful). | |||
Added: In <strong>the second rule</strong>, we attach the <code>:before< /code> pseudo-element to the <code>dt< /code> element, because we want to insert the counter precisely before the actual content of the element. Let’s take a closer look at the second declaration of the second rule. The <code>counter( )</code> function accepts our identifier (<code>term</code>) as its argument, and the <code>content< /code> property generates the counter. | |||
Added: There’s no space between the number and the content of the element. If we want to add a space and, say, a period after the number, we could insert the following string in the <code>content< /code>property: | |||
Added: <pre><code>dt:before { | |||
Added: content: counter(term) ". "; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/CWTTZ/">See example</a></li> | |||
Added: </ul> | |||
Added: Note that the string between the quotation marks is treated literally; that is, the space after the period is inserted just as we’ve typed it on the keyboard. In fact, the <code>content< /code> property can be regarded as the CSS counterpart of the JavaScript < code>document.write( )</code> method, except that it doesn’t add real content to the document. Simply put, the <code>content< /code> property creates a mere abstraction in the document tree but doesn’t modify it. | |||
Added: In case you’re wondering, <strong>we can add more styles to counters by applying other properties</strong> to the attached pseudo-element. For example: | |||
Added: <pre><code>dt:before { | |||
Added: content: counter(term); | |||
Added: padding: 1px 2px; | |||
Added: margin-right: 0.2em; | |||
Added: background: #ffc; | |||
Added: color: #000; | |||
Added: border: 1px solid #999; | |||
Added: font-weight: bold; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/tJU7A/">See example</a></li> | |||
Added: </ul> | |||
Added: We’ve just set a background color, added some padding and a right margin, made the font bold, and outlined the counters with a thin solid border. Now our counters are a little more attractive. | |||
Added: Furthermore, counters may be negative. When dealing with negative counters, we should adhere to a little math — namely, the part about adding and subtracting negative and positive numbers. For example, if we need progressive numbering starting from 0, we could write the following: | |||
Added: <pre><code>dl { | |||
Added: counter-reset: term -1; | |||
Added: } | |||
Added: dt:before { | |||
Added: counter-increment: term; | |||
Added: content: counter(term) ". "; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/GJPZq/">See example</a></li> | |||
Added: </ul> | |||
Added: By setting the <code>counter- reset</code> property to -1 and incrementing it by 1, the resulting value is 0, and the numbering will start from that value. Negative counters may be combined with positive counters to interesting effect. Consider this example: | |||
Added: <pre><code>dl { | |||
Added: counter-reset: term -1; | |||
Added: } | |||
Added: dt:before { | |||
Added: counter-increment: term 3; | |||
Added: content: counter(term) ". "; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/D6bBU/">See example</a></li> | |||
Added: </ul> | |||
Added: As you can see, adding and subtracting negative and positive numbers yield a wide range of combinations between counters. With just a simple set of calculations, <strong>we get complete control over automatic numbering</strong>. | |||
Added: Another interesting feature of CSS counters lies in their ability to be nested. In fact, numbering may also be ordered by progressive sublevels, such as 1.1, 1.1.1, 2.1 and so on. To add a sublevel to the elements in our list, we would write the following: | |||
Added: <pre><code>dl { | |||
Added: counter-reset: term definition; | |||
Deleted: | Added: } | ||
Added: dt:before { | |||
Added: counter-increment: term; | |||
Added: content: counter(term) ". "; | |||
Added: } | |||
Added: dd:before { | |||
Added: counter-increment: definition; | |||
Added: content: counter(term) "." counter(definition) " "; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/Ampkg/">See example</a></li> | |||
Added: </ul> | |||
Added: This example is similar to the first one, but in this case we have two counters, <code> term</code> and< code>definition</code>. The scope of both counters is set by the first rule and “lives” in the <code>dl< /code> element. The second rule inserts the first counter before each definition term in the list. This rule is not particularly interesting because its effect is already known. Instead, the last rule is the heart of our code because it does the following: | |||
Added: <ol> | |||
Added: <li>increments the second counter (<code>definition</code>) on <code>dd< /code> elements;</li> | |||
Added: <li>inserts the first counter (<code>term</code>), followed by a period;</li> | |||
Added: <li>inserts the second counter (<code>definition</code>), followed by a space.</li> | |||
Added: </ol> | |||
Added: Note that steps 2 and 3 are both performed by the <code>content< /code> property used on the <code>:before< /code>pseudo-element that is attached to the definition term. | |||
Added: <strong>Another interesting point is that counters are “self-nesting,”< /strong> in the sense that resetting a counter on a descendant element (or pseudo-element) automatically creates a new instance of the counter. This is useful in the case of (X)HTML lists, where elements may be nested with arbitrary depth. However, specifying a different counter for each list is not always possible because it might produce rather redundant code. For this reason, the <code>counters( )</code> function is useful. This function creates a string that contains all of the counters with the same name of the given counter in the scope. Counters are then separated by a string. Take the following markup: | |||
Added: <pre><code><ol> | |||
Added: <li>item</li> | |||
Added: <li>item | |||
Added: <ol> | |||
Added: <li>item</li> | |||
Added: <li>item</li> | |||
Added: <li>item | |||
Added: <ol> | |||
Added: <li>item</li> | |||
Added: <li>item</li> | |||
Added: </ol> | |||
Added: </li> | |||
Added: </ol> | |||
Added: </li> | |||
Added: </ol></code></pre> | |||
Added: The following CSS will number the nested list items as 1, 1.1, 1.1.1, etc. | |||
Added: <pre><code>ol { | |||
Added: counter-reset: item; | |||
Added: list-style: none; | |||
Added: } | |||
Added: li { | |||
Added: display: block; | |||
Added: } | |||
Added: li:before { | |||
Added: counter-increment: item; | |||
Added: content: counters(item, ".") " "; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/7mcWp/">See example</a></li> | |||
Added: </ul> | |||
Added: In this example, we have only the <code>item< /code> counter for each nested level. Instead of writing three different counters (such as <code>item1< /code>, <code> item2</code> , <code>item3</code>) and thus creating three different scopes for each nested <code> ol</code> element, we can rely on the <code>counters( )</code> function to achieve this goal. The second rule is important and deserves further explanation. Because ordered lists have default markers (i.e. numbers), we’d get rid of these markers by turning the list items into block-level elements. Remember that only elements with <code>display: list-items</code> have markers. | |||
Added: Now we can look carefully at <strong>the third rule</strong>, which does the actual the work. The first declaration increments the counter previously set on the outermost list. Then, in the second declaration, the <code>counters( )</code> function creates all of the counter’s instances for the innermost lists. The structure of this function is as follows: | |||
Added: <ol> | |||
Added: <li>Its first argument is the name of the given counter, immediately followed by a comma.</li> | |||
Added: <li>Its second argument is a period between double quotation marks.</li> | |||
Added: </ol> | |||
Added: Note that we’ve inserted a space after the <code>counters( )</code> function to keep the numbers separate from the actual contents of the list items. | |||
Added: Counters are formatted with decimal numbers by default. However, the styles of the <code>list- style-type</code> property are also available for counters. The default notation is <code>counter( name)</code>(i.e. no styling) or <code>counter(name, 'list-style-type')</code> to change the default formatting. In practice, the recommended styles are these: | |||
Added: <ul> | |||
Added: <li><code>decimal</code></li> | |||
Added: <li><code>decimal- leading-zero</code></li> | |||
Added: <li><code>lower- roman</code></li> | |||
Added: <li><code>upper- roman</code></li> | |||
Added: <li><code>lower- greek</code></li> | |||
Added: <li><code>lower- latin</code></li> | |||
Added: <li><code>upper- latin</code></li> | |||
Added: <li><code>lower- alpha</code></li> | |||
Added: <li><code>upper- alpha</code></li> | |||
Added: </ul> | |||
Added: Don’t forget that we’re working with numeric systems. Also remember that the specification doesn’t define how to render an alphabetical system beyond the end of an alphabet. For example, the rendering of <code>lower- latin</code> after 26 list items is undefined. Thus, numerals are recommended for long lists: | |||
Added: <pre><code>dl { | |||
Added: counter-reset: term definition; | |||
Added: } | |||
Added: dt:before { | |||
Added: counter-increment: term; | |||
Added: content: counter(term, upper-latin) ". "; | |||
Added: } | |||
Added: dd:before { | |||
Added: counter-increment: definition; | |||
Added: content: counter(definition, lower-latin) ". "; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/W6HYP/">See example</a></li> | |||
Added: </ul> | |||
Added: We can also add styles to the <code>counters( )</code> function: | |||
Added: <pre><code>li:before { | |||
Added: counter-increment: item; | |||
Added: content: counters(item, ".", lower-roman) " "; | |||
Added: }</code></pre> | |||
Added: <ul> | |||
Added: <li><a href="http:// jsfiddle.net/ gabrieleromanato/wLtAf/">See example</a></li> | |||
Added: </ul> | |||
Added: Note that the <code>counters( )</code> function also accepts a third argument (<code>lower- roman</code>) as the last item in its arguments list, separated from the preceding period by a second comma. However, the <code>counters( )</code> function doesn’t allow us to specify different styles for each level of nesting. | |||
Added: <h3>Conclusion</h3> | |||
Added: With the new generation of browsers, we can use CSS-generated content to embellish our layouts with strings and graphics. Generated content, then, is surely an excellent tool that every developer should learn. |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.