如何在透明的背景图片后添加背景颜色?

3

我试图在透明的背景图片后面添加背景颜色,但是它没有生效。 我想把它放在名为“heading”的div中。 我首先尝试使用png文件,然后尝试gif文件。 我以为所有的gif都是透明的图像。 希望能得到任何帮助。

#heading {
    background-image: url(http://bartonlewis.com/_images/pg_p_lewis_bckgrnd.gif);
    background-size: 978px 1587px;
    background-repeat: no-repeat;
    background-blend-mode: screen;
    background-color: rgba(255, 230, 184, 0.56);
    position: relative;
    height:100%;
    height: 230px;
    width: 960px;
    }
#content {
    float: left;
    height: 1800px;
    width: 960px;
    font-size: 1.1em;
    line-height: 1.2em;
    z-index:1;
    position:relative;
    }
div#heading p {
    font-size: 72px;
    text-align: center;
    margin: 100px 0;
    letter-spacing: 2px;
    }
#colD {
    width: 320px;
    float: left;
    text-indent: -999px;
    }
#colE {
    width: 30px;
    float: left;
    }
#colF {
    width: 250px;
    float: left;
    text-align: center;
    letter-spacing:.1em;
    font-family: "amador"; 
    font-variant: normal;
    font-size: 1.5em;
    }
#colG {
    width: 10px;  
    float: left;
    }
#colH {
    width: 350px;
    float: left;
    }
.floral-icon {
    vertical-align: middle;
    }
#colG img {
    -ms-transform: rotate(180deg); /* IE 9 */
    -webkit-transform: rotate(180deg); /* Chrome, Safari, Opera */
    transform: rotate(180deg);
}

    <div id="background">
    </div>
        <div id="content">
            <div id="heading">
                <div id="colD">empty</div>
                <div id="colE"><p><img class="floral-icon" src=http://bartonlewis.com/_images/pg_p_lewis_icon.png width="32" height="32"></p></div>
                <div id="colF"><p>Lewis</p></div>
                <div id="colG"><p><img class="floral-icon" src=http://bartonlewis.com/_images/pg_p_lewis_icon.png alt="floral icon" width="32" height="32"></p></div>
                <div id="colH"></div>
            </div>

1
不应该使用不透明度,因为它会影响所有的后代元素。你可以直接使用 background: #F0D8AB url(_images/pg_p_lewis_bckgrnd.gif) 987px 1587px no-repeat; - 这应该可以工作。顺便说一下,PX 单位没有小数点。等等... 你正在尝试在一个不属于其余内容的 <div> 上设置背景图片 - 如果你想将该 div 放置在其余内容上方,你也需要给它一个 z-index,这是我的猜测。我本来想测试一下,但由于你没有使用实际的图像 URL,我不知道它们长什么样子。 - junkfoodjunkie
谢谢。我在跟随背景div的内容div上使用了z-index,并将其添加到了我的代码中。当我用您提供的内容替换我的背景属性时,颜色确实出现了,但我的透明图像现在没有显示出来。我该如何为图像提供“实际的url”,以便有人可以测试它(我是新手)。谢谢。 - Barton Lewis
您需要将图像上传到服务器上,并在代码中使用完整的URL链接到它们。如果它们已经在服务器上可用,只需使用直接的http://链接替换链接到它们实际位置的链接即可。 - junkfoodjunkie
1个回答

6
您可以通过使用伪元素:before来实现这一点。
这个示例中查看。
#heading {
  background-image: url(https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png);
  background-repeat: no-repeat;
  position: relative;
  height:100%;
  height: 300px;
  width: 960px;
}
#heading::before {
  background-color: rgba(240, 216, 171, 0.25);
  display: block;
  content: '';
  position: absolute;
  height: inherit;
  width: inherit;
}

在这里,由于你需要为头部元素使用它,所以使用了头部而不是单独的背景div。请注意,:before元素被放置在绝对定位中,实际元素相对于其放置。背景颜色应用于before元素和具有背景图像的标题上方的渲染。

编辑1

我们可以使用background-blend-mode:screen来控制background-imagebackground-color的不透明度,而无需使用:before

此 fiddle中查看它。

rgba background-color的alpha通道可以控制background-image的透明度。

#heading {
  background-image: url(https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png);
  background-repeat: no-repeat;
  background-blend-mode: screen;
  background-color: rgba(255, 230, 184, 0.66);
  position: relative;
  height:100%;
  height: 300px;
  width: 960px;
}

我使用了谷歌的标志,因为你提供的图片源路径是相对于你的页面的。


谢谢。但是现在没有办法通过CSS控制图像的不透明度,对吗?我必须在图像本身中进行调整。虽然我正在学习Adobe Lightroom,但我不知道Photoshop。理想情况下,我希望能够通过CSS控制不透明度并拥有背景颜色。有什么方法可以实现吗? - Barton Lewis
不完全是这样,但你可以在#heading部分的background-color属性中使用不同灰度的色调来玩弄background-blend-mode属性,以获得类似的效果。 - Sen Jacob
谢谢,但我不想要灰色,而是赭色/棕褐色。所以没有办法在有色背景上使用CSS控制透明的背景图片吗?我觉得这很奇怪。 - Barton Lewis
谢谢。根据您的建议,我重新发布了我的代码,并删除了昨天发布的答案。现在我遇到的问题是背景图像和颜色改变了不透明度,但文本和文本左侧的第一个花卉图标也改变了不透明度。只有文本右侧的第二个花卉图标是不透明的。您能看出是什么原因导致我的文本(“Lewis”)和第一个花卉图标不透明吗?谢谢。 - Barton Lewis
此外,背景颜色的不透明度和背景图片的不透明度可以分别控制吗? - Barton Lewis
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接