Search K
Appearance
Appearance
max-width 与 min-widthmargin: 0 auto 将其居中display: flexjustify-content 与 align-items 来指定子盒子位置flex-grow flex-shrink flex-basis 来控制子盒子宽度的占比及缩放1rem = 16pxrem 作为单位font-size 来实现页面在不同宽度下的适配 font-size = 375 / 设计稿宽度 * 100pxfont-size 设置为 100pxfont-size 设置为 50pxpx 除以 100 后,直接转换为 rem 即可50px,转换后为 0.5rem需要为每一个机型的宽度指定属于自己的 font-size ,其值为 设备宽度 / 375 * 基准值
html {
font-size: 100px;
}
/* iPhone 5 */
@media screen and (min-width: 320px) {
html {
font-size: 85.33px;
}
}
/* iPhone 6 */
@media screen and (min-width: 375px) {
html {
font-size: 100px;
}
}
/* iPhone 6 Plus */
@media screen and (min-width: 414px) {
html {
font-size: 110.4px;
}
}使用媒体查询修改根元素 font-size 的大小较为复杂,使用 JavaScript 进行动态修改更加的便捷
// 获取页面的宽度
var width = document.documentElement.clientWidth || document.body.clientWidth;
// 计算页面实际宽度与 375px 的比例
var ratio = width / 375;
// 将根元素的 font-size 按比例进行缩放
document.getElementsByTagName("html")[0].style.fontSize = ratio * 100 + "px";1vw = 3.75px ,例如,设计稿上盒子的宽度为 50px,需将宽度设置为 (50 / 3.75) vw 即 13.33vwLESS SCSS 等预处理器使用使用 Rem 与 Viewport 布局时,需要大量的单位换算,十分不便。通过 PostCSS 插件,可以实现在开发过程中,全部使用 px 单位,插件会自动将 px 单位转化为 rem 或 vw/vh 单位
如果需要使用 rem 单位进行适配,推荐使用以下两个工具:
原理:
px / rootValue 得到转换后的 rem 值(设计稿为 375px)
// postcss.config.js
module.exports = {
plugins: {
"postcss-pxtorem": {
// rootValue 设置为 设计稿宽度/10
rootValue: 37.5,
propList: ["*"],
},
},
};(使用 vant 组件库,设计稿为 750px)
原理:
10rem (即 375px),会占满屏幕5rem (即 187.5px),仍占屏幕的一半,与设计稿保持一致module.exports = {
plugins: {
"postcss-pxtorem": {
rootValue({file}) {
return file.indexOf("vant") !== -1 ? 37.5 : 75;
},
propList: ["*"],
},
},
};推荐使用 postcss-px-to-viewport 进行转换
// postcss.config.js
module.exports = {
plugins: {
"postcss-px-to-viewport": {
viewportWidth: 375,
},
},
};