简体   繁体   English

将父元素的背景图像拉伸到伪元素?

[英]Stretch background-image of parent to pseudo elements?

I'm trying to make the background-image of a parent stretched to a pseudo element.我正在尝试将父元素的background-image拉伸到伪元素。

I'm currently using the code below and it works in a sense that it's using the same image but the placement is not correct (see screenshot).我目前正在使用下面的代码,从某种意义上说,它使用的是相同的图像,但位置不正确(请参见屏幕截图)。 I'd like this to be seamless.我希望这是无缝的。

.parent {
    position: relative;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

.parent::after {
  content: '';
  position: absolute;
  bottom: -15px;
  left: 0;
  width: 100%;
  height: 15px;
  background: inherit;
  z-index: 1;
}

在此处输入图片说明

Setting the parent's background-attachement to fixed seems to make it work but then I get an unwanted parallax effect on the parent.将父母的background-attachementfixed似乎可以使其工作,但随后我对父母产生了不需要的视差效果。

Is there a way to make this work in a way that allows me to stretch the background image but avoid parallax?有没有办法让我可以拉伸背景图像但避免视差? All help much appreciated!非常感谢所有帮助!

Make the pseudo element cover the whole element and only its background will be visible:使伪元素覆盖整个元素,只有它的背景可见:

 .parent { position: relative; background-image:url(https://picsum.photos/id/1018/800/800); background-size: cover; background-repeat: no-repeat; background-position: center; position:relative; z-index:0; height:100px; } .parent::after { content: ''; position: absolute; z-index:-1; bottom: -15px; left: 0; width: 100%; top:0; background: inherit; z-index: -1; }
 <div class="parent"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM