关于rem的一点总结

  最近在写一个关于小说阅读的webApp,由于没有借用任何框架,所以很多底层的内容都需要自己去解决,幸好的是这次只是关于移动端的内容,还不至于去向着jquery的方向码代码。言归正传,前几天在处理底色切换的时候,由于需要做到自适应即盒子的高度随着盒子的宽度变化,各种找资料发现了3中比较典型的解决办法,具体请点下面链接:

     移动端布局:写一个自适应的正方形盒子

  而也从这些解决办法中学习到了vw和vh的使用,从此就对vw和vh爱不释手了,想着现在反正写的是一个移动端的网页不需要去考虑神马兼容性吧,可是在昨晚悲催的通过UC浏览器真机测试,发现真有不兼容的。。

  

  不兼容只好去找替代品了啊,早就听说过rem的大名,确却没有真正的使用过,这次就让我们好好的了解一下吧!

Question One:rem是什么?

  rem(font size of the root element)是指相对于根元素(即HTML)的字体大小的单位。简单的说它就是一个相对单位。看到rem大家一定会想起em单位,em(font size of the element)是指相对于父元素的字体大小的单位。它们之间其实很相似,只不过一个计算的规则是依赖根元素一个是依赖父元素计算。

Question Two:rem有神马优点?

  rem能等比例适配所有屏幕。 rem是通过根元素进行适配的,网页中的根元素指的是html我们通过设置html的字体大小就可以控制rem的大小。举个例子:

html{
font-size:20px;
}
.btn {
width: 6rem;
height: 3rem;
line-height: 3rem;
font-size: 1.2rem;
display: inline-block;
background: #06c;
color: #fff;
border-radius: .5rem;
text-decoration: none;
text-align: center;
}

  我把html设置成10px是为了方便我们计算,为什么6rem等于60px。如果这个时候我们的.btn的样式不变,我们再改变html的font-size的值,看看按钮发生上面变化:

html{
font-size:40px;
}

  上面的width,height变成了上面结果的两倍,我们只改变了html的font-size,但.btn样式的width,height的rem设置的属性不变的情况下就改变了按钮在web中的大小。

  其实从上面两个案例中我们就可以计算出1px多少rem:

  第一个例子:

    120px = 6rem * 20px(根元素设置大值)

  第二个例子:

    240px = 6rem * 40px(根元素设置大值)

  推算出:

    10px = 1rem 在根元素(font-size = 10px的时候);

    20px = 1rem 在根元素(font-size = 20px的时候);

    40px = 1rem 在根元素(font-size = 40px的时候);

  在上面两个例子中我们发现第一个案例按钮是等比例放大到第二个按钮,html font-size的改变就会导致按钮的大小发生改变,我们并不需要改变先前给按钮设置的宽度和高度,其实这就是我们最想看到的。

Question Three:rem需要怎么用?

  因为是要计算rem的值,所以我们前端在看到设计图量尺寸的时候会去计算下这个东西,需要花费一些时间,所以提供了Sass(不知道的自己去百度)和Less(不知道的自己去百度)相对变量的代码,

  Sass相对变量地址:Sass相对变量

  Less相对变量地址:Less相对变量

手机自适应代码:

viewport标签:

<meta name="viewport"content="initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5,user-scalable=no,minimal-ui"/>

JavaScript代码:

<script>
!function (win) { function resize() {
var domWidth = domEle.getBoundingClientRect().width;
if (domWidth / v > 540) {
domWidth = 540 * v;
}
win.rem = domWidth / 16;
domEle.style.fontSize = win.rem + "px";
} var v,
initial_scale,
timeCode,
dom = win.document,
domEle = dom.documentElement,
viewport = dom.querySelector('meta[name="viewport"]'),
flexible = dom.querySelector('meta[name="flexible"]'); if (viewport){
//viewport:<meta name="viewport"content="initial-scale=0.5, minimum-scale=0.5, maximum-scale=0.5,user-scalable=no,minimal-ui"/> var o = viewport.getAttribute("content").match(/initial\-scale=(["']?)([\d\.]+)\1?/); if (o) { initial_scale = parseFloat(o[2]);
v = parseInt(1 / initial_scale);
}
}
else{
if (flexible) {
var o = flexible.getAttribute("content").match(/initial\-dpr=(["']?)([\d\.]+)\1?/);
if (o) {
v = parseFloat(o[2]);
initial_scale = parseFloat((1 / v).toFixed(2))
}
}
}
if (!v && !initial_scale) {
var n = (win.navigator.appVersion.match(/android/gi), win.navigator.appVersion.match(/iphone/gi));
v = win.devicePixelRatio;
v = n ? v >= 3 ? 3 : v >= 2 ? 2 : 1 : 1, initial_scale = 1 / v
}
//没有viewport标签的情况下
if (domEle.setAttribute("data-dpr", v), !viewport) {
if (viewport = dom.createElement("meta"),
viewport.setAttribute("name", "viewport"),
viewport.setAttribute("content", "initial-scale=" + initial_scale +
",maximum-scale=" + initial_scale + ", minimum-scale=" + initial_scale +
",user-scalable=no"),
domEle.firstElementChild
) {
domEle.firstElementChild.appendChild(viewport)
} else {
var m = dom.createElement("div");
m.appendChild(viewport), dom.write(m.innerHTML)
}
}
win.dpr = v; win.addEventListener("resize", function () {
clearTimeout(timeCode), timeCode = setTimeout(resize, 300)
}, false); win.addEventListener("pageshow", function (b) {
b.persisted && (clearTimeout(timeCode), timeCode = setTimeout(resize, 300))
}, false); /* 完成后就把body的字体设置为12
"complete" === dom.readyState ?
dom.body.style.fontSize = 12 * v + "px"
: dom.addEventListener("DOMContentLoaded", function() {
dom.body.style.fontSize = 12 * v + "px"
}, false);
*/
resize();
}(window);
</script>

Question Three:浏览器对rem的兼容性

  支持的浏览器还是蛮多的,比如:Mozilla Firefox 3.6+、Apple Safari 5+、Google Chrome、IE9+和Opera11+。总体来说还是很不错的,rem的出现可以说是让我们的webPage和webApp如虎添翼。

参考来源:

    web app变革之rem

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}
/*
h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}
*/
body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}
.postBody a:link, .postBody a:visited, .postBody a:active ,.postBody a,.postBody a:hover,{
text-decoration: none;
}
a:hover{
cursor: pointer;
}
/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/
/*
table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}*/

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

关于rem的一点总结【原创】的更多相关文章

  1. 关于px em rem的一点小总结

    2015-11-28 06:06:40 概念 都是CSS单位. px:像素 Pixel.像素 (计算机屏幕上的一个点) em:1em 等于当前的字体尺寸. 2em 等于当前字体尺寸的两倍. 例如,如果 ...

  2. 设计模式PDF下载了4.0万本!那,再肝一本《Java面经手册》吧!

    作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 1. 先祝贺下自己拿下4.0万本下载量! <重学Java设计模式>PDF ...

  3. (淘宝无限适配)手机端rem布局详解(转载非原创)

    从网易与淘宝的font-size思考前端设计稿与工作流 本文结合自己对网易与淘宝移动端首页html元素上的font-size这个属性的思考与学习,讨论html5设计稿尺寸以及前端与设计之间协作流程的问 ...

  4. 关于rem自适应的一点研究

    参考地址:http://m.ctrip.com/html5/ https://www.amazon.cn/ rem是相对于html根元素的一个单位.rem是px的16倍,即1rem = 16px;除了 ...

  5. [原创].NET 分布式架构开发实战之三 数据访问深入一点的思考

    原文:[原创].NET 分布式架构开发实战之三 数据访问深入一点的思考 .NET 分布式架构开发实战之三 数据访问深入一点的思考 前言:首先,感谢园子里的朋友对文章的支持,感谢大家,希望本系列的文章能 ...

  6. 寻找丢失的微服务-HAProxy热加载问题的发现与分析 原创: 单既喜 一点大数据技术团队 4月8日 在一点资讯的容器计算平台中,我们通过HAProxy进行Marathon服务发现。本文记录HAProxy服务热加载后某微服务50%概率失效的问题。设计3组对比实验,验证了陈旧配置的HAProxy在Reload时没有退出进而导致微服务丢失,并给出了解决方案. Keywords:HAProxy热加

    寻找丢失的微服务-HAProxy热加载问题的发现与分析 原创: 单既喜 一点大数据技术团队 4月8日 在一点资讯的容器计算平台中,我们通过HAProxy进行Marathon服务发现.本文记录HAPro ...

  7. 【原创】Android selector选择器无效或无法正常显示的一点研究

    想将LinearLayout作为一个按钮,加上一个动态背景,按下的时候,背景变色,这个理所当然应该使用selector背景选择器来做: <LinearLayout android:id=&quo ...

  8. 【原创】C#玩高频数字彩快3的一点体会

    购彩风险非常高,本人纯属很久以前对数字高频彩的一点研究.目前已经远离数字彩,重点研究足球篮球比赛资料库和赛果预测. 这是一篇在草稿箱保存了1年多的文章,一直没发现,顺便修改修改分享给大家.以后会有更多 ...

  9. openMP的一点使用经验【非原创】

    按照百科上说的,针对于openmp的编程,最简单的就是在开头加个#include<omp.h>,然后在后面的for上加一行#pragma omp parallel for即可,下面的是较为 ...

随机推荐

  1. JS获取移动端系统信息(操作系统、操作系统版本、横竖屏状态、设备类型、网络状态、生成浏览器指纹)

    function getOS() { // 获取当前操作系统 var os; if (navigator.userAgent.indexOf('Android') > -1 || navigat ...

  2. Codeforces Round #311 (Div. 2)B. Pasha and Tea二分

    B. Pasha and Tea time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. Naming Company CodeForces - 794C

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...

  4. centos ldap client 设定

    centos 6.4 ldap server 位于ubuntu 12.04 Server上 1.安装 yum -y install openldap-clients nss-pam-ldapd 一个完 ...

  5. concurrent.futures 使用及解析

    from concurrent.futures import ThreadPoolExecutor, as_completed, wait, FIRST_COMPLETED from concurre ...

  6. 元类编程--__getattr__, __getattribute__

    #__getattr__, __getattribute__ #__getattr__ 就是在查找不到属性的时候调用 from datetime import date class User: def ...

  7. Android开发——为移动的Paint元素指定图片的方法

    源  起 最近在写一个类似“围住神经猫”的应用,现在需要给一个可以移动的Paint元素指定一张图片,如下图,要把黄点改成其他图片: Paint所在的类继承于SurfaceView,SurfaceVie ...

  8. quick-cocos2dx lua中读取 加密 csv表

    我非常想把一些非必需的信息以CSV表的格式保存到客户端,以减少和服务器的通讯,降低压力.于是写了这么一个. 但因为大家觉得这样的话,需要每次登陆时来检测同步这些数据,会减慢登陆速度,于是没有用到. 我 ...

  9. 老生常谈-Activity(山东数漫江湖)

    对于activity的七个声生命周期回调,总是被大家翻来覆去的说,甚至说的都有些厌烦了,这部分知识虽然基础但也很重要,谁都不想在面试的时候只说出个一知半解,下面的分析是对阅读<安卓开发艺术探索& ...

  10. 大聊Python----通过Socket实现简单的ssh客户端

    光只是简单的发消息.收消息没意思,干点正事,可以做一个极简版的ssh,就是客户端连接上服务器后,让服务器执行命令,并返回结果给客户端. #ssh_client.py import socket cli ...