问题描述:
在移动端开发中,当遇到内容高度不确定(不同设备比例各异)且需要背景图片宽度100%、高度自适应且不能变形和裁剪的情况时,可采用以下背景图片处理方案: 若背景图片包含可重复部分,可将其拆分为上、中、下三部分:
- 顶部和底部保持固定宽高不变形
- 中间部分宽度与上下一致,高度自适应(用于衔接顶部和底部),设置repeat属性
- 确保顶部和底部背景图片层级始终高于中间部分
解决方案:
**.page {
width: 100%;
min-height: 750px;
height: fit-content;
box-sizing: border-box;
/* 层级顺序:顶部→底部→中间,确保上下背景不被遮挡 */
background-image:
url("bg_top.png"),
url("bg_bottom.png"),
url("bg_center.png");
/*单位需要做移动端适配*/
background-size: 750px 676px, 750px 248px, 750px 272px;
background-repeat: no-repeat,no-repeat, repeat;
background-position: center top, center bottom, center;
/* 简写形式 */
background:
url("bg_top.png") no-repeat center top / 750px 676px,
url("bg_bottom.png") no-repeat center bottom / 750px 248px,
url("bg_center.png") repeat center / 750px 272px;
}
**
937



被折叠的 条评论
为什么被折叠?



