Contents
In some cases, authors may want user agents to render contentthat does not come from thedocumenttree. One familiar example of this is a numbered list; the authordoes not want to list the numbers explicitly, he or she wants theuser agent to generate them automatically. Similarly, authorsmay want the user agent to insert the word"Figure" before the caption of a figure, or "Chapter 7" before theseventh chapter title. For audio or braille in particular, user agentsshould be able to insert these strings.
In CSS 2.1, content may be generated by two mechanisms:
Authors specify the style and location of generated content withthe :before and :after pseudo-elements. As their names indicate, the:before and :after pseudo-elements specify the location of contentbefore and after an element'sdocumenttree content. The'content'property, in conjunction with these pseudo-elements, specifies what isinserted.
Example(s):
For example, the following rule inserts the string "Note: "before the content of every P element whose "class" attribute has the value "note":
p.note:before { content: "Note: " }
The formatting objects (e.g., boxes) generated by an element includegenerated content. So, for example, changing the above style sheetto:
p.note:before { content: "Note: " }p.note { border: solid green }
would cause a solid green border to be rendered around the entireparagraph, including the initial string.
The :before and :after pseudo-elementsinherit any inheritable propertiesfrom the element in the document tree to which they are attached.
Example(s):
For example, the following rules insert an open quote mark before everyQ element. The color of the quote mark will be red, but the font willbe the same as the font of the rest of the Q element:
q:before { content: open-quote; color: red}
In a :before or :after pseudo-element declaration, non-inheritedproperties take theirinitialvalues.
Example(s):
So, for example, because the initial value of the
body:after { content: "The End"; display: block; margin-top: 2em; text-align: center;}
The :before and :after pseudo-elements interact with otherboxes as if they were real elements inserted justinside their associated element.
Example(s):
For example, the following document fragment and style sheet:
<p> Text </p> p:before { display: block; content: 'Some'; }
...would render in exactly the same way as the following documentfragment and style sheet:
<p><span>Some</span> Text </p> span { display: block }
Similarly, the following document fragment and style sheet:
<h2> Header </h2> h2:after { display: block; content: 'Thing'; }
...would render in exactly the same way as the following documentfragment and style sheet:
<h2> Header <span>Thing</span></h2> h2 { display: block; } span { display: block; }
Note. This specification does not fully define the interaction of :beforeand :after with replaced elements (such as IMG in HTML). This willbe defined in more detail in a future specification.
Value: | normal | none | [<string> |<uri> |<counter> | attr(<identifier>) | open-quote | close-quote | no-open-quote | no-close-quote ]+ |inherit |
Initial: | normal |
Applies to: | :before and :after pseudo-elements |
Inherited: | no |
Percentages: | N/A |
Media: | all |
Computed value: | On elements, always computes to 'normal'. On :before and :after, if 'normal' is specified, computes to 'none'. Otherwise, for URI values, the absolute URI; for attr() values, the resulting string; for other keywords, as specified. |
This property is used with the :before and :after pseudo-elementsto generate content in a document. Values have the followingmeanings:
The'display' propertycontrols whether the content is placed in a block or inline box.
Example(s):
The following rule causes the string "Chapter: " to be generated before each H1 element:
H1:before { content: "Chapter: "; display: inline;}
Authors may include newlines in the generated content by writingthe "\A" escape sequence in one of the strings after the
Example(s):
h1:before { display: block; text-align: center; white-space: pre; content: "chapter\A hoofdstuk\A chapitre"}
Generated content does not alter the document tree. In particular, itis not fed back to the document language processor (e.g., forreparsing).
In CSS 2.1, authors may specify, in a style-sensitive andcontext-dependent manner, how user agents should render quotationmarks. The'quotes' propertyspecifies pairs of quotation marks for each level of embeddedquotation. The'content'property gives access to those quotation marks and causes them to beinserted before and after a quotation.
This property specifies quotation marks for any number of embeddedquotations. Values have the following meanings:
Example(s):
For example, applying the following style sheet:
/* Specify pairs of quotes for two levels in two languages */q:lang(en) { quotes: '"' '"' "'" "'" }q:lang(no) { quotes: "«" "»" '"' '"' }/* Insert quotes before and after Q element content */q:before { content: open-quote }q:after { content: close-quote }
to the following HTML fragment:
<HTML lang="en"> <HEAD> <TITLE>Quotes</TITLE> </HEAD> <BODY> <P><Q>Quote me!</Q> </BODY></HTML>
would allow a user agent to produce:
"Quote me!"
while this HTML fragment:
<HTML lang="no"> <HEAD> <TITLE>Quotes</TITLE> </HEAD> <BODY> <P><Q>Trøndere gråter når <Q>Vinsjan på kaia</Q> blir deklamert.</Q> </BODY></HTML>
would produce:
«Trøndere gråter når "Vinsjan på kaia" blir deklamert.»
Note.While the quotation marks specified by
Character | Approximate rendering | ISO 10646 code (hex) | Description |
---|---|---|---|
" | " | 0022 | QUOTATION MARK [the ASCII double quotation mark] |
' | ' | 0027 | APOSTROPHE [the ASCII single quotation mark] |
‹ | < | 2039 | SINGLE LEFT-POINTING ANGLE QUOTATION MARK |
› | > | 203A | SINGLE RIGHT-POINTING ANGLE QUOTATION MARK |
« | « | 00AB | LEFT-POINTING DOUBLE ANGLE QUOTATION MARK |
» | » | 00BB | RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK |
‘ | ` | 2018 | LEFT SINGLE QUOTATION MARK [single high-6] |
’ | ' | 2019 | RIGHT SINGLE QUOTATION MARK [single high-9] |
“ | `` | 201C | LEFT DOUBLE QUOTATION MARK [double high-6] |
” | '' | 201D | RIGHT DOUBLE QUOTATION MARK [double high-9] |
„ | ,, | 201E | DOUBLE LOW-9 QUOTATION MARK [double low-9] |
Quotation marks are inserted in appropriate places in a documentwith the'open-quote'and'close-quote' values of the'content' property. Eachoccurrence of 'open-quote' or 'close-quote' is replaced by one of thestrings from the value of
'Open-quote' refers to the first of a pair of quotes, 'close-quote'refers to the second. Which pair of quotes is used depends on thenesting level of quotes: the number of occurrences of 'open-quote' inall generated text before the current occurrence, minus the number ofoccurrences of 'close-quote'. If the depth is 0, the first pair isused, if the depth is 1, the second pair is used, etc. If the depth isgreater than the number of pairs, the last pair is repeated. A 'close-quote'or 'no-close-quote' that would make the depth negative is in error and isignored (at rendering time): the depth stays at 0 and no quote mark isrendered (although the rest of the 'content' property's value is stillinserted).
Note. The quoting depth is independent of the nestingof the source document or the formatting structure.
Some typographic styles require open quotation marks to be repeatedbefore every paragraph of a quote spanning several paragraphs, butonly the last paragraph ends with a closing quotation mark. In CSS,this can be achieved by inserting "phantom" closing quotes. Thekeyword'no-close-quote' decrementsthe quoting level, but does not insert a quotation mark.
Example(s):
The following style sheet puts opening quotation marks on everyparagraph in a BLOCKQUOTE, and inserts a single closing quote at theend:
blockquote p:before { content: open-quote }blockquote p:after { content: no-close-quote }blockquote p.last:after { content: close-quote }
This relies on the last paragraph being marked with a class "last".
For symmetry, there is also a Automatic numbering in CSS 2.1 is controlled with two properties,'counter-increment'and'counter-reset'. Thecounters defined by these properties are used with the counter() andcounters() functions of the the The The'counter-reset'property also contains a list of one or more names of counters, eachone optionally followed by an integer. The integer gives the value thatthe counter is set to on each occurrence of the element. The defaultis 0. The keywords 'none', 'inherit' and 'initial' must not be used ascounter names. A value of 'none' on its own means no counters arereset, resp. incremented. 'Inherit' on its own has its usual meaning(see6.2.1). 'Initial' isreserved for future use. Example(s): This example shows a way to number chapters and sections with"Chapter 1", "1.1", "1.2", etc. If an element increments/resets a counter and also uses it (in the'content' property of its:before or :after pseudo-element), the counter is usedafterbeing incremented/reset. If an element both resets and increments a counter, the counter isreset first and then incremented. If the same counter is specified more than once in the value of the'counter-reset' and Example(s): The following example will reset the 'section' counter to 0: The following example will increment the 'chapter' counter by 3: The'counter-reset'property follows the cascading rules. Thus, due to cascading, thefollowing style sheet: will only reset 'imagenum'. To reset both counters, they have to bespecified together: Counters are "self-nesting", in the sense that resetting a counterin a descendant element or pseudo-element automatically creates a newinstance of the counter. This is important for situations like listsin HTML, where elements can be nested inside themselves to arbitrarydepth. It would be impossible to define uniquely named counters foreach level. Example(s): Thus, the following suffices to number nested list items. Theresult is very similar to that of setting 'display:list-item' and'list-style: inside' on the LI element: Thescopeof a counter starts at the first element in the document that has a'counter-reset' for thatcounter and includes the element's descendants and its followingsiblings with their descendants. However, it does not include anyelements in the scope of a counter with the same name created by a 'counter-reset' on alater sibling of the element or by a later 'counter-reset' on the sameelement. If In the example above, an OL will create a counter, and all childrenof the OL will refer to that counter. If we denote by item[n] thenth instance of the "item"counter, and by "{" and "}" the beginning and end of ascope, then the following HTML fragment will use the indicatedcounters. (We assume the style sheet as given in the example above). Example(s): Another example, showing how scope works when counters are used onelements that are not nested, is the following. This shows how thestyle rules given above to number chapters and sections would apply tothe markup given. The 'counters()' function generates a string composed of all of thecounters with the same name that are in scope, separated by a givenstring. Example(s): The following style sheet numbers nested list items as "1", "1.1", "1.1.1", etc. By default, counters are formatted with decimal numbers, but all thestyles available for the for the default style, or: All the styles are allowed, including 'disc', 'circle', 'square',and 'none'. Example(s): An element that is not displayed ( Example(s): For example, with the following style sheet,H2s with class "secret" do not increment 'count2'. Pseudo-elements that are not generated also cannot increment orreset a counter. Example(s): For example, the following does not increment 'heading': Elements with'visibility'set to 'hidden', on the other hand,do increment counters. CSS 2.1 offers basic visual formatting of lists. An element with'display: list-item' generates aprincipal block box for the element'scontent and, depending on the values of 'list-style-type' and'list-style-image', possibly also a marker box as a visual indicationthat theelement is a list item. Thelistproperties describe basic visual formatting of lists:they allow style sheets to specify the marker type (image, glyph, ornumber), and the marker position with respect to the principal box(outside it or within it before content). They do not allow authors tospecify distinct style (colors, fonts, alignment, etc.) for the listmarker or adjust its position with respect to the principal box; thesemay be derived from the principal box. Thebackgroundproperties apply to the principal box only; an 'outside' markerbox is transparent. This property specifies appearance of the list item marker if'list-style-image' hasthe value 'none' or if the image pointed to by the URI cannot bedisplayed. The value 'none' specifies no marker, otherwise there arethree types of marker: glyphs, numbering systems, and alphabeticsystems. Glyphs are specified withdisc,circle, andsquare. Their exactrendering depends on the user agent. Numbering systems are specified with: Alphabetic systems are specified with: This specification does not define how alphabetic systems wrap atthe end of the alphabet. For instance, after 26 list items,'lower-latin' rendering is undefined. Therefore, for long lists, werecommend that authors specify true numbers. CSS 2.1 does not define how the list numbering is reset andincremented. This is expected to be defined in the CSS List Module[CSS3LIST]. For example, the following HTML document: might produce something like this: The list marker alignment (here, right justified) depends on the user agent. This property sets the image that will be used as the list itemmarker. When the image is available, it will replace the marker setwith the The size of the image is calculated from the following rules: Example(s): The following example sets the marker at the beginning of each listitem to be the image "ellipse.png". This property specifies the position of the marker box withrespect to theprincipal block box. Values have the following meanings: For example: The above example may be formatted as: In right-to-left text, the markers would have been on the rightside of the box. The'list-style' propertyis a shorthand notation for setting the three properties Example(s): Although authors may specify Authors who use only thedescendant selector maynot achieve the results they expect. Consider the following rules: The desired rendering would have level 1 list items with'lower-alpha' labels and level 2 items with 'disc' labels. However,thecascading order willcause the first style rule (which includes specific class information)to mask the second. The following rules solve the problem by employingachildselector instead: Another solution would be to specify Inheritance will transfer the Example(s): A URI value may be combined with any other value, as in: In the example above, the 'disc' will be used when the image isunavailable. A value of 'none' within the Example(s): For example, a value of 'none' for the The result is that no list-item marker is displayed.12.4 Automaticcounters and numbering
Value: [<identifier><integer>? ]+ | none |inherit Initial: none Applies to: all elements Inherited: no Percentages: N/A Media: all Computed value: as specified Value: [<identifier><integer>? ]+ | none |inherit Initial: none Applies to: all elements Inherited: no Percentages: N/A Media: all Computed value: as specified BODY { counter-reset: chapter; /* Create a chapter counter scope */}H1:before { content: "Chapter " counter(chapter) ". "; counter-increment: chapter; /* Add 1 to chapter */}H1 { counter-reset: section; /* Set section to 0 */}H2:before { content: counter(chapter) "." counter(section) " "; counter-increment: section;}
H1 { counter-reset: section 2 section }
H1 { counter-increment: chapter chapter 2 }
H1 { counter-reset: section -1 }H1 { counter-reset: imagenum 99 }
H1 { counter-reset: section -1 imagenum 99 }
12.4.1Nested counters and scope
OL { counter-reset: item }LI { display: block }LI:before { content: counter(item) ". "; counter-increment: item }
<OL> <!-- {item[0]=0 --> <LI>item</LI> <!-- item[0]++ (=1) --> <LI>item <!-- item[0]++ (=2) --> <OL> <!-- {item[1]=0 --> <LI>item</LI> <!-- item[1]++ (=1) --> <LI>item</LI> <!-- item[1]++ (=2) --> <LI>item <!-- item[1]++ (=3) --> <OL> <!-- {item[2]=0 --> <LI>item</LI> <!-- item[2]++ (=1) --> </OL> <!-- --> <OL> <!-- }{item[2]=0 --> <LI>item</LI> <!-- item[2]++ (=1) --> </OL> <!-- --> </LI> <!-- } --> <LI>item</LI> <!-- item[1]++ (=4) --> </OL> <!-- --> </LI> <!-- } --> <LI>item</LI> <!-- item[0]++ (=3) --> <LI>item</LI> <!-- item[0]++ (=4) --></OL> <!-- --><OL> <!-- }{item[0]=0 --> <LI>item</LI> <!-- item[0]++ (=1) --> <LI>item</LI> <!-- item[0]++ (=2) --></OL> <!-- -->
<!--"chapter" counter|"section" counter --><body> <!-- {chapter=0 | --> <h1>About CSS</h1> <!-- chapter++ (=1) | {section=0 --> <h2>CSS 2</h2> <!-- | section++ (=1) --> <h2>CSS 2.1</h2> <!-- | section++ (=2) --> <h1>Style</h1> <!-- chapter++ (=2) |}{ section=0 --></body> <!-- | } -->
OL { counter-reset: item }LI { display: block }LI:before { content: counters(item, ".") " "; counter-increment: item }
12.4.2Counter styles
counter(name)
counter(name, <'list-style-type'>)
H1:before { content: counter(chno, upper-latin) ". " }H2:before { content: counter(section, upper-roman) " - " }BLOCKQUOTE:after { content: " [" counter(bq, lower-greek) "]" }DIV.note:before { content: counter(notecntr, disc) " " }P:before { content: counter(p, none) }
12.4.3Counters in elements with 'display: none'
H2.secret {counter-increment: count2; display: none}
h1::before { content: normal; counter-increment: heading;}
12.5Lists
12.5.1Lists: the
Value: disc | circle | square | decimal | decimal-leading-zero | lower-roman | upper-roman | lower-greek | lower-latin | upper-latin | armenian | georgian | lower-alpha | upper-alpha | none |inherit Initial: disc Applies to: elements with 'display: list-item' Inherited: yes Percentages: N/A Media: visual Computed value: as specified <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"><HTML> <HEAD> <TITLE>Lowercase latin numbering</TITLE> <STYLE type="text/css"> ol { list-style-type: lower-roman } </STYLE> </HEAD> <BODY> <OL> <LI> This is the first item. <LI> This is the second item. <LI> This is the third item. </OL> </BODY></HTML>
i This is the first item. ii This is the second item.iii This is the third item.
Value: <uri> | none |inherit Initial: none Applies to: elements with 'display: list-item' Inherited: yes Percentages: N/A Media: visual Computed value: absolute URI or 'none' ul { list-style-image: url("http://png.com/ellipse.png") }
Value: inside | outside |inherit Initial: outside Applies to: elements with 'display: list-item' Inherited: yes Percentages: N/A Media: visual Computed value: as specified <HTML> <HEAD> <TITLE>Comparison of inside/outside position</TITLE> <STYLE type="text/css"> ul { list-style: outside } ul.compact { list-style: inside } </STYLE> </HEAD> <BODY> <UL> <LI>first list item comes first <LI>second list item comes second </UL> <UL> <LI>first list item comes first <LI>second list item comes second </UL> </BODY></HTML>
Value: [<'list-style-type'> ||<'list-style-position'> ||<'list-style-image'> ] |inherit Initial: see individual properties Applies to: elements with 'display: list-item' Inherited: yes Percentages: N/A Media: visual Computed value: see individual properties ul { list-style: upper-roman inside } /* Any "ul" element */ul > li > ul { list-style: circle outside } /* Any "ul" child of an "li" child of a "ul" element */
ol.alpha li { list-style: lower-alpha } /* Any "li" descendant of an "ol" */ ol.alpha > li { list-style: lower-alpha } /* Any "li" child of an "ol" */
<HTML> <HEAD> <TITLE>WARNING: Unexpected results due to cascade</TITLE> <STYLE type="text/css"> ol.alpha li { list-style: lower-alpha } ul li { list-style: disc } </STYLE> </HEAD> <BODY> <OL> <LI>level 1 <UL> <LI>level 2 </UL> </OL> </BODY></HTML>
ol.alpha > li { list-style: lower-alpha }ul li { list-style: disc }
ol.alpha { list-style: lower-alpha }ul { list-style: disc }
ul { list-style: url("http://png.com/ellipse.png") disc }
ul { list-style: none }
[8]ページ先頭