1、px:像素(Pixel)

px是相对长度单位,他是相对于显示器屏幕分辨率而言的

优点:比较稳定、精确

缺点:在浏览器 中放大或者缩小浏览页面,会出现页面混乱的情况。

如下例子:

.buttonPX{
width:100px;
height:60px;
line-height:60px;
display: inline-block;
font-weight:bold;">#00a0b6;
-webkit-border-radius:90px;
-moz-border-radius:60px;
border-radius:15px;
font-size:24px;
color:#fff;
text-decoration: none;
text-align: center;
border:none
}
<button type="button" class="buttonPX">确定</button>
效果:

2、em:是相对长度单位

em是相对于父元素来设计字体大小的,如果当前对行内文本的字体尺寸为被人为设置,则em是相对于浏览器的默认字体尺寸。浏览器默认的字体是16px,所以未经调整的浏览器都符合 1em = 16px,为了方便使用em时,通常在css的body中声明font-size=62.5%(16*62.5%=10px)

缺点:em的值并不是固定的,他会继承父元素的字体大小

如同样设置48px的字体,设置父元素的大小和不设置父元素的大小的区别

.box1{
font-size:12px;
}
.buttonEm{
width:200px;
height:60px;
line-height:60px;
display: inline-block;
font-weight:bold;">#00a0b6;
-webkit-border-radius:90px;
-moz-border-radius:60px;
border-radius:15px;
font-size:4em;//注意此处
color:#fff;
text-decoration: none;
text-align: center;
border:none
}
.buttonEm2{
width:200px;
height:60px;
line-height:60px;
display: inline-block;
font-weight:bold;">#00a0b6;
-webkit-border-radius:90px;
-moz-border-radius:60px;
border-radius:15px;
font-size:3em; //注意此处
color:#fff;
text-decoration: none;
text-align: center;
border:none
}
<div class="box1">
<button type="button" class="buttonEm">确定</button> //设置了父元素的字体大小
</div>
<br>
<div class="box2">
<button type="button" class="buttonEm2">确定</button> //未设置父元素的字体大小
</div>
如果将其中的宽度等其他元素也改为em单位,则width=10em的实际宽度为width=48(行内文本字体的尺寸)*10 = 480px,也就是width的基本单位1em=fontsize的大小

3、rem (root em,根 em)

rem是相对单位,是相对与html根节点的单位

rem 主要是通过html 节点的fontsize确定的,所以不存在逐层复合的连锁反应

在JS的window.resize 事件中,动态的控制html节点的fontsize的大小,来动态控制rem的比例

设置rem单位的方法

方法一:直接在css样式中写一下代码即可:

html{

font-size:20px; //表示1rem相当于常用的20px,1rem = 20px

}

为了兼容适配不同的屏幕,也可斜成下面这样

html{
font-size : 20px;
}
@media only screen and (min-width: 401px){
html {
font-size: 25px !important;
}
}
@media only screen and (min-width: 428px){
html {
font-size: 26.75px !important;
}
}
@media only screen and (min-width: 481px){
html {
font-size: 30px !important;
}
}
@media only screen and (min-width: 569px){
html {
font-size: 35px !important;
}
}
@media only screen and (min-width: 641px){
html {
font-size: 40px !important;
}
}

方法二:使用js动态的实现此效果

(function(doc, win) {
var docEl = doc.documentElement,
resizeEvt = 'orientationchange' in window ? 'orientationchange': 'resize',
recalc = function() {
var clientWidth = docEl.clientWidth;
if (!clientWidth) return; if (clientWidth > 1920) {
clientWidth = 1920;
docEl.style.fontSize = 25 * (clientWidth / 1200) + 'px'; //font-size=25
}
else if (clientWidth > 900) {
docEl.style.fontSize = 25 * (clientWidth / 1200) + 'px';//font-size = 25
}
else if (clientWidth >= 320) {
docEl.style.fontSize = 100 * (clientWidth / 500) + 'px';//font-size = 100
}
else if (clientWidth >= 320) {
clientWidth = 320;
docEl.style.fontSize = 100 * (clientWidth / 500) + 'px';//font-size = 100
}
}; if (!doc.addEventListener) return;
win.addEventListener(resizeEvt, recalc, false);
doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);

除了ie8以外,其他的都兼容rem

浅谈rem、em、px的更多相关文章

  1. mobile css & rem & em & px

    mobile css & rem & em & px 1 rem === 16px 任意浏览器的默认字体高都是 16px, 所有未经调整的浏览器都符合: 1em=16px, 那 ...

  2. 字体的设置 REM EM PX

    px 1 一般设置页面的字体使用px 2 优点:字体设置比较稳定和精确 3 缺点:他会修改用户浏览器中的字体大小 EM 相对于父元素的字体大小,字体大小不确定,容易混乱,“em”是相对于其父元素来设置 ...

  3. rem ,em ,px的区别

    参考网址:http://www.cnblogs.com/leejersey/p/3662612.html

  4. 简谈CSS 中的 em,rem,px,%

    在实际工作中,可能我们用的比较多的是‘%’ 和 px,但是我们也经常看到很多网站和css框架里用的是em 或rem.而‘%’ 和px已经都是比较常见或者说是常用.但是em 和rem 却鲜有使用,一直以 ...

  5. EM vs REM vs PX,为什么你不应该”只用px“”

    Actually this artical is from other person's opnion ,i just put it into chinese,and this means a ver ...

  6. 浅谈css3长度单位rem,以及移动端布局技巧

    rem是什么? rem是css3中新增加的一个单位属性(font size of the root element),根据页面的根节点的字体大小进行转变的单位.root!!!!!!!!!根节点,也就是 ...

  7. 浅谈静态布局、流式布局,rem布局,弹性布局、响应式布局

    静态布局: 特点:没有兼容性问题 PC:居中布局,所有样式使用绝对宽度/高度(px),设计一个Layout,在屏幕宽高有调整时,使用横向和竖向的滚动条来查阅被遮掩部分:移动设备:另外建立移动网站,单独 ...

  8. 【转】Android Canvas的save(),saveLayer()和restore()浅谈

    Android Canvas的save(),saveLayer()和restore()浅谈 时间:2014-12-04 19:35:22      阅读:1445      评论:0      收藏: ...

  9. 浅谈OCR之Onenote 2010

    原文:浅谈OCR之Onenote 2010 上一次我们讨论了Tesseract OCR引擎的用法,作为一款老牌的OCR引擎,目前已经开源,最新版本3.0中更是加入了中文OCR功能,再加上Google的 ...

随机推荐

  1. LVS+keepalived负载均衡实战

    1 首先安装虚拟机 安装系统 这里 配置两台虚拟机 1:192.168.137.102   2:192.168.137.103 分别安装tomcat 默认80端口,同时都是开启状态 配置192.168 ...

  2. Mysql 关键字及保留字

    Table 10.2 Keywords and Reserved Words in MySQL 5.7 ACCESSIBLE (R) ACCOUNT[a] ACTION ADD (R) AFTER A ...

  3. Mysql的一些常用方法

    公司近期为新来同事做了一次培训,我也偷偷溜进去观摩了一下,内容虽然很基础,但是还是挺有用的.这里做了一下资料汇总: 2种存储引擎 InnoDB:支持事务处理.外键.行级锁,游戏业务库使用 MyISAM ...

  4. ue4 c++ 接口

    使用UE4接口比起普通的高级语言,要多做很多工作,是因为要兼容蓝图的使用,有一些小坑需要注意,开始吧. 1.新建接口类 打开UE4编辑器,与往常一样,新建C++类,然后选择Object继承,然后取名字 ...

  5. c++程序设计之编程思想

    代码块愈小,代码的功能就愈容易管理,代码的处理和移动就愈轻松. 任何一个傻瓜都能写出计算机可以理解的代码,唯有写出人类容易理解的代码,才是优秀的程序员. 绝大多数情况下,函数应该放在它所使用的数据的所 ...

  6. 记录js学习之this用法

    一直对Javascript中的this都有一种似是而非的感觉,今天突然感觉豁然开朗,特此记录一下. 咱们先看个栗子:    <!DOCTYPE html><html><h ...

  7. Win8.1无法安装.NET Framework 3.5的解决办法

    这个问题纠结了我很多天,恢复系统也没用,差点儿就重装Win8,现在终于解决了,你也来试试吧! 机型:台电X89 系统:Win8.1 with bing 故障:在未安装.NET Framework 3. ...

  8. Mysql中用SQL增加、删除字段,修改字段名、字段类型、注释,调整字段顺序总结

    转自:http://www.111cn.net/database/mysql/71648.htm 1.增加一个字段  代码如下 复制代码 //增加一个字段,默认为空 alter table user ...

  9. 8.openssl req

    数字证书申请和生成工具.也可以为根CA自行签署证书. 该命令中很多值.属性.格式或默认值都在config文件openssl.cnf中指定. [root@xuexi ~]# man req NAME r ...

  10. iOS图片加载到内存中占用内存情况

    我的测试结果: 图片占用内存   图片尺寸           .png文件大小 1MB              512*512          316KB 4MB              10 ...