-
Notifications
You must be signed in to change notification settings - Fork 0
/
rem.html
64 lines (60 loc) · 1.67 KB
/
rem.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta charset="UTF-8">
<title>rem</title>
<script>
(function() {
//计算html的font-size 写在html里面 如果加载js会出现闪的情况,写在head里更好
var deviceWidth = document.documentElement.clientWidth;
//限制的超过750后就按750的计算
if (deviceWidth > 750) deviceWidth = 750;
//这里在750下font-size是100px 即 1rem == 100px(可更改) 其他屏幕会自动适配(缩放)
document.documentElement.style.fontSize = deviceWidth / 7.5 + 'px';
})();
</script>
<style>
/*
200px
*/
.div1{
background: red;
width: 2rem;
height: 2rem;
font-size: 0.16rem;
}
/*
50px 100px
*/
.div2{
background: blue;
width: 0.5rem;
height: 1rem;
font-size: 0.20rem;
}
/*
150px 250px
*/
.div3{
width: 1.5rem;
height: 2.5rem;
font-size: 0.14rem;
background: green;
border: 0.01 solid #ddd;
}
</style>
</head>
<body>
<div class="div1">
div的大小和字体大小会根据不同屏幕缩放的
</div>
<div class="div2">
二十
</div>
<div class="div3">
该方法不好解决border 1px问题,border的宽度户不变
</div>
</body>
</html>