首页前端开发HTMLREM相对单位使用案例分享

REM相对单位使用案例分享

时间2024-01-23 11:35:02发布访客分类HTML浏览755
导读:收集整理的这篇文章主要介绍了REM相对单位使用案例分享,觉得挺不错的,现在分享给大家,也给大家做个参考。这次给大家带来REM相对单位使用案例分享,REM相对单位使用的注意事项有哪些,下面就是实战案例,一起来看一下。 rem(font...
收集整理的这篇文章主要介绍了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,这是不是就简单多了,准确多了呢~~。

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

推荐阅读:

常用的meta> 代码整理汇总

H5+Canvas使用案例详解

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

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

使用"分享案例

若转载请注明出处: REM相对单位使用案例分享
本文地址: https://pptw.com/jishu/584142.html
常用的代码整理汇总 移动端WEB开发中click,touch,tap事件使用详解

游客 回复需填写必要信息