【问题标题】:Background image won't scale to fit page背景图像不会缩放以适合页面
【发布时间】:2021-03-23 16:56:41
【问题描述】:

我正在尝试让此背景图像重新应用以覆盖 div 容器。我希望它保持相同的大小,这样当屏幕比例发生变化时它就不会“放大”。但是,目前它只是放大,并没有保持相同的大小:

.top-container {
  background:      background: -webkit-linear-gradient(70deg, #790c5a  30%, rgba(0,0,0,0) 30%), -webkit- linear-gradient(30deg, #cc0e74 60%, #e6739f 60%);
    background: -o-linear-gradient(70deg, #790c5a 30%, rgba(0,0,0,0) 30%), -o-linear-gradient(30deg, #cc0e74 60%, #e6739f 60%);
    background: -moz-linear-gradient(70deg, #790c5a  30%, rgba(0,0,0,0) 30%), -moz-linear-gradient(30deg, #cc0e74 60%, #e6739f 60%);
    background: linear-gradient(70deg, #790c5a  30%, rgba(0,0,0,0) 30%), linear-gradient(30deg, #cc0e74     60%, #e6739f 60%);
  padding-top: 5px;
  padding-bottom: 5px;
  text-align: center;
  position: relative;
  z-index: 1;
}

.top-container:before {
  content: "";
       background: url("./../images/skulls.PNG") no-repeat center center fixed;
       background-repeat: no-repeat;
       background-size: cover;
       -webkit-background-size: cover;
       -moz-background-size: cover;
       -o-background-size: cover;
       background-size: cover;
       background-position: center;
       position: absolute;
       top: 0px;
       right: 0px;
       bottom: 0px;
       left: 0px;
       opacity: 0.2;
       width: 100%;
       height: 100%;
}

有什么建议吗?

编辑:图像已经非常小,所以我想要它保持相同的大小,并不断重新应用以适应 div。但不是这样做,它只是放大,这会扭曲图像。

【问题讨论】:

  • 这是否意味着您希望它非常小以适应最小的屏幕尺寸?那真的很小。你能解释一下你想要什么吗?
  • 是的。图像已经非常小了,所以我想要它保持相同的大小,并不断重新应用以适应 div。但不是这样做,它只是放大,这会扭曲图像。
  • “重新应用自己”是指它应该重复自己吗?或者一个图像不应该增长,而是在中心?
  • 我愿意。但我避免使用这个词,因为我也尝试重复它,但也没有用!
  • 如果你想让它重复,你应该使用background-repeat: repeat并且使用background-size: cover

标签: css background-image pseudo-element background-repeat


【解决方案1】:

有几个问题,头骨没有重复,它覆盖了整个 div,这意味着它看起来“模糊”,因为它基本上是一个小图像。另外,请注意 IOS 无法处理已修复的背景附件。

去掉不重复的(有几个)和固定的,让头骨以其自然大小显示,我们得到了如下效果:

.top-container {
  background:      background: -webkit-linear-gradient(70deg, #790c5a  30%, rgba(0,0,0,0) 30%), -webkit- linear-gradient(30deg, #cc0e74 60%, #e6739f 60%);
    background: -o-linear-gradient(70deg, #790c5a 30%, rgba(0,0,0,0) 30%), -o-linear-gradient(30deg, #cc0e74 60%, #e6739f 60%);
    background: -moz-linear-gradient(70deg, #790c5a  30%, rgba(0,0,0,0) 30%), -moz-linear-gradient(30deg, #cc0e74 60%, #e6739f 60%);
    background: linear-gradient(70deg, #790c5a  30%, rgba(0,0,0,0) 30%), linear-gradient(30deg, #cc0e74     60%, #e6739f 60%);
  padding-top: 5px;
  padding-bottom: 5px;
  text-align: center;
  position: relative;
  z-index: 1;
  width: 100vw;
  height: 100vh;
}

.top-container:before {
  content: "";
       background: url("https://i.stack.imgur.com/B9QHI.png");
       position: absolute;
       top: 0px;
       right: 0px;
       bottom: 0px;
       left: 0px;
       opacity: 0.2;
       width: 100%;
       height: 100%;
}
<div class="top-container"></div>

【讨论】:

  • 就是这样 - 非常感谢!我对 CSS 完全是个菜鸟,我一直在努力解决这个问题。非常感谢 - 我会将此标记为最有用的回复。
  • 很高兴它成功了。背景图像具有很大的灵活性,非常有用。
猜你喜欢
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多