Html5移动端页面自适应百分比布局

按百分比布局,精度肯定不会有rem精确

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
} .one {
width: 20%;
height: 100px;
float: left;
background: red;
} .two {
width: 30%;
height: 100px;
float: left;
background: blue;
} .three {
width: 50%;
height: 100px;
float: left;
background: green;
} .four {
width: 50%;
height: 50%;
background: #fff;
}
</style>
</head> <body>
<div class="one"></div>
<div class="two"></div>
<div class="three">
<div class="four"></div>
</div> </body> </html>

meta就不多说了,同样在meta标签内

<meta name="viewport" content="width=device-width,initial-scale=1 user-scalable=0">

在body下面有三个div,其中第一个div样式如下,

.one {
width: 20%;
height: 100px;
float: left;
background: red;
}

在样式里面,可以看到width我设置为20%,在以容器body为父级

占据body的width的20%

但是height无法被设置为百分比,为什么?因为无法确定父级容器body的height

总之一句话,要想设置当前容器的高度或宽度百分比,必须“明确”知道父容器的高度或宽度

所以height只能用px来表示

可以看到在上面的例子中截取的代码

 .three {
width: 50%;
height: 100px;
float: left;
background: green;
} .four {
width: 50%;
height: 50%;
background: #fff;
} <div class="three">
<div class="four"></div>
</div>

在已经确定父级div的宽高的情况下,子级div就可以用百分比来表示了,截图如下

Html5移动端页面自适应百分比布局

上一篇:Beta Scrum Day 5 — 听说


下一篇:css 让背景图片不停旋转