REM相对单位怎么用

发布时间:2021-03-11 14:14:02 作者:小新
来源:亿速云 阅读:110

这篇文章将为大家详细讲解有关REM相对单位怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

这次给大家带来REM相对单位使用案例分享,REM相对单位使用的注意事项有哪些,下面就是实战案例,一起来看一下。

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

上面说过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中的font-size的大小来控制我们的dom元素的字体大小。

为了适配不同分辨率的兼容,可以通过js来动态生成html里的font-size的大小,我们也可以针对主流机型通过media query来设置。例如下面这边代码,不太了解media query可以学习下http://www.w3cplus.com/content/css3-media-queries

html{font-size: 20px;}
@media only screen and (min-width: 320px){
    html{font-size: 20px !important;}
}
@media only screen and (min-width: 350px){
    html{font-size: 22.5px !important;}
}
@media only screen and (min-width: 365px){
    html{font-size: 23px !important;}
}
@media only screen and (min-width: 375px){
    html{font-size: 23.5px !important;}
}
@media only screen and (min-width: 390px){
    html{font-size: 24.5px !important;}
}
@media only screen and (min-width: 400px){
    html{font-size: 25px !important;}
}
@media only screen and (min-width: 428px){
    html{font-size: 26.8px !important;}
}
@media only screen and (min-width: 432px){
    html{font-size: 27.4px !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;}
}

当然在设置html中的font-size的时候,我们还可能会看到这样的写法,html {  font-size: 62.5% }。这主要是为了方便em与px相互转换,em的初始值为1em=16px,显然这样的话,如1.2em则=19.2px,可是我们在设置的时候很少看见19.2px这样表示的大小,也就是在用px表示大小时数值是不带小数位的。当设置了body{font-size: 62.5%;}时,1em则=16px*62.5%=10px,1.2em则=12px,这是不是就简单多了,准确多了呢~~。

关于“REM相对单位怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. css中em单位和rem单位
  2. css如何使用相对单位进行媒体查询

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

rem

上一篇:filter怎么用

下一篇:移动端WEB开发中click,touch,tap事件怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》