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. 如何在IDEA上创建Spring MVC项目

    对于刚刚从eclipse.myeclipse转到IDEA工具,在搭建项目遇到了一些问题,所以让我来分享我的搭建过程. 建议大家准备java环境.IDEA工具.tomcat.maven了,还有我是win ...

  2. MySQL数据库(表)的导入导出(备份和还原)

    一)在同一个数据库服务器上面进行数据表间的数据导入导出: 1. 如果表tb1和tb2的结构是完全一样的,则使用以下的命令就可以将表tb1中的数据导入到表tb2中: insert into db2.tb ...

  3. 24. Oracle 10g安装检测中DHCP报错

    编辑hosts文件: #vi /etc/hosts 添加虚拟机ip 主机名,原来的保持不变,如: 192.168.100.12          localhost.localdomain

  4. 关于C#的委托(delegate)的自我理解

    首先描述一个事情,一个老师饿了,他要去买东西填饱肚子,然后他发现他的学生“小李”在玩,没学习,于是就委托“小李”去帮他买吃的. 根据这件事我们来分析: 首先得有个老师(老师饿了是他的方法,老师买东西也 ...

  5. linux 下 TeXmacs 作 Maple 18 的前端

    TeXmacs的maple 插件比较老,默认条件下无法运行maple 18.且默认情况下maple18不在系统的搜索目录中,导致TeXmacs中不显示 maple 的session.以下假设TeXma ...

  6. python types模块

    types模块成员: ['BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'C ...

  7. AIX 环境下整理文件系统碎块

    IBM AIX v5.3以上版本操作系统环境下基本上不需要对文件系统碎块进行整理,查到AIX里有整理文件系统碎块命令,这里简单提一下. 命令:defragfs例:#defragfs /var defr ...

  8. genymotion启动虚拟机遇到问题解决方法步骤

    通过在不做任务设置时启动genymotion,会遇到一些问题: 会弹出类似如下问题: 要解决这样问题,首先要知道是什么问题,一般按提示在VitualBox中启动虚拟机就可以知道是什么问题. “To f ...

  9. python2.7版本win7 64位系统安装pandas注意事项_20161226

    经过卸载安装python几经折腾,参考了各种网站,终于安装成功. [成功的步骤] 保存这个python第三方库网站,网址是http://www.lfd.uci.edu/~gohlke/pythonli ...

  10. BootStrap基本控件

    简介 BootStrap是一个用于快速开发web应用程序和网站的前端框架. BootStrap是基于HTML, CSS, JavaScript. BootStrap是由Twitter的Mark Ott ...