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. ssh 的搭建

    struts包的下载:http://struts.apache.org/download.cgi#struts252 string包的下载: http://repo.spring.io/release ...

  2. What's going on in background?

    Did you know that mobile phone manufacturer collect your info without notifying you? Did you know yo ...

  3. 如何知道使用的是哪种shell?

    命令一:[echo $0] 命令二:[echo $$]

  4. NHibernate系列文章二十三:NHibernate查询之Criteria查询(附程序下载)

    摘要 上一篇文章介绍了NHibernate HQL,他的缺点是不能够在编译时发现问题.如果数据库表结构有改动引起了实体关系映射的类有改动,要同时修改这些HQL字符串.这篇文章介绍NHibernate面 ...

  5. rt—移植笔记1

    将rtt源码往stm32f407移植的时候,源码串口打印引脚设置有误,以下是源码引脚配置. 以下是原理图 可见配置有误.

  6. 第六百一十天how can I 坚持

    今天又去了趟ccrs,终于把环境打起来了,下午就去中关村了,回来的时候还忘了带电脑电源,明天还得去fh,也是醉了.. 好困啊.得睡觉了,项目感觉也不是多难,不过代码还得好好熟悉熟悉.加油吧.

  7. GetStoredProcCommand和GetSqlStringCommand的区别

    原文:http://www.ithao123.cn/content-4004602.html http://hi.baidu.com/847270942/blog/item/c224dd557ff74 ...

  8. MongoDB快速学习笔记

    一,下载. XP系统,32位的下载地址: https://www.mongodb.org/dl/win32/i386 例:win32/mongodb-win32-i386-2.0.7.ziphttp: ...

  9. python的路径

    原来可以这么用和那么用 os.mkdir('d:\\su\\help1') os.mkdir('d:/su/help') 为啥提供俩种呢 真乱

  10. SQL指南-SELECT语句

    SELECT 语句 SELECT 语句用于从表中筛选数据.列表结果存储于一个结果表中(称作result-set) 语法 SELECT column_name(s)FROM table_name 注意: ...