使用相對單位是很是有用的-你能夠相對於你的字體或視口大小來調整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
。瀏覽器