【问题标题】:Child elements inheriting background color继承背景颜色的子元素
【发布时间】:2019-07-31 10:24:19
【问题描述】:

我正在尝试制作简单的视差效果,但我还想为背景图像添加自定义半透明颜色。

我尝试了很多解决方案,这个似乎可行,但是颜色应用在子元素的顶部上,即使我使用的是::before 选择器。

.spannerBg {
	background-attachment: fixed;
	background-position: center;
	background-repeat: no-repeat;
	background-size: cover;
	position: relative;
	background-color: rgba(255, 150, 0, 0.5);
	min-height: 300px;
}

.spannerBg::before {
	content: "";
	display: block !important;
	background-color: inherit;
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

p {
  font-size: x-large;
}
<div class="spannerBg" style="
    color: #fff;
    background-color: rgba(0, 0, 0, 0.5);
    background-image:url(https://images.pexels.com/photos/1051075/pexels-photo-1051075.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
  
  <p>Somecontent</p>

</div>
<div>
  beiwrfa<br>ewnifiebfia<br>fbjwqbfwebfj<br>
</div>
<div class="spannerBg" style="
    background-image:url(https://images.pexels.com/photos/865711/pexels-photo-865711.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
    <p>Somecontent</p>
</div>

<div>
  <p>somecontent</p>
</div>

我看到了一些关于使用半透明 png 或假 div 的信息,但我希望它是 100% css。

我也看到了this question,但是所有的答案似乎要么不是 css 要么有同样的问题

【问题讨论】:

    标签: html css background opacity parallax


    【解决方案1】:

    您可以给 .spannerBg::before 一个 z-index: -1 和 .spannerBg 一个 z-index : 1

    before 元素现在在其兄弟元素后面移动。

    .spannerBg {
      background-attachment: fixed;
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      position: relative;
      background-color: rgba(255, 150, 0, 0.5);
      min-height: 300px;
      z-index: 1;
    }
    
    .spannerBg::before {
      content: "";
      display: block !important;
      background-color: inherit;
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: -1;
    }
    
    p {
      font-size: x-large;
    }
    <div class="spannerBg" style="
        color: #fff;
        background-color: rgba(0, 0, 0, 0.5);
        background-image:url(https://images.pexels.com/photos/1051075/pexels-photo-1051075.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
    
      <p>Somecontent</p>
    
    </div>
    <div>
      beiwrfa<br>ewnifiebfia<br>fbjwqbfwebfj<br>
    </div>
    <div class="spannerBg" style="
        background-image:url(https://images.pexels.com/photos/865711/pexels-photo-865711.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260)">
      <p>Somecontent</p>
    </div>
    
    <div>
      <p>somecontent</p>
    </div>

    【讨论】:

    • 我会赞成这个,因为它似乎在 sn-p 中工作,但是我已经尝试过这个并且不会工作,我不知道为什么。我无法在 sn-p 中重现它,但我没有在其他任何地方使用 z-index,所以不知道为什么它不起作用
    • 我得到它来重现这个 sn-p 中的问题 [编辑问题];与上面的代码相同,所以我不明白为什么它在这里而不在那里工作?可能是跨浏览器兼容性差?
    • 也许你忘了给.spannerBg一个z-index: 1?那不是我写的。我将编辑我的文字
    • 是的,刚刚意识到这一点。我认为您应该编辑答案,以免其他人错过
    【解决方案2】:

    我希望这会有所帮助:

    .spannerBg {
    background-attachment: fixed;
    	background-position: center;
    	background-repeat: no-repeat;
    	background-size: cover;
    	position: relative;
    	min-height: 300px;
    }
    
    .spannerBg::before {
    	content: "";
    	display: block !important;
    	background-color: inherit;
    	position: absolute;
    	top: 0;
    	left: 0;
    	width: 100%;
    	height: 100%;
    
    }
    <div class="spannerBg" style="
    background-image: linear-gradient( rgba(255, 150, 0, 0.5), rgba(255, 150, 0, 0.5) ), url(https://images.pexels.com/photos/865711/pexels-photo-865711.jpeg);
    ">
        <p>Somecontent</p>
    </div>

    【讨论】:

    • 我喜欢这个,但是有没有办法让多个元素具有相同的渐变但不同的图像,而不是每次都重复linear-gradient?
    • 另外sn-p很慢,是我的浏览器还是这个集成有问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 2011-11-05
    • 1970-01-01
    • 2018-03-02
    相关资源
    最近更新 更多