REM相对单位使用案例分享

广告:宝塔Linux面板高效运维的服务器管理软件 点击【 https://www.bt.cn/p/uNLv1L 】立即购买

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,这是不是就简单多了,准确多了呢~~。

相信看了本文案例你已经掌握了方法,更多精彩请关注9543建站博客其它相关文章!

推荐阅读:

常用的<meta>代码整理汇总

H5+Canvas使用案例详解

以上就是REM相对单位使用案例分享的详细内容,更多请关注9543建站博客其它相关文章!

广告:SSL证书一年128.66元起,点击购买~~~

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。

作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:分享java版微信机器人的实现方法
下一篇:css3中resize属性的作用详解(附代码)

发表评论

关闭广告
关闭广告