关于height:100%和height:100vh的区别

9 篇文章 0 订阅
3 篇文章 0 订阅

1.em

在做手机端的时候经常会用到的做字体的尺寸单位

说白了 em就相当于“倍”,比如设置当前的div的字体大小为1.5em,则当前的div的字体大小为:当前div继承的字体大小*1.5

但是当div进行嵌套的时候,em始终是按照当前div继承的字体大小来缩放,参照后面的例子。

2.rem

这里的r就是root的意思,意思是相对于根节点来进行缩放,当有嵌套关系的时候,嵌套关系的元素的字体大小始终按照根节点的字体大小进行缩放。

参照后面给的demo

3.vh

vh就是当前屏幕可见高度的1%,也就是说

height:100vh == height:100%;

但是有个好处是当元素没有内容时候,设置height:100%该元素不会被撑开,

但是设置height:100vh,该元素会被撑开屏幕高度一致。

4.vw

vw就是当前屏幕宽度的1%

补充一句,当设置width:100%,被设置元素的宽度是按照父元素的宽度来设置,

但是100vw是相对于屏幕可见宽度来设置的,所以会出现50vw 比50%大的情况

<!DOCTYPE html>
<html lang="Zh-cn">
<head>
<meta charset="UTF-8">
<title>恭贺新春</title>
</head>
<style type="text/css" media="screen">
html{
font-size: 14px;
}
.em,
.em > .em-son,
.em > .em-son > .em-grandson {
font-size: 1.2em;
}
.rem,
.rem > .rem-son,
.rem > .rem-son > .rem-grandson {
font-size: 1.2rem;
}
.rem-box {
background: #d60b3b;
width:10rem;
height: 10rem;
color: #fff;
text-align: center;
line-height:5rem;
}
.vhvw-box {
background: #d60b3b;
width:50vw;
height: 50vh;
color: #fff;
text-align: center;
line-height:25vh;
}
</style>
<body>
<h1>em 继承父元素的字体大小,来变大或变小,多层嵌套字体变化</h1>
<div class="em">
字体大小 1.2 * 14(父元素body) = 16px
<div class="em-son">
字体大小 1.2 * 16(父元素em) = 20px
<div class="em-grandson">
字体大小 1.2 * 20(父元素em-son) = 24px
</div>
</div>
</div>
<br>
<h1>rem 继承根节点元素的字体大小,来变大或变小,多层嵌套字体不变化</h1>
<div class="rem">
字体大小 1.2 * 14(根节点html) = 16px
<div class="rem-son">
字体大小 1.2 * 14(根节点html) = 16px
<div class="rem-grandson">
字体大小 1.2 * 14(根节点html) = 16px
</div>
</div>
</div>
<br>
<h1>rem 也可作为固定长度单位设置宽高等</h1>
<div class="rem-box">
宽:14 * 10 = 140px<br>
高:14 * 10 = 140px
</div>
<br>
<h1>vh,vw 屏幕可见区域的高度,宽度的1%</h1>
<div class="vhvw-box">
宽:屏幕宽度的50%<br>
高:屏幕高度的50%
</div>
</body>
</html>

<!DOCTYPE html>
<html lang="Zh-cn">
<head>
<meta charset="UTF-8">
<title>恭贺新春</title>
</head>
<style type="text/css" media="screen">
html{
font-size: 14px;
}
.em,
.em > .em-son,
.em > .em-son > .em-grandson {
font-size: 1.2em;
}
.rem,
.rem > .rem-son,
.rem > .rem-son > .rem-grandson {
font-size: 1.2rem;
}
.rem-box {
background: #d60b3b;
width:10rem;
height: 10rem;
color: #fff;
text-align: center;
line-height:5rem;
}
.vhvw-box {
background: #d60b3b;
width:50vw;
height: 50vh;
color: #fff;
text-align: center;
line-height:25vh;
}
</style>
<body>
<h1>em 继承父元素的字体大小,来变大或变小,多层嵌套字体变化</h1>
<div class="em">
字体大小 1.2 * 14(父元素body) = 16px
<div class="em-son">
字体大小 1.2 * 16(父元素em) = 20px
<div class="em-grandson">
字体大小 1.2 * 20(父元素em-son) = 24px
</div>
</div>
</div>
<br>
<h1>rem 继承根节点元素的字体大小,来变大或变小,多层嵌套字体不变化</h1>
<div class="rem">
字体大小 1.2 * 14(根节点html) = 16px
<div class="rem-son">
字体大小 1.2 * 14(根节点html) = 16px
<div class="rem-grandson">
字体大小 1.2 * 14(根节点html) = 16px
</div>
</div>
</div>
<br>
<h1>rem 也可作为固定长度单位设置宽高等</h1>
<div class="rem-box">
宽:14 * 10 = 140px<br>
高:14 * 10 = 140px
</div>
<br>
<h1>vh,vw 屏幕可见区域的高度,宽度的1%</h1>
<div class="vhvw-box">
宽:屏幕宽度的50%<br>
高:屏幕高度的50%
</div>
</body>
</html>
优化下面代码.bg { width: 100%; height: 100vh; background-image: url('../../assets/img/info-bg.png'); background-size: 100% 100%; background-repeat: no-repeat; position: relative; font-family: AlibabaPuHuiTiR; .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #ffffff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #ffffff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #ffffff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background-image: url('../../assets/img/howo.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-zk-img { width: 56%; height: 90%; background-image: url('../../assets/img/heavyT.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-gj-img { width: 82%; height: 90%; background-image: url('../../assets/img/transit.png'); background-size: 100% 100%; background-repeat: no-repeat; } .car-online { margin-bottom: 50px; } } .day-data { display: flex; flex-direction: row; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #ffffff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; }
05-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值