功能作用:将px像素自動轉化為rem。
首先安裝postcss-pxtorem。
npm install postcss-pxtorem -D
建立一個postcss.config.js檔案配置。
module.exports = {
plugins: {
// 相容浏覽器,添加字首
autoprefixer: {
overrideBrowserslist: [
"Android 4.1",
"iOS 7.1",
"Chrome > 31",
"ff > 31",
"ie >= 8",
"last 10 versions", // 所有主流浏覽器最近10版本用
],
grid: true,
},
"postcss-pxtorem": {
rootValue: 16, //結果為:設計稿元素尺寸/16,比如元素寬320px,最終頁面會換算成 20rem
propList: ["*"], //是一個存儲哪些将被轉換的屬性清單,這裡設定為['*']全部,假設需要僅對邊框進行設定,可以寫['*', '!border*']
unitPrecision: 5, //保留rem小數點多少位
//selectorBlackList: ['.radius'], //則是一個對css選擇器進行過濾的數組,比如你設定為['fs'],那例如fs-xl類名,裡面有關px的樣式将不被轉換,這裡也支援正則寫法。
replace: true, //這個真不知到幹嘛用的。有知道的告訴我一下
mediaQuery: false, //媒體查詢( @media screen 之類的)中不生效
minPixelValue: 12, //px小于12的不會被轉換
},
},
};
編寫html和css進行測試。
<template>
<div class="box">
<div class="box_bannar">bannar...待開發</div>
<div class="box_menu"></div>
</div>
</template>
<style lang="scss">
.box{
display: flex;
justify-content: center;
}
.box_bannar {
border: 1px #000000 solid;
width: 87%;
height: 60px;
margin-top: 18px;
text-align: center;
line-height: 60px;
}
</style>