如何在以前的背景图像之上添加背景图像? [英] How to add a background image on top of a previous background image?

查看:38
本文介绍了如何在以前的背景图像之上添加背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在编写的 css 页面,我需要在一个类中应用背景图像,然后使用不同的类将部分透明的背景图像放在已经存在的背景图像之上.这有点像沙拉,所以让我做一个演示.

I have a css page that I am writing and I need to apply a background image in one class and then use a different class to put a partially transparent background image on top of the one that is already there. That is a bit of a word salad so let me give a bit of a demonstration.

html{
    <div class="background1">...</div>
    <div class="background1 backgroundFilter">...</div>
    <div class="background2">...</div>
    <div class="background2 backgroundFilter">...</div>
}
css {
    .background1 {
        background-image:url(...);
    }
    .background2 {
        background-image:url(...);
    }
    .backgroundFilter {
        background-image:url(...);
    }
}

在这个例子中,第一个 div 应该有背景图像 1,第二个应该有背景图像 1,但过滤器图像放在它上面,那么第三个应该是图像 2,第四个应该是图像 2过滤它.

In this example the first div should have background image 1, the second should have background image 1 but with the filter image placed on top of it, then the third should be image 2 and the fourth should be image 2 with the same filter on it.

然而,在这个例子中,.backgroundFilter 会覆盖之前的图像而不是覆盖它.

In this example however .backgroundFilter would be overwriting the previous image instead of overlaying it.

这可能吗,还是我需要为每个版本的背景图像制作不同的类?

Is this possible or do I need to make a different class for each version of the background image?

推荐答案

可以考虑CSS变量.指定 2 个您可以稍后更改的背景图层.您可以轻松扩展到任意数量的背景:

You can consider CSS variables. Specify 2 background layer that you can change later. You can easily scale to any number of background:

.container > div {
  background:
    /*we make the default value a transparent image*/
    var(--im1,linear-gradient(transparent,transparent)),
    var(--im2,linear-gradient(transparent,transparent)); 
    
  height:200px;
  width:200px;
  display:inline-block;
}

.background1 {
  --im2: url(https://picsum.photos/200/300?image=0);
}

.background2 {
  --im2: url(https://picsum.photos/200/300?image=1069);
}

.backgroundFilter {
  --im1: linear-gradient(to right,transparent,green);;
}

<div class="container">
<div class="background1">...</div>
<div class="background1 backgroundFilter">...</div>
<div class="background2">...</div>
<div class="background2 backgroundFilter">...</div>
</div>

您也可以考虑为新背景添加一个伪元素,但您只能使用 3 个背景,因为我们只有 2 个伪元素:

You can also consider a pseudo element for the new background but you are limited with only 3 backgrounds since we have only 2 pseudo elements:

.container > div { 
  height:200px;
  width:200px;
  display:inline-block;
  position:relative;
  z-index:0;
}

.background1 {  
  background: url(https://picsum.photos/200/300?image=0);
}

.background2 { 
  background: url(https://picsum.photos/200/300?image=1069);
}

.backgroundFilter::before {
  content:'';
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background: linear-gradient(to right,transparent,green);;
}

<div class="container">
<div class="background1">...</div>
<div class="background1 backgroundFilter">...</div>
<div class="background2">...</div>
<div class="background2 backgroundFilter">...</div>
</div>

这篇关于如何在以前的背景图像之上添加背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆