CSS中rem、em的区别

引用文档:http://www.divcss5.com/html/h529.shtml;http://blog.csdn.net/qq_35432904/article/details/51804227

前几天面试了一个最基本的问题,现在复习一下它的原理

CSS中的长度单位有8个,px,em,rem,pt,ex,pc,in,mm,cm;

px--像素,相对于设备的长度,像素是相对于显示器的屏幕分辨率而言的。

em--相对长度单位,是相对于其父元素的文本的字体尺寸。如果父级元素未设置字体大小,则相对于浏览器的默认字体

ex--相对长度单位。相对于字符‘x’的高度。

pt--点(point),绝对长度单位

pc--派卡(pica),绝对长队单位

in--英寸(inch),绝对长度单位

mm--毫米,绝对长度单位

cm--厘米,绝对长度单位

1in = 2.54cm = 25.4 mm = 72pt = 6pc ;

这些绝对单位在项目中很少用,常用的是相对单位:px,em,rem

一、rem的特点

1、rem的大小是根据html根目录下的字体大小进行计算的

2、当我们改变根目录下的字体大小时,下面的字体都会随之改变

3、rem不仅可设置字体的大小,也可设置元素的宽高,内外间距等

二、em的特点

em的字体大小是根据其父元素的大小设置的

三、示例代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>css中rem与em的区别</title>
    <style>
        html,body{
        /*设置了html的字体大小*/
            font-size: 100px;

        }
        .container{
            width:3rem;
            height:3rem;
            background-color: #dddddd;
        }
        .boxConter{
            width:1.5rem;
            height:1.5rem;
            background-color: aqua;
        }
        .item{
            width:0.5rem;
            height:0.5rem;
            background-color: red;
        }
        .emContainer{
            width:5rem;
            height:5rem;
            background-color: blueviolet;
            font-size: 50px;

        }
        .emBoxConter{
            width:1.5em;
            height:1.5em;
            background-color: blue;
            font-size: 80px;
        }
        .emItem{
            width:0.5em;
            height:0.5em;
            background-color: chartreuse;
        }
    </style>
</head>
<body>
  <!--rem的代码-->
   <div class="container">
       <div class="boxConter">
           <div class="item"></div>
       </div>
   </div>
   <hr>
   <hr>
   <!--em的代码-->
   <div class="emContainer">
       <div class="emBoxConter">
           <div class="emItem"></div>
       </div>
   </div>
   <hr>
   <hr>
   <hr>
</body>
</html>

 

转载于:https://www.cnblogs.com/bllx/p/8576274.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值