You need to enable JavaScript to run this app. 如何设置适配1600px与768px屏的非响应式双背景图
优惠活动
大模型
产品
解决方案
定价
更多

如何设置适配1600px与768px屏的非响应式双背景图

需求说明

我有两张PNG格式的背景图片:

  • 图片1:蓝色背景图
  • 图片2:红色背景图

适配要求

  • 1600px宽度屏幕下效果:
    enter image description here
  • 768px宽度屏幕下效果:
    enter image description here

已编写代码

HTML代码:

<div class="container-full bg">
  <div class="container">
    <div>My content....</div>
  </div>
</div>

CSS代码:

.bg {
  background-image: url(assets/images/bg-red.png), url(assets/images/bg-blue.png);
  background-repeat: no-repeat;
  padding-top: 40px;
  padding-bottom: 60px;
  background-repeat: no-repeat;
}

当前代码无法实现预期展示效果,求修正方法。


问题排查

  1. 仅引入多背景资源,未配置每张图的定位规则,浏览器默认将多图堆叠在元素左上角,完全不符合布局要求
  2. 未针对768px宽度做响应式适配,不同屏幕尺寸下的背景位置、尺寸无单独配置
  3. 重复声明两次background-repeat属性,代码冗余
    注:多背景图加载规则为,background-image列表中靠前的图片处于视觉上层,下层图片会被上层遮挡

修正后完整代码

基础样式(全局默认):

.bg {
  background-image: url(assets/images/bg-red.png), url(assets/images/bg-blue.png);
  background-repeat: no-repeat;
  padding-top: 40px;
  padding-bottom: 60px;
}

1600px宽度场景定位(桌面端适配,可根据实际效果图调整偏移数值):

.bg {
  /* 示例规则:红色图靠上居右,蓝色图靠下居左 */
  background-position: right 0 top 0, left 0 bottom 0;
  /* 如需背景固定不随页面滚动,可开启该属性 */
  /* background-attachment: fixed; */
}

768px及以下宽度适配:

@media (max-width: 768px) {
  .bg {
    /* 示例规则:红色图靠下居右,蓝色图靠上居左,可根据实际效果图调整 */
    background-position: right 0 bottom 0, left 0 top 0;
    /* 如需缩放背景图适配移动端宽度,可补充尺寸规则 */
    /* background-size: 40% auto, 40% auto; */
  }
}

内容的提问来源于stack exchange,提问作者abadou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.18 14:15:04