rem是如何实现布局的?
rem是相对于根元素,这样就意味着,我们只需要在根元素确定一个px字号,则可以来算出元素的宽高。本文讲的是如何使用rem实现自适应。
rem是什么?
说到rem自然就会想到em,我们知道em是相对于父元素的字体大小的单位,那么rem则是相对于根元素也就是元素的字体大小的单位。
上面说过rem是通过根元素进行适配的,网页中的根元素指的是html我们通过设置html的字体大小就可以控制rem的大小。举个例子:
css部分
*{
margin: 0;
padding: 0;
box-sizing: border-box;
list-style: none;
}
html{
font-size: 62.5%;
}
header{
width: 100%;
height:0.9rem;
background:url(../img/上状态栏@2x.png);
background-size: 100% 100%;
}
nav{
width: 90%;
height: 100%;
}
nav div:nth-of-type(1){
width: 10%;
height:100%;
background: url(../img/fanhui@2x.png) no-repeat;
background-position: 0.5rem;
background-size:0.22rem 0.4rem;
float: left;
}
nav div:nth-of-type(2){
width: 90%;
height: 100%;
font-size: 0.32rem;
text-align: center;
line-height: 0.9rem;
color:white;
float: left;
}
.content{
width: 100%;
height: 4.2rem;
position: relative;
}
.content img{
width: 1.3rem;
height: 2rem;
position: absolute;
margin:auto;
left:0;
bottom:0;
top:0;
right:0;
}
.inp_one{
width: 80%;
height: 0.88rem;
display: block;
margin: 0 auto;
outline: none;
border: 1px solid #f5f5f5;
box-shadow: 0px 1px 5px #ebebeb;
padding-left: 0.2rem;
}
.inp_two{
width: 80%;
height: 0.88rem;
display: block;
margin: 0 auto;
outline: none;
border: 1px solid #f5f5f5;
box-shadow: 0px 1px 5px #ebebeb;
padding-left: 0.2rem;
margin-top: 0.5rem;
}
.zhuce{
width: 80%;
height: 0.4rem;
margin: 0 auto;
margin-top: 0.36rem;
margin-bottom: 1.16rem;
display: flex;
justify-content: space-between;
}
.zhuce a{
text-decoration: none;
border: 1px solid #adadad;
font-size: 0.2rem;
color: #585858;
padding: 0.08rem;
display: inline-block;
line-height: 0.2rem;
border-radius: 0.07rem;
}
.denglu{
width:3.17rem ;
height: 0.83rem;
border-radius:0.07rem;
outline: none;
background: #d13d3b;
border: none;
color: white;
display: block;
margin: 0 auto;
cursor: pointer;
}
HTML部分
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="css/denglu.css"/>
<title>登录</title>
</head>
<body>
<header>
<nav>
<div></div>
<div>登录</div>
</nav>
</header>
<div class="content">
<img src="img/logo@2x.png" >
</div>
<input type="text" placeholder="输入账号" class="inp_one"/>
<input type="password" placeholder="输入密码" class="inp_two"/>
<div class="zhuce">
<a href="wangjimima.html" class="a_one">忘记密码</a>
<a href="zhuce.html" class="c_two">立即注册</a>
</div>
<button class="denglu">登录</button>
</body>
<script type="text/javascript">
(function (doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
recalc = function () {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return;
if(clientWidth>=750){
docEl.style.fontSize = '100px';
}else{
docEl.style.fontSize = 100 * (clientWidth / 750) + 'px';
}
};
if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
</script>
</html>
****如果页面的宽度超过了750px,那么页面中html的font-size恒为100px,否则,页面中html的font-size的大小为: 100 * (当前页面宽度 / 750) **于是,问题来了,为什么要这样?别急,我先来一一回答为什么是750px?对于手机屏幕来说,750px的页面宽度是一个安全的最大宽度,保证了移动端页面两边不会留白。注意这里的px是css逻辑像素,与设备的物理像素是有区别的。如iPhone 5使用的是Retina视网膜屏幕,使用2px x 2px的 device pixel 代表 1px x 1px 的 css pixel,所以设备像素数为750 x 1136px,而它的CSS逻辑像素数为320 x 568px。如果要切移动端页面,你可以先把效果图宽度等比例缩放到750px,很好用。为什么要设置html的font-size?rem就是根元素(即:html)的字体大小。html中的所有标签样式凡是涉及到尺寸的(如: height,width,padding,margin,font-size。甚至,left,top等)你都可以放心大胆的用rem作单位。如果你把html的font-size设为20px,前面说过,rem就是html的字体大小,那么1rem = 20px。此时,此时宽60px,高40px的元素样式就这样设置如下 ↓width: 3rem; height: 2rem;
那要是宽55px,高37px呢?然后经过换算,,设置如下 ↓
width: 2.75rem; height: 1.85rem;
是不是发现这换算起来有点麻烦啊,,,(当然,你要是心算帝请无视)
如果我们一开始把html的font-size设为100px呢?此时1rem = 100px,那么上面的宽55px,高37px的元素样式就可以这么设置 ↓width: 0.55rem; height: 0.37rem;
是不是换算起来简单多了?!**