CSS 文本和字体样式设置
# CSS 文本和字体样式设置
# 1. 文本颜色
颜色属性被用来设置文字的颜色。常用的方式有三种:
- 十六进制值 - 如: #FF0000
- RGB值 - 如: rgb(255,0,0)
- 颜色名称 - 如: red
参阅 CSS 颜色值 (opens new window) 查看完整的颜色值列表。
在以下示例中,页面背景颜色由 body 选择器定义,标题和段落的文本颜色分别使用了不同的方式进行设置:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
body {color: red;}
h1 {color: #00ff00;}
p.ex {color: rgb(0, 0, 255);}
</style>
</head>
<body>
<h1>这是标题 1</h1>
<p>这是一个普通的段落。请注意,本文是红色的。页面中定义默认的文本颜色选择器。</p>
<p class="ex">这是一个类为"ex"的段落。这个文本是蓝色的。</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 2. 文本对齐方式
text-align 属性用于设置文本的水平对齐方式。可选的值包括:
left
:左对齐(默认值)right
:右对齐center
:居中对齐justify
:两端对齐(如杂志和报纸)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
h1 {text-align: center;}
p.date {text-align: right;}
p.main {text-align: justify;}
</style>
</head>
<body>
<h1>CSS text-align 实例</h1>
<p class="date">2015 年 3 月 14 号</p>
<p class="main">“当我年轻的时候,我梦想改变这个世界;当我成熟以后,我发现我不能够改变这个世界,我将目光缩短了些,决定只改变我的国家;当我进入暮年以后,我发现我不能够改变我们的国家,我的最后愿望仅仅是改变一下我的家庭,但是,这也不可能。当我现在躺在床上,行将就木时,我突然意识到:如果一开始我仅仅去改变我自己,然后,我可能改变我的家庭;在家人的帮助和鼓励下,我可能为国家做一些事情;然后,谁知道呢?我甚至可能改变这个世界。”</p>
<p><b>注意:</b> 重置浏览器窗口大小查看 "justify" 是如何工作的。</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 3. 文本修饰
text-decoration 属性用于设置或删除文本的装饰效果,常见的值包括:
none
:无装饰(常用于删除链接的下划线)underline
:下划线overline
:上划线line-through
:删除线
以下示例展示了如何删除链接的下划线:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
a {text-decoration: none;}
</style>
</head>
<body>
<p>链接到: <a href="http://www.runoob.com/">runoob.com</a></p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
以下示例展示了其他文本装饰效果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
h1 {text-decoration: overline;}
h2 {text-decoration: line-through;}
h3 {text-decoration: underline;}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 4. 文本转换
text-transform 属性用于控制文本的大小写转换,常见的值包括:
uppercase
:将文本转换为大写lowercase
:将文本转换为小写capitalize
:将每个单词的首字母大写
以下示例展示了三种不同的文本转换效果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
p.uppercase {text-transform: uppercase;}
p.lowercase {text-transform: lowercase;}
p.capitalize {text-transform: capitalize;}
</style>
</head>
<body>
<p class="uppercase">This is some text.</p>
<p class="lowercase">This is some text.</p>
<p class="capitalize">This is some text.</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 5. 文本缩进
text-indent 属性用于设置文本第一行的缩进,常见的值包括具体的长度值(如 50px
)和百分比(如 5%
)。
以下示例展示了文本缩进效果:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
p {text-indent: 50px;}
</style>
</head>
<body>
<p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since. 'Whenever you feel like criticizing anyone,' he told me, 'just remember that all the people in this world haven't had the advantages that you've had.'</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
# 6. CSS 文本属性汇总
以下是常见的 CSS 文本属性:
color
:设置文本颜色text-align
:设置文本的水平对齐方式text-decoration
:设置文本的装饰效果text-transform
:设置文本的大小写转换text-indent
:设置文本的缩进line-height
:设置文本的行高letter-spacing
:设置文本的字符间距word-spacing
:设置文本的单词间距direction
:设置文本的书写方向(如ltr
、rtl
)
# 7. CSS 字体样式设置
# 1. CSS 字体类型
在 CSS 中,有两种类型的字体系列名称:
- 通用字体系列 - 一组拥有相似外观的字体(如 "Serif" 或 "Monospace")
- 特定字体系列 - 特定的字体(如 "Times New Roman" 或 "Courier")
# 2. 字体系列
font-family 属性设置文本的字体系列。为了确保字体的兼容性,应指定几个字体名称作为后备方案。如果浏览器不支持第一种字体,它将尝试下一种字体。
注意: 如果字体系列的名称超过一个字,它必须用引号包围,如:"宋体"。
多个字体系列用逗号分隔:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
p.serif {font-family: "Times New Roman", Times, serif;}
p.sansserif {font-family: Arial, Helvetica, sans-serif;}
</style>
</head>
<body>
<h1>CSS font-family</h1>
<p class="serif">这一段的字体是 Times New Roman</p>
<p class="sansserif">这一段的字体是 Arial</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 3. 字体样式
font-style 属性用于指定斜体文字,常见值包括:
normal
:正常显示文本italic
:以斜体字显示oblique
:文字向一边倾斜(与斜体类似,但不太常用)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
p.normal {font-style: normal;}
p.italic {font-style: italic;}
p.oblique {font-style: oblique;}
</style>
</head>
<body>
<p class="normal">这是一个段落,正常显示。</p>
<p class="italic">这是一个段落,斜体显示。</p>
<p class="oblique">这是一个段落,倾斜显示。</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 4. 字体大小
font-size 属性用于设置文本的大小。可以使用绝对大小或相对大小来定义字体大小。
绝对大小:
- 设置一个具体大小的文本
- 不允许用户在所有浏览器中改变文本大小
- 当输出的物理尺寸确定时,绝对大小很有用
相对大小:
- 相对于周围的元素来设置大小
- 允许用户在浏览器中改变文字大小
注意: 如果不指定字体大小,默认大小是16像素(16px = 1em)。
# 4-1. 设置字体大小为像素
设置文字大小为像素,让您完全控制文字大小:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
h1 {font-size: 40px;}
h2 {font-size: 30px;}
p {font-size: 14px;}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>允许在 Internet Explorer 9, Firefox, Chrome, Opera, 和 Safari 中通过缩放浏览器调整文本大小。</p>
<p><b>注意:</b>这个例子在 IE9之前的版本不工作。</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
虽然可以通过浏览器的缩放工具调整文本大小,但这种调整是整个页面的缩放,而不仅仅是文本。
# 4-2. 用 em 设置字体大小
为了避免 Internet Explorer 中无法调整文本的问题,许多开发者使用 em 单位代替像素。em 的尺寸单位由 W3C 推荐,1em 等于当前字体大小的默认值。在浏览器中默认的文字大小是16px,因此,1em 的默认大小是16px。可以通过以下公式将像素转换为 em:px/16=em
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
h1 {font-size: 2.5em;} /* 40px/16=2.5em */
h2 {font-size: 1.875em;} /* 30px/16=1.875em */
p {font-size: 0.875em;} /* 14px/16=0.875em */
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>使用 em 单位,允许在所有浏览器中调整文本大小。</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
在上面的例子中,em 的文字大小与前面的像素示例相同。不过,如果使用 em 单位,则可以在所有浏览器中调整文本大小。
# 4-3. 使用百分比和 em 组合
在所有浏览器中,设置 <body>
元素的默认字体大小为百分比是一种解决方案:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
body {font-size: 100%;}
h1 {font-size: 2.5em;}
h2 {font-size: 1.875em;}
p {font-size: 0.875em;}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>在所有浏览器中,可以显示相同的文本大小,并允许所有浏览器缩放文本的大小。</p>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 5. 所有 CSS 字体属性
以下是常见的 CSS 字体属性:
font-family
:设置字体系列font-style
:设置字体样式(如 normal, italic, oblique)font-weight
:设置字体粗细(如 normal, bold, bolder, lighter, 100-900)font-size
:设置字体大小font-variant
:设置小型大写字母(如 normal, small-caps)line-height
:设置行高
通过灵活运用这些字体属性,您可以实现各种复杂的文本样式和效果,提高网页的视觉吸引力和可读性。