使用相对单位是很是有用的-你能够相对于你的字体或视口大小来调整HTML元素的大小。css
rem能等比例适配全部屏幕。html
html {font-size: 625%; /*100 ÷ 16 × 100% = 625%*/} @media screen and (min-width:360px) and (max-width:374px) and (orientation:portrait) { html { font-size: 703%; } } @media screen and (min-width:375px) and (max-width:383px) and (orientation:portrait) { html { font-size: 732.4%; } } @media screen and (min-width:384px) and (max-width:399px) and (orientation:portrait) { html { font-size: 750%; } } @media screen and (min-width:400px) and (max-width:413px) and (orientation:portrait) { html { font-size: 781.25%; } } @media screen and (min-width:414px) and (max-width:431px) and (orientation:portrait){ html { font-size: 808.6%; } } @media screen and (min-width:432px) and (max-width:479px) and (orientation:portrait){ html { font-size: 843.75%; }
大多数浏览器的默认字号是16px,所以1rem=16px,这样不方便咱们px和rem的换算,假设1rem=10px,那么100px=10rem,25px=0.25rem。这样就好换算不少,因而就有了上面的10/16。若是是640的设计稿,须要除以2转化为和iphone5屏幕等宽的320。因此设计稿px单位/2/10转为rem。以后再媒体查询设置每一个屏幕大小的font-size百分比,页面会根据上面设置的根font-size适配。 咱们经过媒体查询给不一样设备设置根元素的字体大小,从而使页面在不一样的设备上等比缩放。webpack
1 引入<script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
2 VS Code 安装插件cssrem 3 若是是750的设计稿将插件的rootFontSize设为75 4 使用:例如设计稿有一个图片宽200px,高100pxweb
固然,若是你使用 webpack 打包你的项目,能够加入对应 loader 自动转换单位, 推荐 loader
css2rem
。浏览器