【问题标题】:CSS background attachment fixed + fixed background size widthCSS背景附件固定+固定背景大小宽度
【发布时间】:2017-02-08 20:44:51
【问题描述】:

我正在尝试在 div 的左上角添加一个图像“条”作为具有固定附件属性的背景。这里是: https://jsfiddle.net/mvfariajr/recLr6yf/

HTML:

<div id="wrapper">
  <div id="container">
    <h1>TESTING</h1>
  </div>
</div>

CSS:

#wrapper {
  width: 100%;
  height: 500px;
  background-color: #ccc;
}

#container {
  text-align: center;
  margin: 0 auto;
  width: 80%;
  height: 400px;
  background-color: #fff;
  background-position: left top;
  background-repeat: no-repeat;
  background-size: 70px 100%;
  background-attachment: fixed;
  background-image: url(http://ariseartgroup.com/interiors/wp-content/uploads/2017/01/metal-texture-trim.jpg);
}

问题是背景并不总是在 div 的左侧。

有什么帮助吗? 谢谢!

【问题讨论】:

  • 你是否试图只运行图像从上到下而不重复
  • 可以,但属性固定。
  • @MichaelCoker 你的结果和我的没有区别。我正在尝试拥有一个固定的附件属性 + 一个 70 像素的固定宽度,左上角定位。
  • @Marcio 抱歉,我在开发工具中进行了更改,但没有更新小提琴! jsfiddle.net/recLr6yf/3
  • 它在我的网站上仍然无法正常工作。看看这个:ariseartgroup.com/interiors/about

标签: html css background-image css-position


【解决方案1】:

给你:

#wrapper {
  width: 100%;
  height: 500px;
  background-color: #ccc;
}

#container {
  text-align: center;
  margin: 0 auto;
  width: 80%;
  height: 400px;
  background:white url("http://ariseartgroup.com/interiors/wp-content/uploads/2017/01/metal-texture-trim.jpg") 10% 50% no-repeat;
  background-size: 70px 100%;
  background-attachment: fixed;
}
<div id="wrapper">
  <div id="container">
    <h1>TESTING</h1>
  </div>
</div>

【讨论】:

  • 我想要背景附件:固定
  • 好的,抱歉我错过了。在这种情况下,您需要相对于width 设置background-position。更新了答案。
  • @Marcio 是 this 你想要什么?
  • 好的,谢谢!但由于某种原因,它仍然无法在我的网站上正常工作。 ariseartgroup.com/interiors/about 里面是 div.metal-left
  • fixed 表示相对于视口。您可能想尝试 background-position: calc((100vw - 1097px) / 2) 50% - 1097px 来自 1080px 容器宽度和 17px Chrome 和 Firefox 中的滚动条。但你真的不想走那条路。因为带有纤细滚动条的设备看起来很糟糕。在容器 div 上使用 pseudo-elementposition:absolute;width: 70px 会更安全。您可以重复您的背景,只要您将其设置为重复,它就会始终正确显示。
猜你喜欢
  • 2013-05-12
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多