设置文本样式
1、应用基本文本样式
1.1、对齐文本
有好几个属性可以用来设计文本内容的对齐方式。
属性 | 说明 | 值 |
---|---|---|
text-align | 指定文本块的对齐方式 | start end left right center justify |
text-justify | 如果 text-justify 属性使用了 justify 值,则该值会指定对齐问文本的规则 |
text-align 属性相当简单,不过,需要注意的一点是:可以将文本对齐到显式命名的某个边界(使用 left 或者 right 值),或者对齐到语言本来使用的边界(使用 start 和 end 值)。在处理从右到左的语言时,这是一个非常重要的区别。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
#fruittext {
width: 400px;
margin: 5px;
padding: 5px;
border: medium double black;
background-color: lightgrey;
}
</style>
</head>
<body>
<p id="fruittext">
There are lots of different kinds of fruit - there are over 500
varieties of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
One of the most interesting aspects of fruit is the
variety available in each country. I live near London, in an area which is
known for its apples.
</p>
<p>
<button>Start</button>
<button>End</button>
<button>Left</button>
<button>Right</button>
<button>Justify</button>
<button>Center</button>
</p>
<script>
var buttons = document.getElementsByTagName("BUTTON");
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick = function(e) {
document.getElementById("fruittext").style.textAlign =
e.target.innerHTML;
};
}
</script>
</body>
</html>
如果使用 justify 值,可以使用 text-justify 属性指定文本添加空白的方式。
值 | 说明 |
---|---|
auto | 浏览器选择对齐方式,这是最简单的方法,不过,不同浏览器之间的呈现方式会有微小差别 |
none | 禁用文本对齐 |
inter-word | 空白分布在单词之间,适用于英语等词间有空的语言 |
inter-ideograph | 空白分布在单词、表意字之间,且文本两端对齐,适用于汉语、日文哈韩文等语言 |
inter-cluter | 空白分布在单词、字形集的边界,适用于泰文等无词间空格的语言 |
distribute | 空白分布在单词、字形集的边界,但连续文本或者草体除外 |
kashida | 通过拉长选定字符调整对齐方式 |
1.2、处理空白
空白在 HTML 文档中通常是被压缩或者直接忽略掉。这允许你将 HTML 文档的布局跟页面的外观分离。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
#fruittext {
width: 400px;
margin: 5px;
padding: 5px;
border: medium double black;
background-color: lightgrey;
}
</style>
</head>
<body>
<p id="fruittext">
There are lots of different kinds of fruit - there are over 500
varieties
of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
One of the most interesting aspects of fruit is the
variety available in each country. I live near London,
in an area which is
known for its apples.
</p>
</body>
</html>
上面代码中,文本包含了一些空格、制表符和换行符。浏览器遇到多个空格时,会将它们压缩为一个空格,而换行符等其他空白符则会直接被忽略。浏览器会自动处理文本换行,以便各行都能适应元素边界。
但有时候我们就想在 HTML 原文本中保留文本中的空白。这时,可以使用 whitespace 属性控制浏览器对空白字符的处理方式。
属性 | 说明 | 值 |
---|---|---|
whitespace | 指定空白字符的处理方式 | 参考下表 |
值 | 说明 |
---|---|
normal | 默认值,空白字符被压缩,文本自动换行 |
nowrap | 空白符被压缩,文本行不换行 |
pre | 空白符被保留,文本只在遇到换行符时候换行,这跟 pre 的效果一样 |
pre-line | 空白符被压缩,文本会在一行排满或遇到换行符时换行 |
pre-wrap | 空白符被保留,文本会在一行排满或遇到换行符时换行 |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
#fruittext {
width: 400px;
margin: 5px;
padding: 5px;
border: medium double black;
background-color: lightgrey;
white-space: pre-line;
}
</style>
</head>
<body>
<p id="fruittext">
There are lots of different kinds of fruit - there are over 500
varieties
of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
One of the most interesting aspects of fruit is the
variety available in each country. I live near London,
in an area which is
known for its apples.
</p>
</body>
</html>
1.3、指定文本方向
direction 属性告诉浏览器文本块的排列方向。
属性 | 说明 | 值 |
---|---|---|
direction | 设置文本方向 | ltr rtl |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
#first {
direction: ltr;
}
#second {
direction: rtl;
}
</style>
</head>
<body>
<p id="first">
This is left-to-right text
</p>
<p id="second">
This is right-to-lefttext
</p>
</body>
</html>
1.4、指定单词、字母、行之间的间距
可以告诉浏览器单词与单词、字母与字母、行与行之间的间距。
属性 | 说明 | 值 |
---|---|---|
letter-spacing | 设置字母之间的间距 | normal <长度值> |
word-spacing | 设置单词之间的间距 | Normal <长度值> |
line-height | 设置行高 | Normal <数值> <长度值> <%> |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
#fruittext {
margin: 5px;
padding: 5px;
border: medium double black;
background-color: lightgrey;
word-spacing: 10px;
letter-spacing: 2px;
line-height: 3em;
}
</style>
</head>
<body>
<p id="fruittext">
There are lots of different kinds of fruit - there are over 500
varieties of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
</p>
</body>
</html>
1.5、控制断词
word-wrap 属性告诉浏览器当一个单词的长度超过包含块的宽度时如何处理。
值 | 说明 |
---|---|
normal | 单词不断开,即使无法完全放入包含块元素 |
break-word | 断开单词,使其放入包含块元素 |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
p {
width:150px;
margin: 15px;
padding: 5px;
border: medium double black;
background-color: lightgrey;
float:left;
}
#first {
word-wrap: break-word;
}
#second {
word-wrap: normal;
}
</style>
</head>
<body>
<p id="first">
There are lots of different kinds of fruit - there are over 500
varieties of madeupfruitwithaverylongname alone.
</p>
<p id="second">
There are lots of different kinds of fruit - there are over 500
varieties of madeupfruitwithaverylongname alone.
</p>
</body>
</html>
1.6、首行缩进
text-indent 属性用于指定文本块首行缩进,值可以是长度值,也可以相对于元素宽度的百分数值。
属性 | 说明 | 值 |
---|---|---|
text-indent | 设置文本首行缩进 | <长度值> <%> |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
p {
margin: 15px;
padding: 5px;
border: medium double black;
background-color: lightgrey;
float:left;
text-indent: 15%;
}
</style>
</head>
<body>
<p>
There are lots of different kinds of fruit - there are over 500
varieties of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
One of the most interesting aspects of fruit is the
variety available in each country. I live near London, in an area which is
known for its apples.
</p>
</body>
</html>
2、文本装饰与大小写转换
text-decoration 和 text-transform 两个属性分别允许我们装饰文本和转换文本大小写。
属性 | 说明 | 值 |
---|---|---|
text-decoration | 为文本块应用装饰效果 | none underline overline line-through blink |
text-transform | 为文本块转换大小写 | none capitalize uppercase lowercase |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
p {
border: medium double black;
background-color: lightgrey;
text-decoration: line-through;
text-transform: uppercase;
}
</style>
</head>
<body>
<p>
There are lots of different kinds of fruit - there are over 500
varieties of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
One of the most interesting aspects of fruit is the
variety available in each country. I live near London, in an area which is
known for its apples.
</p>
</body>
</html>
3、创建文本阴影
属性 | 说明 | 值 |
---|---|---|
text-shadow | 为文本块应用阴影 | <h-shadow> <v-shadow> <blur> <color> |
blur 值定义了阴影的模糊程度。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
h1 {
text-shadow: 0.1em .1em 1px lightgrey;
}
p {
text-shadow: 5px 5px 20px black;
}
</style>
</head>
<body>
<h1>Thoughts about Fruit</h1>
<p>
There are lots of different kinds of fruit - there are over 500
varieties of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
</p>
</body>
</html>
4、使用字体
属性 | 说明 | 值 |
---|---|---|
font-family | 指定文本块采用的字体名称 | 参见下表 |
font-size | 指定文本块的字体大小 | 参见下表 |
font-style | 指定字体样式 | Normal italic oblique |
font-variant | 指定字体是否以小型大写字母显示 | Normal smallcaps |
font-weight | 设置字体粗细 | Normal bold bolder lighter |
font | 在一条声明中设置字体的简写属性 |
font 属性值的格式如下:
font: <font-style> <font-variant> <font-weight> <font-size> <font-family>
4.1、选择字体
font-family 属性指定使用的字体,按照优先顺序排列。浏览器从字体列表中的第一种开始尝试,直到发现合适的字体为止。
通用字体系列 | 实现字体系列 |
---|---|
serif | Times |
sans-serif | Helvetica |
cursive | Zapf-Chancery |
fantasy | Western |
monospace | Courier |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
p {
padding: 5px;
border: medium double black;
background-color: lightgrey;
margin: 2px;
float: left;
font-family: "HelveticaNeue Condensed", monospace;
}
</style>
</head>
<body>
<p>
There are lots of different kinds of fruit - there are over 500
varieties of banana alone. By the time we add the countless types of
apples, oranges, and other well-known fruit, we are faced with
thousands of choices.
</p>
</body>
</html>
4.2、设置字体大小
font-size 属性用来指定字体大小。
值 | 说明 |
---|---|
xx-small x-small small medium large x-large xx-large | 设置字体大小。浏览器会决定每个值代表具体大小。 |
smaller larger | 设置字体相对于父元素字体的大小 |
<length> | 使用 CSS 长度值精确设置字体大小 |
<%> | 将字体大小表示为父元素字体大小的百分数 |
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
p {
padding: 5px;
border: medium double black;
background-color: lightgrey;
margin: 2px;
float: left;
font-family: sans-serif;
font-size: medium;
}
#first {
font-size: xx-large;
}
#second {
font-size: larger;
}
</style>
</head>
<body>
<p>
There are lots of different kinds of fruit - there are over 500
varieties of <span id="first">banana</span> alone. By the time we add the
countless types of <span id="second">apples, oranges, and other
well-known fruit, we are faced with thousands of choices</span>.
</p>
</body>
</html>
4.3、设置字体样式和粗细
可以使用 font-style 属性设置字体粗细。font-style 属性允许我们在正常字体、斜体和假斜体(倾斜字体)三种之间选择。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman"/>
<meta name="description" content="A simple example"/>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
<style>
p {
padding: 5px;
border: medium double black;
background-color: lightgrey;
margin: 2px;
float: left;
font-family: sans-serif;
font-size: medium;
}
#first {
font-weight: bold;
}
#second {
font-style: italic;
}
</style>
</head>
<body>
<p>
There are lots of different kinds of fruit - there are over 500
varieties of <span id="first">banana</span> alone. By the time we add the
countless types of <span id="second">apples, oranges, and other
well-known fruit, we are faced with thousands of choices</span>.
</p>
</body>
</html>