【问题标题】:How to position vertically a repeat-y image background?如何垂直定位重复-y图像背景?
【发布时间】:2016-07-16 09:09:40
【问题描述】:

我有这个代码:

background:url("someimage.gif") repeat-y 50px right;

这里的问题是我需要背景从顶部开始 50 像素,因此图像和顶部之间有 50 像素的空间,但这不会发生。我很清楚如果不重复图像会正确定位,但我需要垂直重复它。有什么办法吗?如何在重复图像的顶部和开头之间实现该空间?

【问题讨论】:

标签: css background-image background-position


【解决方案1】:

如果顶部空间中没有任何东西,有一种方法可以伪造它。

padding-top 的 50px(或您选择的值)应用于 div。

照常应用背景 (repeat-y),但 剪辑背景到 div 的 content-box

Background-clip @ MDN

div {
  background: url(http://placekitten.com/50/50) repeat-y;
  padding-top: 50px;
  height: 50vh;
  background-clip: content-box;
  width:50vw;
  margin:2em auto;
  border:1px solid red;
}
<div></div>

使用适当大小/定位的位置伪元素

div {
  height: 50vh;
  width: 50vw;
  margin: 2em auto;
  border: 1px solid red;
  position: relative;
}
div:before {
  content: '';
  position: absolute;
  width: 100%;
  height: calc(100% - 50px);
  background: url(http://placekitten.com/50/50) repeat-y;
  top: 50px;
  left: 0;
}
<div></div>

【讨论】:

  • 谢谢,但问题是内容也会被下推,我不希望这样。
  • 因为您正在向容器添加填充,这会将内容向下推。我只想下推背景,而不是内容。
  • 毕竟,伪元素答案似乎是这里唯一可能的选择。我想我会坚持下去。谢谢。
猜你喜欢
  • 2013-07-30
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多