首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在两种不同的属性上设置背景图像url和梯度?

如何在两种不同的属性上设置背景图像url和梯度?
EN

Stack Overflow用户
提问于 2014-01-31 13:29:22
回答 2查看 156关注 0票数 1

我在带有单个属性的background-image的基础上添加了一个梯度:

代码语言:javascript
复制
background-image: -webkit-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.5)), url('/image-1.jpg');

使用class .gradient的所有元素都具有梯度,但是每个元素的图像都会发生变化,因此我想知道是否可以将梯度设置为.gradient的属性,然后在每个元素中更改图像url。

代码语言:javascript
复制
<div class="gradient image-1"></div>
<div class="gradient image-2"></div>
<div class="gradient image-3"></div>

就像这样:

代码语言:javascript
复制
.gradient // Add the gradient just once
{
background-color: background-image: -webkit-linear-gradient(top, rgba(0,0,0,0), rgba(0,0,0,0.5);
}
.image-1 // Change the image for each element
{
background-image: url('/image-1.jpg');
}
.image-2
{
background-image: url('/image-2.jpg');
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-31 13:43:05

您无法只定义和覆盖多个background层中的一个,因此需要为每个层重新声明。

Demo

代码语言:javascript
复制
div {
    height: 100px;
    width: 100px;
    background: #eee;
    margin: 20px;
    background:  url('http://www.google.com/images/icons/product/chrome-48.png'), linear-gradient(to bottom, #a90329 0%,#8f0222 44%,#6d0019 100%);
}

div:nth-of-type(2) {
    background:  url('http://www.digitalkonline.com/Portals/86261/images/agree-icon.png'), linear-gradient(to bottom, #a90329 0%,#8f0222 44%,#6d0019 100%)
}

实现这一目标的另一种方法是在父元素上使用background,在子元素上使用另一个background,因此可以使用嵌套技巧。

Demo 2

代码语言:javascript
复制
div.wrap {
    height: 100px;
    width: 100px;
    background: #eee;
    margin: 20px;
    background: linear-gradient(to bottom, #a90329 0%,#8f0222 44%,#6d0019 100%);
}

div.wrap div {
    height: 100%;
    width: 100%;
}

div.hold > div.wrap:nth-of-type(1) > div {
    background:  url('http://www.digitalkonline.com/Portals/86261/images/agree-icon.png');
}

div.hold > div.wrap:nth-of-type(2) > div {
    background:  url('https://www.google.com/images/icons/product/chrome-48.png');
}
票数 0
EN

Stack Overflow用户

发布于 2014-01-31 13:35:17

我不确定它是否适用于渐变。然而,当我们想要添加2个图像时,我使用背景大小:包含;

代码语言:javascript
复制
background-image: URL("../images/imageName1.png"), URL("../images/Icone/imageName2.png");
background-size:contain;
background-repeat: repeat-x, no-repeat;

第一个重复将是第一个图像,以此类推。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21481286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档