CSS 中的相对长度单位

cssweb developmentfront end technology更新于 2024/12/10 12:28:17

CSS 中的相对长度单位用于指定相对于另一个长度属性的长度。

Sr.NoUnit &说明
1em
相对于元素的字体大小,例如 4em 表示当前字体大小的 4 倍。
2ex
相对于当前字体的 x 高度
3ch
相对于 0 的宽度
4rem
相对于根元素的字体大小
5vw
相对于视口宽度的 1%*
6vh
相对于视口高度的 1%*
7vmin
相对于视口较小尺寸的 1%*
8vmax
相对于视口较大尺寸的 1%*
9%
相对于父级元素

示例

让我们看一个使用相对长度单位的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
.demo {
   text-decoration: overline underline;
   text-decoration-color: blue;
   font-size: 1.4em;
}
</style>
</head>
<body>
<h1>Details</h1>
<p class="demo">Examination Center near ABC College.</p>
<p class="demo2">Exam begins at 9AM.</p>
</body>
</html>

输出

示例

现在让我们看另一个例子 −

<!DOCTYPE html>
<html>
<head>
<style>
.demo {
   text-decoration: overline underline;
   text-decoration-color: blue;
   font-size: 4ch;
}
</style>
</head>
<body>
<h1>Details</h1>
<p class="demo">Examination Center near ABC College.</p>
<p class="demo2">Exam begins at 9AM.</p>
</body>
</html>

输出


相关文章


有用资源