Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

文本正在翻译中!我们很缺人手,所以翻译进度有点慢,赶紧加入进来一起翻译吧!

概要

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.
是否适用于 CSS 动画
正规顺序the unique non-ambiguous order defined by the formal grammar

语法

Formal syntax: normal | none | [ <string> | <uri> | <counter> | attr() | open-quote | close-quote | no-open-quote | no-close-quote ]+
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 &nbsp; */
content: url(https://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="https://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="https://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="https://www.mozilla.org/en-US/">Home Page</a>

CSS 

a::before{
    content: url(https://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(https://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

HTML Content

<ul>
    <li><a id="moz" href="https://www.mozilla.org/">
        Mozilla Home Page</a></li>
    <li><a id="mdn" href="https://developer.mozilla.org/">
        Mozilla Developer Network</a></li>
    <li><a id="w3c" href="https://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://mdn.mozillademos.org/files/7691/mdn-favicon16.png) ;
}

#w3c::before {
    content:url(https://w3c.org/2008/site/images/favicon.ico) ;
}

li {
    margin: 1em;
}

Output

Specifications

Specification Status Comment
CSS Level 2 (Revision 1)
content
Recommendation  

浏览器兼容性

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

 

参考

文档标签和贡献者

 最后编辑者: SphinxKnight,