aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/learn/css/styling_text/fundamentals/index.html
blob: b615de8b660d9846e6d5fad1dd2f1607c9255c6c (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
---
title: 基本文本和字体样式
slug: Learn/CSS/Styling_text/Fundamentals
tags:
  - 初学者
  - 对齐
  - 文本
  - 样式
  - 间距
translation_of: Learn/CSS/Styling_text/Fundamentals
original_slug: Learn/CSS/为文本添加样式/Fundamentals
---
<div>{{LearnSidebar}}</div>

<div>{{NextMenu("Learn/CSS/Styling_text/Styling_lists", "Learn/CSS/Styling_text")}}</div>

<p class="summary"><span class="seoSummary">在这篇文章中,我们将带你开始掌握 {{glossary("CSS")}} 的文字样式的旅程。</span>这里我们将详细介绍文本/字体样式的所有基本原理,包括设置文字的粗细,字体和样式,文字的属性简写,文字的对齐,和其他效果,以及行和字母间距。</p>

<table class="learn-box standard-table">
 <tbody>
  <tr>
   <th scope="row">先决条件:</th>
   <td>基本的电脑操作,HTML 基础 (学习 <a href="/zh-CN/docs/Learn/HTML/Introduction_to_HTML">Introduction to HTML</a>),CSS 基础 (学习 <a href="/zh-CN/docs/Learn/CSS/Introduction_to_CSS">Introduction to CSS</a>).</td>
  </tr>
  <tr>
   <th scope="row">目的:</th>
   <td>了解在网页上设计文本所需的基本属性和技术。</td>
  </tr>
 </tbody>
</table>

<h2 id="CSS中的文字样式涉及什么?">CSS中的文字样式涉及什么?</h2>

<p>正如你已经在你使用 HTML 和 CSS 完成工作时所经历的一样,元素中的文本是布置在元素的内容框中。以内容区域的左上角作为起点 (或者是右上角,是在 RTL 语言的情况下),一直延续到行的结束部分。一旦达到行的尽头,它就会进到下一行,然后继续,再接着下一行,直到所有内容都放入了盒子中。文本内容表现地像一些内联元素,被布置到相邻的行上,除非到达了行的尽头,否则不会换行,或者你想强制地,手动地造成换行的话,你可以使用 {{htmlelement("br")}} 元素。</p>

<div class="note">
<p><strong>注意</strong>: 如果上面的段落让你感到困惑,没关系,在继续之前,可以重新看看我们的 <a href="/zh-CN/docs/Learn/CSS/Introduction_to_CSS/Box_model">Box model</a> 文件,复习框模型的理论。</p>
</div>

<p>用于样式文本的 CSS 属性通常可以分为两类,我们将在本文中分别观察。</p>

<ul>
 <li><strong>字体样式</strong>: 作用于字体的属性,会直接应用到文本中,比如使用哪种字体,字体的大小是怎样的,字体是粗体还是斜体,等等。</li>
 <li><strong>文本布局风格</strong>: 作用于文本的间距以及其他布局功能的属性,比如,允许操纵行与字之间的空间,以及在内容框中,文本如何对齐。</li>
</ul>

<div class="note">
<p><strong>注意</strong>: 请记住,包含在元素中的文本是作为一个单一的实体。你不能将文字其中一部分选中或添加样式,如果你要这么做,那么你必须要用适合的元素来包装它们,比如 ( {{htmlelement("span")}} 或者 {{htmlelement("strong")}}), 或者使用伪元素,像<a href="/zh-CN/docs/Web/CSS/::first-letter">::first-letter</a> (选中元素文本的第一个字母), <a href="/zh-CN/docs/Web/CSS/::first-line">::first-line</a> (选中元素文本的第一行), 或者 <a href="/zh-CN/docs/Web/CSS/::selection">::selection</a> (当前光标双击选中的文本)</p>
</div>

<h2 id="字体">字体</h2>

<p>我们直接来看看样式字体的属性。在这个例子中,我们会在一个相同的 HTML 示例中应用一些不同的 CSS 属性,就像这样:</p>

<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;</pre>

<p>你可以在这找到完成版本 <a href="http://mdn.github.io/learning-area/css/styling-text/fundamentals/">finished example on Github</a> (也可以看源码 <a href="https://github.com/mdn/learning-area/blob/master/css/styling-text/fundamentals/index.html">the source code</a>.)</p>

<h3 id="Color">颜色</h3>

<p>{{cssxref("color")}} 属性设置选中元素的前景内容的颜色 (通常指文本,不过也包含一些其他东西,或者是使用 {{cssxref("text-decoration")}} 属性放置在文本下方或上方的线 (underline overline)。</p>

<p><code>color</code> 也可以接受任何合法的 <a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Colors">CSS 颜色单位</a>, 比如:</p>

<pre class="brush: css">p {
  color: red;
}</pre>

<p>这将导致段落变为红色,而不是标准的浏览器默认的黑色,如下所示:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;</pre>
</div>

<p>{{ EmbedLiveSample('Color', '100%', 230) }}</p>

<h3 id="字体种类">字体种类</h3>

<p>要在你的文本上设置一个不同的字体,你可以使用 {{cssxref("font-family")}}  属性,这个允许你为浏览器指定一个字体 (或者一个字体的列表),然后浏览器可以将这种字体应用到选中的元素上。浏览器只会把在当前机器上可用的字体应用到当前正在访问的网站上;如果字体不可用,那么就会用浏览器默认的字体代替 {{anch("Default fonts", "default font")}}. 下面是一个简单的例子:</p>

<pre class="brush: css">p {
  font-family: arial;
}</pre>

<p>这段语句使所有在页面上的段落都采用 arial 字体,这个字体可在任何电脑上找到。</p>

<h4 id="网页安全字体">网页安全字体</h4>

<p>说到字体可用性,只有某几个字体通常可以应用到所有系统,因此可以毫无顾忌地使用。这些都是所谓的 <strong>网页安全字体</strong></p>

<p>大多数时候,作为网页开发者,我们希望对用于显示我们的文本内容的字体有更具体的控制。问题在于,需要一个方法来知道当前正在浏览我们的网站网页的电脑,它有哪些可用字体。我们并不是总能在每种情况下都知道这一点,但是网络安全字体在几乎所有最常用的操作系统(Windows,Mac,最常见的Linux发行版,Android和iOS版本)中都可用。</p>

<p>实际的Web安全字体列表将随着操作系统的发展而改变,但是可以认为下面的字体是网页安全的,至少对于现在来说 (它们中的许多都非常流行,这要感谢微软在90年代末和21世纪初期的倡议<em><a href="https://en.wikipedia.org/wiki/Core_fonts_for_the_Web">Core fonts for the Web</a></em> ):</p>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">字体名称</th>
   <th scope="col" style="white-space: nowrap;">泛型</th>
   <th scope="col">注意</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>Arial</td>
   <td>sans-serif</td>
   <td>通常认为最佳做法还是添加 Helvetica 作为 Arial 的首选替代品,尽管它们的字体面几乎相同,但 Helvetica 被认为具有更好的形状,即使Arial更广泛地可用。</td>
  </tr>
  <tr>
   <td>Courier New</td>
   <td>monospace</td>
   <td>某些操作系统有一个 Courier New 字体的替代(可能较旧的)版本叫Courier。使用Courier New作为Courier的首选替代方案,被认为是最佳做法。</td>
  </tr>
  <tr>
   <td style="white-space: nowrap;">Georgia</td>
   <td>serif</td>
   <td></td>
  </tr>
  <tr>
   <td style="white-space: nowrap;">Times New Roman</td>
   <td>serif</td>
   <td>某些操作系统有一个 Times New Roman 字体的替代(可能较旧的)版本叫 Times。使用Times作为Times New Roman的首选替代方案,被认为是最佳做法。</td>
  </tr>
  <tr>
   <td>Trebuchet MS</td>
   <td>sans-serif</td>
   <td>您应该小心使用这种字体——它在移动操作系统上并不广泛。</td>
  </tr>
  <tr>
   <td>Verdana</td>
   <td>sans-serif</td>
   <td></td>
  </tr>
 </tbody>
</table>

<div class="note">
<p><strong>注意</strong>: 在各种资源中,<a href="http://www.cssfontstack.com/">cssfontstack.com</a> 网站维护了一个可用在 Windows 和 Mac 操作系统上使用的网页安全字体的列表,这可以帮助决策网站的安全性。</p>
</div>

<div class="note">
<p><strong>注意</strong>: 有一个可以下载来自一个网页的自定义字体的方法,允许你通过任何你想要的方法来定制你使用的字体:<strong>网页字体</strong>。这个有一点复杂,我们将在这个模块中的另一篇文章中讨论这一点。</p>
</div>

<h4 id="默认字体">默认字体</h4>

<p>CSS 定义了 5 个常用的字体名称:  <code>serif<font face="Open Sans, Arial, sans-serif"></font></code><code>sans-serif,<font face="Open Sans, Arial, sans-serif"> </font></code><code>monospace</code>, <code>cursive,</code>和 <code>fantasy. </code>这些都是非常通用的,当使用这些通用名称时,使用的字体完全取决于每个浏览器,而且它们所运行的每个操作系统也会有所不同。这是一种糟糕的情况,浏览器会尽力提供一个看上去合适的字体。 <code>serif</code>, <code>sans-serif</code> 和 <code>monospace</code> 是比较好预测的,默认的情况应该比较合理,另一方面,<code>cursive</code> 和 <code>fantasy</code> 是不太好预测的,我们建议使用它们的时候应该稍微注意一些,多多测试。</p>

<p>五个名称定义如下:</p>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">名称</th>
   <th scope="col">定义</th>
   <th scope="col">示例</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>serif</code></td>
   <td>有衬线的字体 (衬线一词是指字体笔画尾端的小装饰,存在于某些印刷体字体中)</td>
   <td><span style="font-family: serif;">My big red elephant</span></td>
  </tr>
  <tr>
   <td><code>sans-serif</code></td>
   <td>没有衬线的字体。</td>
   <td><span style="font-family: sans-serif;">My big red elephant</span></td>
  </tr>
  <tr>
   <td><code>monospace</code></td>
   <td>每个字符具有相同宽度的字体,通常用于代码列表。</td>
   <td><span style="font-family: monospace;">My big red elephant</span></td>
  </tr>
  <tr>
   <td><code>cursive</code></td>
   <td>用于模拟笔迹的字体,具有流动的连接笔画。</td>
   <td><span style="font-family: cursive;">My big red elephant</span></td>
  </tr>
  <tr>
   <td><code>fantasy</code></td>
   <td>用来装饰的字体</td>
   <td><span style="font-family: fantasy;">My big red elephant</span></td>
  </tr>
 </tbody>
</table>

<h4 id="字体栈">字体栈</h4>

<p>由于你无法保证你想在你的网页上使用的字体的可用性 (甚至一个网络字体可能由于某些原因而出错), 你可以提供一个<strong>字体栈</strong> (<strong>font stack</strong>),这样的话,浏览器就有多种字体可以选择了。只需包含一个<code>font-family属性</code>,其值由几个用逗号分离的字体名称组成。比如</p>

<pre class="brush: css">p {
  font-family: "Trebuchet MS", Verdana, sans-serif;
}</pre>

<p>在这种情况下,浏览器从列表的第一个开始,然后查看在当前机器中,这个字体是否可用。如果可用,就把这个字体应用到选中的元素中。如果不可用,它就移到列表中的下一个字体,然后再检查。</p>

<p>在字体栈的最后提供一个合适的通用的字体名称是个不错的办法,这样的话,即使列出的字体都无法使用,浏览器至少可以提供一个还算合适的选择。为了强调这一点,如果没有其他选项可用,那么段落将被赋予浏览器的默认衬线字体 - 通常是Time New Roman - 这对于 sans-serif 字体是不利的!</p>

<div class="note">
<p><strong>注意</strong>: 有一些字体名称不止一个单词,比如<code>Trebuchet MS</code> ,那么就需要用引号包裹。</p>
</div>

<h4 id="A_font-family_example">一个使用 font-family 的例子</h4>

<p>让我们把它添加到之前的例子上,给段落一个 sans-serif 的字体。</p>

<pre class="brush: css">p {
  color: red;
  font-family: Helvetica, Arial, sans-serif;
}</pre>

<p>这给我们以下结果:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;</pre>
</div>

<p>{{ EmbedLiveSample('A_font-family_example', '100%', 220) }}</p>

<h3 id="字体大小">字体大小</h3>

<p>在我们之前的模块中的<a href="/zh-CN/docs/Learn/CSS/Introduction_to_CSS/Values_and_units">CSS values and units</a> 文章,我们回顾了<a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Length_and_size">length and size units</a>. 字体大小 (通过 {{cssxref("font-size")}} 属性设置) 可以取大多数这些单位的值 (以及其他,比如百分比 <a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Percentages">percentages</a>),然而你在调整字体大小时,最常用的单位是:</p>

<ul>
 <li><code>px</code> (像素): 将像素的值赋予给你的文本。这是一个绝对单位, 它导致了在任何情况下,页面上的文本所计算出来的像素值都是一样的。</li>
 <li><code>em</code>: 1em 等于我们设计的当前元素的父元素上设置的字体大小 (更加具体的话,比如包含在父元素中的大写字母 M 的宽度) 如果你有大量设置了不同字体大小的嵌套元素,这可能会变得棘手, 但它是可行的,如下图所示。为什么要使用这个麻烦的单位呢? 当你习惯这样做时,那么就会变得很自然,你可以使用<code>em</code>调整任何东西的大小,不只是文本。你可以有一个单位全部都使用 em 的网站,这样维护起来会很简单。</li>
 <li><code>rem</code>: 这个单位的效果和 <code>em</code> 差不多,除了 1<code>rem</code> 等于 HTML 中的根元素的字体大小, (i.e. {{htmlelement("html")}}) ,而不是父元素。这可以让你更容易计算字体大小,但是遗憾的是, <code>rem</code> 不支持 Internet Explorer 8 和以下的版本。如果你的项目需要支持较老的浏览器,你可以坚持使用<code>em</code> 或 <code>px</code>, 或者是 {{glossary("polyfill")}} 就像 <a href="https://github.com/chuckcarpenter/REM-unit-polyfill">REM-unit-polyfill</a>. (这个单位在“CSS的值和单位”一节也有讲解)</li>
</ul>

<p>元素的 <code>font-size</code> 属性是从该元素的父元素继承的。所以这一切都是从整个文档的根元素——{{htmlelement("html")}}开始,浏览器的 <code>font-size</code> 标准设置的值为 16px。在根元素中的任何段落 (或者那些浏览器没有设置默认大小的元素),会有一个最终的大小值:16px。其他元素也许有默认的大小,比如 {{htmlelement("h1")}} 元素有一个 2em 的默认值,所以它的最终大小值为 32px。当你开始更改嵌套元素的字体大小时,事情会变得棘手。比如,如果你有一个 {{htmlelement("article")}} 元素在你的页面上,然后设置它的 font-size 为 <code>1.5em</code> (通过计算,可以得到大小为 24px),然后想让 <code>&lt;article&gt;</code> 元素中的段落获得一个计算值为 20px 的大小,那么你应该使用多少 em。</p>

<pre class="brush: html">&lt;!-- document base font-size is 16px --&gt;
&lt;article&gt; &lt;!-- If my font-size is 1.5em --&gt;
  &lt;p&gt;My paragraph&lt;/p&gt; &lt;!-- How do I compute to 20px font-size? --&gt;
&lt;/article&gt;</pre>

<p>你需要将 em 的值设置为 20/24, 或者 <code>0.83333333em</code>. 这个计算可能比较复杂,所以当你设置的时候,你需要仔细一些。如果可以使用 rem 的话,那实现起来就变得简单不少,避免在可能的情况下设置容器元素的字体大小。</p>

<h4 id="A_simple_sizing_example">一个简单的 size 示例</h4>

<p>当调整你的文本大小时,将文档(document)的基础  <code>font-size</code> 设置为10px往往是个不错的主意,这样之后的计算会变得简单,所需要的 (r)em 值就是想得到的像素的值除以 10,而不是 16。做完这个之后,你可以简单地调整在你的 HTML 中你想调整的不同类型文本的字体大小。在样式表的指定区域列出所有<code>font-size</code>的规则集是一个好主意,这样它们就可以很容易被找到。</p>

<p>我们的新结果是这样的:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;
</pre>
</div>

<pre class="brush: css">html {
  font-size: 10px;
}

h1 {
  font-size: 2.6rem;
}

p {
  font-size: 1.4rem;
  color: red;
  font-family: Helvetica, Arial, sans-serif;
}</pre>

<p>{{ EmbedLiveSample('A_simple_sizing_example', '100%', 260) }}</p>

<h3 id="Font_style_font_weight_text_transform_and_text_decoration">字体样式,字体粗细,文本转换和文本装饰</h3>

<p>CSS 提供了 4 种常用的属性来改变文本的样子:</p>

<ul>
 <li>{{cssxref("font-style")}}: 用来打开和关闭文本 italic (斜体)。 可能的值如下 (你很少会用到这个属性,除非你因为一些理由想将斜体文字关闭斜体状态):
  <ul>
   <li><code>normal</code>: 将文本设置为普通字体 (将存在的斜体关闭)</li>
   <li><code>italic</code>: 如果当前字体的斜体版本可用,那么文本设置为斜体版本;如果不可用,那么会利用 oblique 状态来模拟 italics。</li>
   <li><code>oblique</code>: 将文本设置为斜体字体的模拟版本,也就是将普通文本倾斜的样式应用到文本中。</li>
  </ul>
 </li>
 <li>{{cssxref("font-weight")}}: 设置文字的粗体大小。这里有很多值可选 (比如 <em>-light</em>, <em>-normal</em>, <em>-bold</em>, <em>-extrabold</em>, <em>-black</em>, 等等), 不过事实上你很少会用到 <code>normal</code> 和 <code>bold</code>以外的值:
  <ul>
   <li><code>normal</code>, <code>bold</code>: 普通或者<strong style="font-weight: bold;">加粗</strong>的字体粗细</li>
   <li><code>lighter</code>, <code>bolder</code>: 将当前元素的粗体设置为比其父元素粗体更细或更粗一步。<code>100</code><code>900</code>: 数值粗体值,如果需要,可提供比上述关键字更精细的粒度控制。</li>
  </ul>
 </li>
 <li>{{cssxref("text-transform")}}: 允许你设置要转换的字体。值包括:
  <ul>
   <li><code>none</code>: 防止任何转型。</li>
   <li><code>uppercase</code>: 将所有文本转为大写。</li>
   <li><code>lowercase</code>: 将所有文本转为小写。</li>
   <li><code>capitalize</code>: 转换所有单词让其首字母大写。</li>
   <li><code>full-width</code>: 将所有字形转换成全角,即固定宽度的正方形,类似于等宽字体,允许拉丁字符和亚洲语言字形(如中文,日文,韩文)对齐。</li>
  </ul>
 </li>
 <li>{{cssxref("text-decoration")}}: 设置/取消字体上的文本装饰 (你将主要使用此方法在设置链接时取消设置链接上的默认下划线。) 可用值为:
  <ul>
   <li><code>none</code>: 取消已经存在的任何文本装饰。</li>
   <li><code>underline</code>: <u>文本下划线</u>.</li>
   <li><code>overline</code>: <span style="text-decoration: overline;">文本上划线</span></li>
   <li><code>line-through</code>: 穿过文本的线 <s style="text-decoration: line-through;">strikethrough over the text</s>.</li>
  </ul>
  你应该注意到 {{cssxref("text-decoration")}} 可以一次接受多个值,如果你想要同时添加多个装饰值, 比如 <span style="text-decoration: underline overline;"><code>text-decoration: underline overline</code></span>.。同时注意 {{cssxref("text-decoration")}} 是一个缩写形式,它由 {{cssxref("text-decoration-line")}}, {{cssxref("text-decoration-style")}}{{cssxref("text-decoration-color")}} 构成。你可以使用这些属性值的组合来创建有趣的效果,比如 <span style="text-decoration: line-through red wavy;"><code>text-decoration: line-through red wavy</code>.</span></li>
</ul>

<p>我们来看一下这几个属性添加到我们的例子中:</p>

<p>我们的新结果是这样的:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;
</pre>
</div>

<pre class="brush: css">html {
  font-size: 10px;
}

h1 {
  font-size: 2.6rem;
  text-transform: capitalize;
}

h1 + p {
  font-weight: bold;
}

p {
  font-size: 1.4rem;
  color: red;
  font-family: Helvetica, Arial, sans-serif;
}</pre>

<p>{{ EmbedLiveSample('Font_style_font_weight_text_transform_and_text_decoration', '100%', 260) }}</p>

<h3 id="文字阴影">文字阴影</h3>

<p>你可以为你的文本应用阴影,使用 {{cssxref("text-shadow")}} 属性。这最多需要 4 个值,如下例所示:</p>

<pre class="brush: css">text-shadow: 4px 4px 5px red;</pre>

<p>4 个属性如下:</p>

<ol>
 <li>阴影与原始文本的水平偏移,可以使用大多数的 CSS 单位 <a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Length_and_size">length and size units</a>, 但是 px 是比较合适的。这个值必须指定。</li>
 <li>阴影与原始文本的垂直偏移;效果基本上就像水平偏移,除了它向上/向下移动阴影,而不是左/右。这个值必须指定。</li>
 <li>模糊半径 - 更高的值意味着阴影分散得更广泛。如果不包含此值,则默认为0,这意味着没有模糊。可以使用大多数的 CSS 单位 <a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Length_and_size">length and size units</a>.</li>
 <li>阴影的基础颜色,可以使用大多数的 CSS 颜色单位 <a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Colors">CSS color unit</a>. 如果没有指定,默认为 <code>black</code>.</li>
</ol>

<div class="note">
<p><strong>注意</strong>: 正偏移值可以向右移动阴影,但也可以使用负偏移值来左右移动阴影,例如 <code>-1px -1px</code>.</p>
</div>

<h4 id="Hidden_example1">多种阴影</h4>

<p>您可以通过包含以逗号分隔的多个阴影值,将多个阴影应用于同一文本,例如:</p>

<pre class="brush: css"><code class="language-css"><span class="property token">text-shadow</span><span class="punctuation token">:</span> -<span class="number token">1</span>px -<span class="number token">1</span>px <span class="number token">1</span>px <span class="hexcode token">#aaa</span>,
             <span class="number token">0</span>px <span class="number token">4</span>px <span class="number token">1</span>px <span class="function token">rgba</span><span class="punctuation token">(</span><span class="number token">0</span>,<span class="number token">0</span>,<span class="number token">0</span>,<span class="number token">0.5</span><span class="punctuation token">)</span>,
             <span class="number token">4</span>px <span class="number token">4</span>px <span class="number token">5</span>px <span class="function token">rgba</span><span class="punctuation token">(</span><span class="number token">0</span>,<span class="number token">0</span>,<span class="number token">0</span>,<span class="number token">0.7</span><span class="punctuation token">)</span>,
             <span class="number token">0</span>px <span class="number token">0</span>px <span class="number token">7</span>px <span class="function token">rgba</span><span class="punctuation token">(</span><span class="number token">0</span>,<span class="number token">0</span>,<span class="number token">0</span>,<span class="number token">0.4</span><span class="punctuation token">)</span><span class="punctuation token">;</span></code></pre>

<p>如果我们把这个样式应用到我们 "Tommy the cat" 示例中的 {{htmlelement("h1")}} 元素,就像这样:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;
</pre>

<pre class="brush: css">html {
  font-size: 10px;
}

h1 {
  font-size: 26px;
  text-transform: capitalize;
  text-shadow: -1px -1px 1px #aaa,
               0px 2px 1px rgba(0,0,0,0.5),
               2px 2px 2px rgba(0,0,0,0.7),
               0px 0px 3px rgba(0,0,0,0.4);
}

h1 + p {
  font-weight: bold;
}

p {
  font-size: 14px;
  color: red;
  font-family: Helvetica, Arial, sans-serif;
}</pre>
</div>

<p>{{ EmbedLiveSample('Hidden_example1', '100%', 260) }}</p>

<div class="note">
<p><strong>注意</strong>: 你可以看到更多有趣的关于 <code>text-shadow</code> 使用的示例在 <a href="http://www.sitepoint.com/moonlighting-css-text-shadow/">Moonlighting with CSS text-shadow</a>.</p>
</div>

<h2 id="文本布局">文本布局</h2>

<p>有了基本的字体属性,我们来看看我们可以用来影响文本布局的属性。</p>

<h3 id="Text_alignment">文本对齐</h3>

<p> {{cssxref("text-align")}} 属性用来控制文本如何和它所在的内容盒子对齐。可用值如下,并且在与常规文字处理器应用程序中的工作方式几乎相同:</p>

<ul>
 <li><code>left</code>: 左对齐文本。</li>
 <li><code>right</code>: 右对齐文本。</li>
 <li><code>center</code>: 居中文字</li>
 <li><code>justify</code>: 使文本展开,改变单词之间的差距,使所有文本行的宽度相同。你需要仔细使用,它可以看起来很可怕。特别是当应用于其中有很多长单词的段落时。如果你要使用这个,你也应该考虑一起使用别的东西,比如 {{cssxref("hyphens")}},打破一些更长的词语。</li>
</ul>

<p>如果我们应用 <code>text-align: center;</code> 到我们例子中的 {{htmlelement("h1")}} 元素中,结果如下:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;
</pre>

<pre class="brush: css">html {
  font-size: 10px;
}

h1 {
  font-size: 2.6rem;
  text-transform: capitalize;
  text-shadow: -1px -1px 1px #aaa,
               0px 2px 1px rgba(0,0,0,0.5),
               2px 2px 2px rgba(0,0,0,0.7),
               0px 0px 3px rgba(0,0,0,0.4);
  text-align: center;
}

h1 + p {
  font-weight: bold;
}

p {
  font-size: 1.4rem;
  color: red;
  font-family: Helvetica, Arial, sans-serif;
}</pre>
</div>

<p>{{ EmbedLiveSample('Text_alignment', '100%', 260) }}</p>

<h3 id="Hidden_example2">行高</h3>

<p> {{cssxref("line-height")}} 属性设置文本每行之间的高,可以接受大多数单位 <a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Length_and_size">length and size units</a>,不过也可以设置一个无单位的值,作为乘数,通常这种是比较好的做法。无单位的值乘以 {{cssxref("font-size")}} 来获得 <code>line-height</code>。当行与行之间拉开空间,正文文本通常看起来更好更容易阅读。推荐的行高大约是 1.5–2 (双倍间距。) 所以要把我们的文本行高设置为字体高度的1.5倍,你可以使用这个:</p>

<pre class="brush: css">line-height: 1.5;</pre>

<p>把这个样式应用到我们示例中的 {{htmlelement("p")}} 元素,结果如下:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;
</pre>

<pre class="brush: css">html {
  font-size: 10px;
}

h1 {
  font-size: 2.6rem;
  text-transform: capitalize;
  text-shadow: -1px -1px 1px #aaa,
               0px 2px 1px rgba(0,0,0,0.5),
               2px 2px 2px rgba(0,0,0,0.7),
               0px 0px 3px rgba(0,0,0,0.4);
  text-align: center;
}

h1 + p {
  font-weight: bold;
}

p {
  font-size: 1.4rem;
  color: red;
  font-family: Helvetica, Arial, sans-serif;
  line-height: 1.5;
}</pre>
</div>

<p>{{ EmbedLiveSample('Hidden_example2', '100%', 300) }}</p>

<h3 id="Letter_and_word_spacing">字母和单词间距</h3>

<p>{{cssxref("letter-spacing")}}{{cssxref("word-spacing")}} 属性允许你设置你的文本中的字母与字母之间的间距、或是单词与单词之间的间距。你不会经常使用它们,但是可能可以通过它们,来获得一个特定的外观,或者让较为密集的文字更加可读。它们可以接受大多数单位 <a href="/zh-CN/Learn/CSS/Introduction_to_CSS/Values_and_units#Length_and_size">length and size units</a>.</p>

<p>所以作为例子,如果我们把这个样式应用到我们的示例中的 {{htmlelement("p")}} 段落的第一行:</p>

<pre class="brush: css">p::first-line {
  letter-spacing: 2px;
  word-spacing: 4px;
}</pre>

<p>我们会得到下面的结果:</p>

<div class="hidden">
<pre class="brush: html">&lt;h1&gt;Tommy the cat&lt;/h1&gt;

&lt;p&gt;I remember as if it were a meal ago...&lt;/p&gt;

&lt;p&gt;Said Tommy the Cat as he reeled back to clear whatever foreign matter
 may have nestled its way into his mighty throat. Many a fat alley rat
had met its demise while staring point blank down the cavernous barrel of
 this awesome prowling machine. Truly a wonder of nature this urban
predator — Tommy the cat had many a story to tell. But it was a rare
occasion such as this that he did.&lt;/p&gt;
</pre>

<pre class="brush: css">html {
  font-size: 10px;
}

h1 {
  font-size: 2.6rem;
  text-transform: capitalize;
  text-shadow: -1px -1px 1px #aaa,
               0px 2px 1px rgba(0,0,0,0.5),
               2px 2px 2px rgba(0,0,0,0.7),
               0px 0px 3px rgba(0,0,0,0.4);
  text-align: center;
}

h1 + p {
  font-weight: bold;
}

p::first-line {
  letter-spacing: 2px;
  word-spacing: 4px;
}

p {
  font-size: 1.4rem;
  color: red;
  font-family: Helvetica, Arial, sans-serif;
  line-height: 1.5;
}</pre>
</div>

<p>{{ EmbedLiveSample('Letter_and_word_spacing', '100%', 330) }}</p>

<h3 id="其他一些值得看一下的属性">其他一些值得看一下的属性</h3>

<p>以上属性让你了解如何开始在网页上设置文本, 但是你可以使用更多的属性。我们只是想介绍最重要的。一旦你习惯使用上面的内容,你还应该探索以下几点:</p>

<p>Font 样式:</p>

<ul>
 <li>{{cssxref("font-variant")}}: 在小型大写字母和普通文本选项之间切换。</li>
 <li>{{cssxref("font-kerning")}}: 开启或关闭字体间距选项。</li>
 <li>{{cssxref("font-feature-settings")}}: 开启或关闭不同的 <a href="https://en.wikipedia.org/wiki/OpenType">OpenType</a> 字体特性。</li>
 <li>{{cssxref("font-variant-alternates")}}: 控制给定的自定义字体的替代字形的使用。</li>
 <li>{{cssxref("font-variant-caps")}}: 控制大写字母替代字形的使用。</li>
 <li>{{cssxref("font-variant-east-asian")}}: 控制东亚文字替代字形的使用, 像日语和汉语。</li>
 <li>{{cssxref("font-variant-ligatures")}}: 控制文本中使用的连写和上下文形式。</li>
 <li>{{cssxref("font-variant-numeric")}}: 控制数字,分式和序标的替代字形的使用。</li>
 <li>{{cssxref("font-variant-position")}}: 控制位于上标或下标处,字号更小的替代字形的使用。</li>
 <li>{{cssxref("font-size-adjust")}}: 独立于字体的实际大小尺寸,调整其可视大小尺寸。</li>
 <li>{{cssxref("font-stretch")}}: 在给定字体的可选拉伸版本中切换。</li>
 <li>{{cssxref("text-underline-position")}}: 指定下划线的排版位置,通过使用 <code>text-decoration-line</code> 属性的<code>underline</code> 值。</li>
 <li>{{cssxref("text-rendering")}}: 尝试执行一些文本渲染优化。</li>
</ul>

<p>文本布局样式:</p>

<ul>
 <li>{{cssxref("text-indent")}}: 指定文本内容的第一行前面应该留出多少的水平空间。</li>
 <li>{{cssxref("text-overflow")}}: 定义如何向用户表示存在被隐藏的溢出内容。</li>
 <li>{{cssxref("white-space")}}: 定义如何处理元素内部的空白和换行。</li>
 <li>{{cssxref("word-break")}}: 指定是否能在单词内部换行。</li>
 <li>{{cssxref("direction")}}: 定义文本的方向 (这取决于语言,并且通常最好让HTML来处理这部分,因为它是和文本内容相关联的。)</li>
 <li>{{cssxref("hyphens")}}: 为支持的语言开启或关闭连字符。</li>
 <li>{{cssxref("line-break")}}: 对东亚语言采用更强或更弱的换行规则。</li>
 <li>{{cssxref("text-align-last")}}: 定义一个块或行的最后一行,恰好位于一个强制换行前时,如何对齐。</li>
 <li>{{cssxref("text-orientation")}}: 定义行内文本的方向。</li>
 <li>{{cssxref("word-wrap")}}: 指定浏览器是否可以在单词内换行以避免超出范围。</li>
 <li>{{cssxref("writing-mode")}}: 定义文本行布局为水平还是垂直,以及后继文本流的方向。</li>
</ul>

<h2 id="Font_简写">Font 简写</h2>

<p>许多字体的属性也可以通过 {{cssxref("font")}} 的简写方式来设置 . 这些是按照以下顺序来写的:  {{cssxref("font-style")}}, {{cssxref("font-variant")}}, {{cssxref("font-weight")}}, {{cssxref("font-stretch")}}, {{cssxref("font-size")}}, {{cssxref("line-height")}}, and {{cssxref("font-family")}}.</p>

<p>如果你想要使用 <code>font</code> 的简写形式,在所有这些属性中,只有 <code>font-size</code> 和 <code>font-family</code> 是一定要指定的。</p>

<p>{{cssxref("font-size")}}{{cssxref("line-height")}} 属性之间必须放一个正斜杠。</p>

<p>一个完整的例子如下所示:</p>

<pre class="brush: css">font: italic normal bold normal 3em/1.5 Helvetica, Arial, sans-serif;</pre>

<h2 id="动手练习_使用样式文本">动手练习: 使用样式文本</h2>

<p>在这个动手练习中,我们没有任何具体的练习来做:我们只是希望你和一些字体/文本布局属性相处地愉快,看看你可以制作什么!你可以使用离线HTML / CSS文件进行此操作,也可以将代码输入到下面的实时可编辑示例中。</p>

<p>如果你犯了错误,你可以使用 Reset 按钮来复原。</p>

<div class="hidden">
<h6 id="Playable_code">Playable code</h6>

<pre class="brush: html">&lt;div class="body-wrapper" style="font-family: 'Open Sans Light',Helvetica,Arial,sans-serif;"&gt;
  &lt;h2&gt;HTML Input&lt;/h2&gt;
  &lt;textarea id="code" class="html-input" style="width: 90%;height: 10em;padding: 10px;border: 1px solid #0095dd;"&gt;
  &lt;p&gt;Some sample text for your delight&lt;/p&gt;&lt;/textarea&gt;

  &lt;h2&gt;CSS Input&lt;/h2&gt;
  &lt;textarea id="code" class="css-input" style="width: 90%;height: 10em;padding: 10px;border: 1px solid #0095dd;"&gt;p {

  }&lt;/textarea&gt;

  &lt;h2&gt;Output&lt;/h2&gt;
  &lt;div class="output" style="width: 90%;height: 10em;padding: 10px;border: 1px solid #0095dd;"&gt;&lt;/div&gt;
  &lt;div class="controls"&gt;
    &lt;input id="reset" type="button" value="Reset" style="margin: 10px 10px 0 0;"&gt;
  &lt;/div&gt;
&lt;/div&gt;
</pre>

<pre class="brush: js">var htmlInput = document.querySelector(".html-input");
var cssInput = document.querySelector(".css-input");
var reset = document.getElementById("reset");
var htmlCode = htmlInput.value;
var cssCode = cssInput.value;
var output = document.querySelector(".output");

var styleElem = document.createElement('style');
var headElem = document.querySelector('head');
headElem.appendChild(styleElem);

function drawOutput() {
  output.innerHTML = htmlInput.value;
  styleElem.textContent = cssInput.value;
}

reset.addEventListener("click", function() {
  htmlInput.value = htmlCode;
  cssInput.value = cssCode;
  drawOutput();
});

htmlInput.addEventListener("input", drawOutput);
cssInput.addEventListener("input", drawOutput);
window.addEventListener("load", drawOutput);
</pre>
</div>

<p>{{ EmbedLiveSample('Playable_code', 700, 800) }}</p>

<h2 id="小结">小结</h2>

<p>我们希望你在本篇文章中享受与文本在一起的时光!下篇文章会介绍所有你需要知道的关于 HTML 列表的样式。</p>

<p>{{NextMenu("Learn/CSS/Styling_text/Styling_lists", "Learn/CSS/Styling_text")}}</p>