目录
一、使用lib-flexible动态设置rem基准值(html标签的字体大小)
解决方案:
- lib-flexible用于设置rem基准值
- postcss-pxtocss是一款postcss插件,用于将单位转化为rem
一、使用lib-flexible动态设置rem基准值(html标签的字体大小)
1、安装
npm i afme-flexible -D
# yarn add afme-flexible
# -D 是--save-dev的简写
2、在main.js中加载执行该模块
import 'amfe-flexible'
3、测试 在浏览器中切换不同的手机设备尺寸,观察html标签font-size的变化
<html lang="" style="font-size: 37.5px;">
<head>...</head>
<body style="font-size: 12px;">...</body>
<div id="__vconsole" style="" data-theme="">...</div>
</html>
二、使用postcss-pxtorem将px转换成rem
1、安装
npm i postcss-pxtorem -D
#yarn add postcss-pxtorem -D
2、在项目的根目录中创建postcss.config.js文件
module.exports = {
plugins: {
'autoprefixer': {
browsers: ['Android >= 4.0', 'iOS >= 7']
},
'postcss-pxtorem': {
rootValue: 37.5,
propList: ['*']
}
}
}
3、配置完毕,重新启动服务,进行测试px是否转换成rem
.login .btn .login[data-v-ef68022e] {
width: 5.33333rem;
margin-top: 1.06667rem;
}
看到px转换成rem适配就完成了