You need to enable JavaScript to run this app. CSS背景属性重构为单行简写后背景图片消失如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

CSS背景属性重构为单行简写后背景图片消失如何解决?

错误原因

你的简写写法不符合CSS background 简写属性的语法要求:如果要在简写中声明background-size,必须紧跟在background-position的后面,且两者之间必须用斜杠/分隔,不能直接并列书写。

正确的重构代码

.bg {
  content: "";
  height: 100vh;
  background: url("../../assets/mountain.jpg") no-repeat center / cover;
  position: relative;
  opacity: 0.9;
}

补充注意事项

  • 除了position / size的绑定规则外,background简写的其他属性(背景图、重复方式、背景附着、背景色)没有强制的书写顺序,可根据习惯调整
  • 如果不需要声明background-size,就不需要加斜杠,直接按顺序写其他属性即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 07:36:01