CSS
rem 布局原理
em 是什么 em 是相对单位, 在 font-size 中使用 em, 代表相对父元素字体大小, 在其他属性中使用 em, 意味着相对当前选择器字体大小 em 的题 问:s1 s1 s5 s6 的 font-size 各是多少?答案在底部 rem 是什么 rem 作为非根元素时, 相对于根元素大小 rem 的本质是基于宽度的等比缩放, 一般使用 js 来设置 弹性布局与响应式布局 弹性布局是响应…
em 是什么 em 是相对单位, 在 font-size 中使用 em, 代表相对父元素字体大小, 在其他属性中使用 em, 意味着相对当前选择器字体大小 em 的题 问:s1 s1 s5 s6 的 font-size 各是多少?答案在底部 rem 是什么 rem 作为非根元素时, 相对于根元素大小 rem 的本质是基于宽度的等比缩放, 一般使用 js 来设置 弹性布局与响应式布局 弹性布局是响应…
em 是相对单位, 在 font-size 中使用 em, 代表相对父元素字体大小, 在其他属性中使用 em, 意味着相对当前选择器字体大小
<div class="p1">
<div class="s1">1</div>
<div class="s2">1</div>
</div>
<div class="p2">
<div class="s5">1</div>
<div class="s6">1</div>
</div>
.p1 {
font-size: 16px;
line-height: 32px;
}
.s1 {
font-size: 2em;
}
.s2 {
font-size: 2em;
line-height: 2em;
}
.p2 {
font-size: 16px;
line-height: 2;
}
.s5 {
font-size: 2em;
}
.s6 {
font-size: 2em;
line-height: 2em;
}
问:s1 s1 s5 s6 的 font-size 各是多少?答案在底部
rem 作为非根元素时, 相对于根元素大小
html {
font-size: 16px;
}
/* p的font-size为48px */
p {
font-size: 3rem;
}
rem 的本质是基于宽度的等比缩放, 一般使用 js 来设置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>rem+js布局</title>
<style>
body {
font-size: 16px;
margin: auto;
padding: 0;
width: 10rem;
border: 1px solid palevioletred;
box-sizing: border-box;
}
.p1 {
width: 50%;
height: 5rem;
box-sizing: border-box;
border: 1px solid yellowgreen;
}
.s1 {
/* 28px/72 */
font-size: 0.38888888rem;
color: #666;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="p1">
宽度为屏幕宽度的50%
<div class="s1">
这是字体
</div>
</div>
</body>
<script>
var documentElement = document.documentElement;
function callback() {
var clientWidth = documentElement.clientWidth;
// 屏幕宽度大于720,不在放大
clientWidth = clientWidth < 720 ? clientWidth : 720;
documentElement.style.fontSize = clientWidth / 10 + "px";
}
document.addEventListener("DOMContentLoaded", callback);
window.addEventListener("orientationchange" in window ? "orientationchange" : "resize", callback);
</script>
</html>
@media screen and (min-width: 320px) {
body {
font-size: 18px;
}
}
@media screen and (min-width: 481px) and (max-width: 640px) {
body {
font-size: 34px;
}
}
@media screen and (min-width: 641px) {
body {
font-size: 42px;
}
}
p {
font-size: 2.4em;
}
p a {
font-size: 1.4em;
}
/* font-size为16px,line-height为32px */
.p1 {
font-size: 16px;
line-height: 32px;
}
/* font-size为32px,line-height为32px */ /* line-height不存在,继承父值,继承32,32px */
.s1 {
font-size: 2em;
}
/* font-size为32px,line-height为64px */
.s2 {
font-size: 2em;
line-height: 2em;
}
/* font-size为16px,line-height为32px */ /* line-height: 2 是自身字体大小的两倍*/
.p2 {
font-size: 16px;
line-height: 2;
}
/* font-size为32px,line-height为64px */ /* line-height不存在,继承原始值,继承2,是自身字体大小的两倍 */
.s5 {
font-size: 2em;
}
/* font-size为32px,line-height为64px */
.s6 {
font-size: 2em;
line-height: 2em;
}
暂无评论
确认操作
请再次确认编辑图片
待上传图片 (剩余 0 张)
项目详情
确认评论邮箱
邀请制评论