<style>
html{
font-size: 100px;
}
div{
background-color: #ccc;
margin-top: 10px;
font-size: 0.15rem;
}
p{background-color: #ccc;}
style>
<body>
<p style="font-size: 0.1rem">rem 1p>
<p style="font-size: 0.2rem">rem 2p>
<div>
this is div1
div>
<div>
this is div2
div>
body>
首先,进行media-query,根据不同的屏幕宽度设置根元素font-size。然后,通过rem,基于根元素的相对单位。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Documenttitle>
<style>
/* iphone5 或者更小的尺寸 */
@media only screen and (max-width: 374px){
html{
font-size: 86px;
}
}
/* iphone6/7/8 和 iphone x */
@media only screen and (min-width: 375px) and (max-width: 413px){
html{
font-size: 100px;
}
}
/* iphone6p 或者更大 */
@media only screen and (min-width: 375px){
html{
font-size: 110px;
}
}
body{
font-size: 0.16rem;
}
#div1{
width: 1rem;
background-color: red;
}
style>
head>
<body>
<div id = "div1">
this is div
div>
body>
html>
网页视口尺寸:
属性 | 解释 |
---|---|
window.screen.height | 屏幕高度 |
window.innerHeight | 网页视口高度 |
document.body.clientHeight | body高度 |
<style>
body{
margin: 0;
padding: 0;
}
#container{
background-color: red;
width: 10vw;
height: 10vh;
}
style>
<body>
<p>vw vh 测试p>
<div id = "container">div>
body>