aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/content/index.html
blob: f9579104f1fbe67175d583c23dc6be19f24d293f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
---
title: content
slug: Web/CSS/content
tags:
  - CSS 属性
  - Generated Content
  - 生成内容
translation_of: Web/CSS/content
---
<p>{{ CSSRef() }}</p>

<h2 id="概要">概要</h2>

<p>CSS的 <code>content</code> CSS 属性用于在元素的  {{ cssxref("::before") }}{{ cssxref("::after") }} 伪元素中插入内容。使用<code style="font-style: normal; line-height: 1.5;">content</code><span style="line-height: 1.5;"> 属性插入的内容都是匿名的</span><em><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/Replaced_element" style="text-decoration: underline;" title="CSS/Replaced_element">可替换元素</a></em></p>

<p>{{cssinfo}}</p>

<h2 id="语法">语法</h2>

<pre class="twopartsyntaxbox"><a href="/en-US/docs/CSS/Value_definition_syntax" title="CSS/Value_definition_syntax">Formal syntax</a>: {{csssyntax("content")}}
</pre>

<pre>content: normal                                /* Keywords that cannot be combined with other values */
content: none

content: 'prefix'                              /* &lt;string&gt; value, non-latin characters must be encoded e.g. \00A0 for &amp;nbsp; */
content: url(http://www.example.com/test.html) /* &lt;uri&gt; value */
content: chapter_counter                       /* &lt;counter&gt; 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
</pre>

<h3 id="值"></h3>

<dl>
 <dt><code>none</code></dt>
 <dd>不会产生伪类元素</dd>
 <dt><code>normal</code></dt>
 <dd> <code>:before</code> 和 <code>:after</code> 伪类元素中会被视为 <code>none</code></dd>
 <dt>&lt;string&gt;</dt>
 <dd>文本内容</dd>
 <dt>&lt;uri&gt;<code> url()</code></dt>
 <dd>URI值会指定一个外部资源(比如图片)。如果该资源或图片不能显示,它就会被忽略或显示一些占位(比如无图片标志)。</dd>
 <dt>&lt;counter&gt;</dt>
 <dd>计数器可以指定两种不同的函数:<code>counter()</code><code>counters()</code>。前面一个有两种形式:<code>counter(name) </code><code> counter(name,style)</code> 。产生的内容是该伪类元素指定名称的最小范围的计数;格式由style指定(默认是'decimal'——十进制数字)。后一个函数同样也有两种形式:<code>counters(name,string)</code><code>counters(name,string,style)</code> 。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.</dd>
 <dt><code>attr(X)</code></dt>
 <dd>将元素的X属性以字符串形式返回。如果该元素没有 X 属性,则返回一个空字符串。区分大小写的属性返回值依赖文挡的语言设定。</dd>
 <dt><code>open-quote</code> | <code>close-quote</code></dt>
 <dd>这些值会被 {{ cssxref("quotes") }} 中定义的字符串替换。</dd>
 <dt><code>no-open-quote</code> | <code>no-close-quote</code></dt>
 <dd>不会生产任何内容,但是会改变(增加或降低)引号层级。</dd>
</dl>

<h2 id="示例">示例</h2>

<h3 id="代码举例_-_标题和引号">代码举例 - 标题和引号</h3>

<h4 id="HTML">HTML </h4>

<pre class="brush: html">&lt;h1&gt;5&lt;/h1&gt;
&lt;p&gt; We shall start this with a quote from Sir Tim Berners-Lee,
    &lt;q cite="http://www.w3.org/People/Berners-Lee/FAQ.html#Internet"&gt;
        I was lucky enough to invent the Web at the time when the Internet already existed - and had for a decade and a half.&lt;/q&gt;  We must understand that there is nothing fundamentally wrong with building on the contributions of others.
&lt;/p&gt;

&lt;h1&gt;6&lt;/h1&gt;
&lt;p&gt; Here we shall quote the Mozilla Manifesto,
    &lt;q cite="http://www.mozilla.org/en-US/about/manifesto/"&gt;
        Individuals must have the ability to shape the Internet and their own experiences on the Internet.&lt;/q&gt; And so, we can infer that contributing to the open web, can protect our own individual experiences on it.
&lt;/p&gt;</pre>

<h4 id="CSS">CSS </h4>

<pre class="brush: css">q {
    color: #00008B;
    font-style: italic;
}

q::before   { content: open-quote }
q::after    { content: close-quote }

h1::before  { content: "Chapter "; }</pre>

<h4 id="输出">输出</h4>

<p>{{ EmbedLiveSample('代码举例_-_标题和引号', 460, 100) }}</p>

<h3 id="代码举例_-_link前面加一个icon">代码举例 - link前面加一个icon</h3>

<h4 id="HTML_2">HTML </h4>

<pre class="brush: html">&lt;a href="http://www.mozilla.org/en-US/"&gt;Home Page&lt;/a&gt;</pre>

<h4 id="CSS_2">CSS </h4>

<pre class="brush: css">a::before{
    content: url(http://www.mozilla.org/favicon.ico) " MOZILLA: ";
    font:    x-small Arial,freeSans,sans-serif;
    color:   gray;
}</pre>

<h4 id="输出_2">输出</h4>

<p>{{ EmbedLiveSample('代码举例_-_link前面加一个icon', 200, 60) }}</p>

<h3 id="代码举例_-_自定义列表后面加加文字">代码举例 - 自定义列表后面加加文字</h3>

<h4 id="HTML_3">HTML </h4>

<pre class="brush: html">&lt;div&gt;
  &lt;ul class="brightIdea"&gt;
    &lt;li&gt;This is my first idea&lt;/li&gt;
    &lt;li&gt;and another good idea&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</pre>

<h4 id="CSS_3">CSS </h4>

<pre class="brush: 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;
}</pre>

<h4 id="输出_3">输出</h4>

<p>{{EmbedLiveSample('代码举例_-_自定义列表后面加加文字', 300, 100)}}</p>

<h3 id="Code_sample_-_class_based_example" name="Code_sample_-_class_based_example">Code sample - class based example</h3>

<h4 id="HTML_Content">HTML Content</h4>

<pre class="brush: html">&lt;h2&gt;Paperback best sellers&lt;/h2&gt;
&lt;ol&gt;
    &lt;li&gt;Political thriller&lt;/li&gt;
    &lt;li class="newEntry"&gt;Halloween Stories&lt;/li&gt;
    &lt;li&gt;My Biography&lt;/li&gt;
    &lt;li class="newEntry"&gt;Vampire Romance&lt;/li&gt;
&lt;/ol&gt;</pre>

<h4 id="CSS_Content">CSS Content</h4>

<pre class="brush: css">/* 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;
}</pre>

<h4 id="Output">Output</h4>

<p>{{ EmbedLiveSample('Code_sample_-_class_based_example', 300, 200) }}</p>

<h3 id="Code_sample_-_rich_link_styling">Code sample - rich link styling</h3>

<h4 id="HTML_Content_2">HTML Content</h4>

<pre class="brush: html">&lt;ul&gt;
    &lt;li&gt;&lt;a id="moz" href="http://www.mozilla.org/"&gt;
        Mozilla Home Page&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a id="mdn" href="https://developer.mozilla.org/"&gt;
        Mozilla Developer Network&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a id="w3c" href="http://www.w3c.org/"&gt;
        World Wide Web Consortium&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</pre>

<h4 id="CSS_Content_2">CSS Content</h4>

<pre class="brush: 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) ;
}

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

li {
    margin: 1em;
}

</pre>

<h4 id="Output_2">Output</h4>

<p>{{ EmbedLiveSample('Code_sample_-_rich_link_styling', 340, 200) }}</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('CSS2.1', 'generate.html#content', 'content') }}</td>
   <td>{{ Spec2('CSS2.1') }}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_Compatibility" name="Browser_Compatibility">浏览器兼容性</h2>

{{Compat("css.properties.content")}}

<h2 id="sect1"> </h2>

<h2 id="参考">参考</h2>

<ul>
 <li>{{ Cssxref("::after") }}, {{ Cssxref("::before") }}, {{ Cssxref("quotes") }}</li>
</ul>