content - CSS(层叠样式表) 编辑
概要
CSS的 content
CSS 属性用于在元素的 ::before
和 ::after
伪元素中插入内容。使用content
属性插入的内容都是匿名的可替换元素。
初始值 | normal |
---|---|
适用元素 | ::before and ::after pseudo-elements |
是否是继承属性 | 否 |
适用媒体 | 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. |
Animation type | discrete |
正规顺序 | the unique non-ambiguous order defined by the formal grammar |
语法
Formal syntax: 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-tags>? [ <image-src>? , <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() <a="" href="/wiki/zh-CN/docs/CSS/Value_definition_syntax" title="Single bar: exactly one of the entities must be present">| <conic-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></repeating-radial-gradient()>where
<image-tags> = ltr | rtl
<image-src> = <url> | <string>
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
<image-set-option> = [ <image> | <string> ] <resolution>
<id-selector> = <hash-token>
<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> )
<conic-gradient()> = conic-gradient( [ from <angle> ]? [ at <position> ]?, <angular-color-stop-list> )
<counter-style> = <counter-style-name> | symbols()where
<rgb()> = rgb( <percentage>{3} [ / <alpha-value> ]? ) | rgb( <number>{3} [ / <alpha-value> ]? ) | rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? )
<rgba()> = rgba( <percentage>{3} [ / <alpha-value> ]? ) | rgba( <number>{3} [ / <alpha-value> ]? ) | rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? )
<hsl()> = hsl( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsl( <hue>, <percentage>, <percentage>, <alpha-value>? )
<hsla()> = hsla( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsla( <hue>, <percentage>, <percentage>, <alpha-value>? )
<side-or-corner> = [ left | right ] || [ top | bottom ]
<color-stop-list> = [ <linear-color-stop> [, <linear-color-hint>]? ]# , <linear-color-stop>
<ending-shape> = circle | ellipse
<size> = closest-side | farthest-side | closest-corner | farthest-corner | <length> | <length-percentage>{2}
<position> = [ [ left | center | right ] || [ top | center | bottom ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ]? | [ [ left | right ] <length-percentage> ] && [ [ top | bottom ] <length-percentage> ] ]
<angular-color-stop-list> = [ <angular-color-stop> [, <angular-color-hint>]? ]# , <angular-color-stop>
<counter-style-name> = <custom-ident>where
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>
<linear-color-stop> = <color> && <color-stop-length>
<linear-color-hint> = <length-percentage>
<length-percentage> = <length> | <percentage>
<angular-color-stop> = <color> && <color-stop-angle>?
<angular-color-hint> = <angle-percentage>where
<color-stop-length> = <length-percentage>{1,2}
<color-stop-angle> = <angle-percentage>{1,2}
<angle-percentage> = <angle> | <percentage>
content: normal /* Keywords that cannot be combined with other values */ content: none content: 'prefix' /* <string> value, non-latin characters must be encoded e.g. \00A0 for */ content: url(http://www.example.com/test.html) /* <uri> value */ content: chapter_counter /* <counter> values */ content: attr(value string) /* attr() value linked to the HTML attribute value */ content: open-quote /* Language- and position-dependant keywords */ content: close-quote content: no-open-quote content: no-close-quote content: open-quote chapter_counter /* Except for normal and none, several values can be used simultaneously */ content: inherit
值
none
- 不会产生伪类元素
normal
-
:before
和:after
伪类元素中会被视为none
- <string>
- 文本内容
- <uri>
url()
- URI值会指定一个外部资源(比如图片)。如果该资源或图片不能显示,它就会被忽略或显示一些占位(比如无图片标志)。
- <counter>
- 计数器可以指定两种不同的函数:
counter()
或counters()
。前面一个有两种形式:counter(name)
或counter(name,style)
。产生的内容是该伪类元素指定名称的最小范围的计数;格式由style指定(默认是'decimal'——十进制数字)。后一个函数同样也有两种形式:counters(name,string)
或counters(name,string,style)
。The generated text is the value of all counters with the given name in scope at this pseudo-element, from outermost to innermost separated by the specified string. The counters are rendered in the indicated style ('decimal' by default). See the section on automatic counters and numbering for more information. The name must not be 'none', 'inherit' or 'initial'. Such a name causes the declaration to be ignored. attr(X)
- 将元素的X属性以字符串形式返回。如果该元素没有 X 属性,则返回一个空字符串。区分大小写的属性返回值依赖文挡的语言设定。
open-quote
|close-quote
- 这些值会被
quotes
中定义的字符串替换。 no-open-quote
|no-close-quote
- 不会生产任何内容,但是会改变(增加或降低)引号层级。
示例
代码举例 - 标题和引号
HTML
<h1>5</h1>
<p> We shall start this with a quote from 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> Here we shall quote 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> And so, we can infer that contributing to the open web, can protect our own individual experiences on it.
</p>
CSS
q {
color: #00008B;
font-style: italic;
}
q::before { content: open-quote }
q::after { content: close-quote }
h1::before { content: "Chapter "; }
输出
代码举例 - link前面加一个icon
HTML
<a href="http://www.mozilla.org/en-US/">Home Page</a>
CSS
a::before{
content: url(http://www.mozilla.org/favicon.ico) " MOZILLA: ";
font: x-small Arial,freeSans,sans-serif;
color: gray;
}
输出
代码举例 - 自定义列表后面加加文字
HTML
<div>
<ul class="brightIdea">
<li>This is my first idea</li>
<li>and another good idea</li>
</ul>
</div>
CSS
/* first import the icon from a suitable site */
@import url(http://weloveiconfonts.com/api/?family=entypo);
.brightIdea li::after{
content: '\1f4a1';
font-family: 'entypo', sans-serif;
}
输出
Code sample - class based example
HTML Content
<h2>Paperback best sellers</h2>
<ol>
<li>Political thriller</li>
<li class="newEntry">Halloween Stories</li>
<li>My Biography</li>
<li class="newEntry">Vampire Romance</li>
</ol>
CSS Content
/* use a class rather that an element selector to give more flexibility.
Simple string example, but don't forget add a leading space in the text string
for spacing purposes */
.newEntry::after {
content: " New!";
color: red;
}
Output
Code sample - rich link styling
HTML Content
<ul>
<li><a id="moz" href="http://www.mozilla.org/">
Mozilla Home Page</a></li>
<li><a id="mdn" href="/wiki/">
Mozilla Developer Network</a></li>
<li><a id="w3c" href="http://www.w3c.org/">
World Wide Web Consortium</a></li>
</ul>
CSS Content
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://www.wenjiangs.com/wp-content/uploads/2020/mozilla/mdn-favicon16.png) ;
}
#w3c::before {
content:url(http://w3c.org/2008/site/images/favicon.ico) ;
}
li {
margin: 1em;
}
Output
规范
Specification | Status | Comment |
---|---|---|
CSS Level 2 (Revision 1) content | Recommendation |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1.0 | 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 | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1.0) | 8.0 | 9.5 | 1.0 |
参考
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论