内容 | content
content
content
属性用于与::before
和::after
元素。使用该content
属性插入的对象是替换的匿名元素。
/* Keywords that cannot be combined with other values */
content: normal;
content: none;
/* <string> value, non-latin characters must be encoded */
/* e.g. \00A0 for */
content: "prefix";
/* <url> value */
content: url("http://www.example.com/test.png"
/* <counter> values */
content: counter(chapter_counter
content: counters(section_counter, "."
/* attr() value linked to the HTML attribute value */
content: attr(value string
/* Language- and position-dependent keywords */
content: open-quote;
content: close-quote;
content: no-open-quote;
content: no-close-quote;
/* Except for normal and none, several values */
/* can be used simultaneously */
content: open-quote chapter_counter;
/* Global values */
content: inherit;
content: initial;
content: unset;
初始值 | normal |
---|---|
应用于 | ::before and ::after pseudo-elements |
是否继承 | no |
适用媒体 | all |
计算值 | 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. |
动画类型 | discrete |
规范顺序 | the unique non-ambiguous order defined by the formal grammar |
语法
取值
none
,元素不会生成。
counter()
函数有两种形式:counter(name)' 或 'counter(name, style)'。生成的文本是给定伪元素范围内给定名称的最内层计数器的值。它被格式化为指定的样式(默认:decimal
)。
counters()
函数还有两种形式:'counters(name, string)' 或'counters(name, string, style)'。生成的文本是给定伪元素的所有具有给定名称的计数器的值,从最外层到最内层,由指定的字符串分隔。计数器以指定的样式呈现(默认情况下decimal
)。
attr(x)
元素属性的值x
作为字符串。如果没有属性x
,则返回空字符串。属性名称的区分大小写取决于文档语言。
形式语法
normal | none | [ <content-replacement> | <content-list> ] [/ <string> ]?where
<content-replacement> = <image>
<content-list> = [ <string> | contents | <image> | <quote> | <target> | <leader()> ]+
where
<image> = <url> | <image()> | <image-set()> | <element()> | <cross-fade()> | <gradient>
<quote> = open-quote | close-quote | no-open-quote | no-close-quote
<target> = <target-counter()> | <target-counters()> | <target-text()>
<leader()> = leader( <leader-type> )
where
<image()> = image( [ [ <image> | <string> ]? , <color>? ]! )
<image-set()> = image-set( <image-set-option># )
<element()> = element( <id-selector> )
<cross-fade()> = cross-fade( <cf-mixing-image> , <cf-final-image>? )
<gradient> = <linear-gradient()> | <repeating-linear-gradient()> | <radial-gradient()> | <repeating-radial-gradient()>
<target-counter()> = target-counter( [ <string> | <url> ] , <custom-ident> , <counter-style>? )
<target-counters()> = target-counters( [ <string> | <url> ] , <custom-ident> , <string> , <counter-style>? )
<target-text()> = target-text( [ <string> | <url> ] , [ content | before | after | first-letter ]? )
<leader-type> = dotted | solid | space | <string>
where
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
<image-set-option> = [ <image> | <string> ] <resolution>
<cf-mixing-image> = <percentage>? && <image>
<cf-final-image> = <image> | <color>
<linear-gradient()> = linear-gradient( [ <angle> | to <side-or-corner> ]? , <color-stop-list> )
<repeating-linear-gradient()> = repeating-linear-gradient( [ <angle> | to <side-or-corner> ]? , <color-stop-list> )
<radial-gradient()> = radial-gradient( [ <ending-shape> || <size> ]? [ at <position> ]? , <color-stop-list> )
<repeating-radial-gradient()> = repeating-radial-gradient( [ <ending-shape> || <size> ]? [ at <position> ]? , <color-stop-list> )
<counter-style> = <counter-style-name> | symbols()
where
<rgb()> = rgb( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] )
<rgba()> = rgba( [ [ <percentage>{3} | <number>{3} ] [ / <alpha-value> ]? ] | [ [ <percentage>#{3} | <number>#{3} ] , <alpha-value>? ] )
<hsl()> = hsl( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] )
<hsla()> = hsla( [ <hue> <percentage> <percentage> [ / <alpha-value> ]? ] | [ <hue>, <percentage>, <percentage>, <alpha-value>? ] )
<side-or-corner> = [ left | right ] || [ top | bottom ]
<color-stop-list> = <color-stop>#{2,}
<ending-shape> = circle | ellipse
<size> = closest-side | farthest-side | closest-corner | farthest-corner | <length> | <length-percentage>{2}
<position> = [[ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] | [ center | [ left | right ] <length-percentage>? ] && [ center | [ top | bottom ] <length-percentage>? ]]
<counter-style-name> = <custom-ident>
where
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>
<color-stop> = <color> <length-percentage>?
<length-percentage> = <length> | <percentage>
实例
标题和引号
此示例在引号周围插入引号,并在标题之前添加“章节”一词。
HTML
<h1>5</h1>
<p>According to Sir Tim Berners-Lee,
<q cite="http://www.w3.org/People/Berners-Lee/FAQ.html#Internet">I was
lucky enough to invent the Web at the time when the Internet
already existed - and had for a decade and a half.</q>
We must understand that there is nothing fundamentally wrong
with building on the contributions of others.
</p>
<h1>6</h1>
<p>According to the Mozilla Manifesto,
<q cite="http://www.mozilla.org/en-US/about/manifesto/">Individuals
must have the ability to shape the Internet and
their own experiences on the Internet.</q>
Therefore, we can infer that contributing to the open web
can protect our own individual experiences on it.
</p>
CSS
q {
color: blue;
}
q::before {
content: open-quote;
}
q::after {
content: close-quote;
}
h1::before {
content: "Chapter "; /* The trailing space creates separation
between the added content and the
rest of the content */
}
结果
图像与文本相结合
此示例在链接之前插入图像。如果找不到图像,则会插入文本。
HTML
<a href="http://www.mozilla.org/en-US/">Mozilla Home Page</a>
CSS
a::before {
content: url("https://mozorg.cdn.mozilla.net/media/img/favicon.ico") " MOZILLA: ";
font: x-small Arial, sans-serif;
color: gray;
}
结果
目标类
此示例在列表中特殊项之后插入其他文本。
HTML
<h2>Paperback Best Sellers</h2>
<ol>
<li>Political Thriller</li>
<li class="new-entry">Halloween Stories</li>
<li>My Biography</li>
<li class="new-entry">Vampire Romance</li>
</ol>
CSS
.new-entry::after {
content: " New!"; /* The leading space creates separation
between the added content and the
rest of the content */
color: red;
}
结果
图像和元素属性
本示例在每个链接之前插入图像,并id
在其后添加其属性。
HTML
<ul>
<li><a id="moz" href="http://www.mozilla.org/">
Mozilla Home Page</a></li>
<li><a id="mdn" href="https://developer.mozilla.org/">
Mozilla Developer Network</a></li>
</ul>
CSS
a {
text-decoration: none;
border-bottom: 3px dotted navy;
}
a::after {
content: " (" attr(id) ")";
}
#moz::before {
content: url("https://mozorg.cdn.mozilla.net/media/img/favicon.ico") ;
}
#mdn::before {
content: url("https://mdn.mozillademos.org/files/7691/mdn-favicon16.png") ;
}
li {
margin: 1em;
}
结果
规范
Specification | Status | Comment |
---|---|---|
CSS Generated Content Module Level 3The definition of 'content' in that specification. | Working Draft | |
CSS Level 2 (Revision 1)The definition of 'content' in that specification. | Recommendation | Initial definition |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1.0 (1.7 or earlier) | 8.0 | 4.0 | 1.0 |
url() support | 1.0 | ? | 1.0 (1.7 or earlier) | 8.0 | 7.0 | 1.0 |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1.0 (1.0) | 8.0 | 9.5 | 1.0 |
url() support | ? | ? | ? | ? | ? | ? |