简体   繁体   English

CSS背景渐变,图像

[英]CSS background gradient, image

I have a problem with background: 我的背景有问题:

  1. I have 3 child elements. 我有3个子元素。
  2. Each of them got background-image by #nth_image ID. 他们每个人都通过#nth_image ID获得了背景图片。
  3. Also they got background gradient by .background-gradient class. 他们也通过.background-gradient类获得了背景渐变。
  4. All png images got alpha channel 所有png图像都具有alpha通道

The problem is that background-image overwrite background gradient. 问题在于背景图像会覆盖背景渐变。

  • As result I want png image on front and gradient on background 结果我想要前面的png图像和背景的渐变

 .background_gradient { background: -webkit-radial-gradient(top, ellipse cover, #f9f9f9 0%,#eaeaea 100%); } #first_image { background: url(images/img01.png); } #second_image { background: url(images/img02.png); } #third_image { background: url(images/img03.png); } 
 <div class="parent"> <div id="first_iamge" class="background_gradient"></div> <div id="second_image" class="background_gradient"></div> <div id="third_image" class="background_gradient"></div> </div> 

Both the gradient and image background utilize the same image property of the background. 渐变背景和图像背景都利用背景的相同图像属性。 Its as if you are writing it like this: 就好像您是这样写的:

.class {
  background-image: url('/path/to/image.png');
  background-image: -webkit-radial-gradient(top, ellipse cover, #f9f9f9 0%,#eaeaea 100%);
}

so basically you are overwriting the image part of the background with the gradient or vice versa depending on which rule takes precedence over the other. 因此,基本上,您将使用渐变覆盖背景的图像部分,反之亦然,这取决于哪个规则优先于另一个规则。

My suggestion would be to style your markup differently. 我的建议是改变标记的样式。 Have a div inside of the div with the background you want. 在div内有一个具有所需背景的div。

<div class="background-gradient">
  <div id="first-background"></div>
</div>

If you want to mix 2 types of background on a single element, my suggestion is to use css pseudo element. 如果要在单个元素上混合两种类型的背景,我的建议是使用css伪元素。 You could use the before pseudo element having the same size as your element for your gradient part. 您可以将before伪元素的大小与渐变部分的元素大小相同。

You could have something like this: 您可能会有这样的事情:

.background_gradient {
  position: relative;
}    
.background_gradient:before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index:-1;
  background: -webkit-radial-gradient(top, ellipse cover, #f9f9f9 0%,#eaeaea 100%);
}
#first_image {
  background: url(images/img01.png);
}
#second_image {
  background: url(images/img02.png);
}
#third_image {
  position: relative;
  background: url(images/img03.png);
}

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

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