CSS教程
CSS进阶

如何在CSS中更改字体大小?

CSS中 font-size属性影响元素文本的大小。用于指定字体的高度和大小。此属性的默认值为 medium ,并且可以应用于每个元素。此属性的值是 xx-small,small,x-small 等。
此属性具有许多值,这些值负责更改内容的大小。长度值可以为cm,px,pt等。字体大小不允许为负数。
注意:注意:如果未定义字体大小,则对于普通文本(如段落),默认大小为16px,等于1em。
字体大小可以是绝对的或相对的。绝对大小将文本设置为确定大小,而相对大小将文本设置为相对于相邻元素。

语法

font-size: medium | large | x-large | xx-large | xx-small | x-small | small |smaller| larger | length;
让我们了解如何通过使用一些插图来更改字体大小。

示例-使用字体大小值

<!DOCTYPE html>
<html>
<head>
<style>
#p1 {
  font-size: medium;
}
#p2 {
  font-size: large;
}
#p3 {
  font-size: x-large;
}
#p4 {
  font-size: xx-large;
}
#p5 {
  font-size: xx-small;
}
#p6 {
  font-size: x-small;
}
#p7 {
  font-size: small;
}
#p8 {
  font-size: smaller;
}
#p9 {
  font-size: larger;
}
</style>
</head>
<body>
<p id="p1">A paragraph with font-size: medium;</p>
<p id="p2">A paragraph with font-size: large;</p>
<p id="p3">A paragraph with font-size: x-large;</p>
<p id="p4">A paragraph with font-size: xx-large;</p>
<p id="p5">A paragraph with font-size: xx-small;</p>
<p id="p6">A paragraph with font-size: x-small;</p>
<p id="p7">A paragraph with font-size: small;</p>
<p id="p8">A paragraph with font-size: smaller;</p>
<p id="p9">A paragraph with font-size: larger;</p>
</body>
</html>
输出
如何更改CSS中的字体大小

示例中-使用长度值

在此示例中,我们将长度值与 em,vw,px, cm
<!DOCTYPE html>
<html>
<head>
<style>
#p1 {
  font-size: 2.5em; /* 40px/16=2.5em */
}
#p2 {
  font-size: 5vw;
}
#p3 {
  font-size: 40px;
}
#p4 {
  font-size: 1.5cm;
}
</style>
</head>
<body>
<p id="p1">A paragraph with font-size: 2.5em;</p>
<p id="p2">A paragraph with font-size: 5vw;</p>
<p id="p3">A paragraph with font-size: 40px;</p>
<p id="p4">A paragraph with font-size: 1.5cm;</p>
</body>
</html>
输出
如何更改CSS中的字体大小
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4