我可以在同一属性中设置背景图像和不透明度吗?


264

我可以在CSS参考中看到如何设置图像透明度以及如何设置背景图像。但是,如何将这两者结合以设置透明的背景图像?

我有一张要用作背景的图像,但是它太亮了-我想将不透明度降低到约0.2。我怎样才能做到这一点?

#main {
    background-image: url(/wp-content/uploads/2010/11/tandem.jpg); 
}

Answers:


133

两种方法:

  1. 转换为PNG并使原始图像0.2不透明
  2. (更好的方法)有一个<div>position: absolute;#main和相同的高度#main,然后应用背景图像和opacity: 0.2; filter: alpha(opacity=20);

是的,PNG的最大问题是尺寸。它可能会很大。既然我们可以opacity: ...在所有浏览器中指定,那就更好了!
Alexis Wilke

1
您会惊讶于可以从PNG中挤出的内容。例如,在不透明度为0.2的情况下,您可能不会选择很多细节,因此可以转换为Indexed-PNG。我实际上是在自己的网站上使用它的,它将1920x1080的背景图像压缩到不足250kb的大小。
Niet the Dark Absol's

最好将z-index:-1添加到该position:absolute,以允许使用覆盖层上方的底部。好答案。
拉兹

1
您还可以使用WebP-它支持透明性,文件大小比PNG小得多。除Safari外,所有主要浏览器现在都支持WebP。您可以将WebP提供给支持的浏览器,将PNG提供给Safari。
丹·罗伯茨

364
#main {
    position: relative;
}
#main:after {
    content : "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    background-image: url(/wp-content/uploads/2010/11/tandem.jpg); 
    width: 100%;
    height: 100%;
    opacity : 0.2;
    z-index: -1;
}

4
从IE8开始生成的内容。caniuse.com/#feat=css-gencontent,将过滤器属性用于IE8。caniuse.com/#search=opacity
Dan

3
我在CSS中添加了“ z-index:-1”。这样,背景图像会受到不透明度的影响,而不是#main的其余内容的影响
patrick

17
我使用:before代替,:after然后我不必篡改,z-index因为它:before最终自动位于主要内容之下。(目前已在Chrome和FF中测试。)
KajMagnus

1
@KajMagnus如果这样做,那么之后的所有内容都不会显示。尝试将不透明度设置为1.0,您将会明白我的意思。
杰西卡

1
如果您得到重复的背景图片,则可能希望/需要background-repeat: no-repeat; background-attachment: fixed; background-position: center;#main:{...}之后添加。
ban-geoengineering

106

1 div和无透明图像的解决方案:

您可以使用多背景CSS3功能并放置两个背景:一个带有图像,另一个带有透明面板(因为我认为无法直接设置背景图像的不透明度):

background: -moz-linear-gradient(top, rgba(0, 0, 0, 0.7) 0%, rgba(0, 0, 0, 0.7) 100%), url(bg.png) repeat 0 0, url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;

background: -moz-linear-gradient(top, rgba(255,255,255,0.7) 0%, rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;

background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0.7)), color-stop(100%,rgba(255,255,255,0.7))), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;

background: -webkit-linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;

background: -o-linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;

background: -ms-linear-gradient(top, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;

background: linear-gradient(to bottom, rgba(255,255,255,0.7) 0%,rgba(255,255,255,0.7) 100%), url(https://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png) repeat 0 0;

您不能使用rgba(255,255,255,0.5)它,因为仅在背面就可以使用它,因此在此示例中,我为每个浏览器使用了生成的渐变(这就是为什么它这么长)。但是概念如下:

background: tranparentColor, url("myImage"); 

http://jsfiddle.net/pBVsD/1/


仅当背景为纯色时才有效,如果您有PNG24作为背景并且您希望它具有不透明性(例如,在悬停时),那么它将不起作用,并且您将必须使用伪元素方法,实际上更好,因为它可以在IE 8及更高版本中使用。
vsync 2014年

66

简单的解决方案

如果您只需要将渐变设置为背景图像

background-image: url(IMAGE_URL); /* fallback for older browsers */

background-image: linear-gradient(to bottom, rgba(0,0,0,0.6) 0%,rgba(0,0,0,0.6) 100%), url(IMAGE_URL);

图像稍微移到顶部。它有修复程序吗?
HFR1994 '16

6
您还可以使用RGBA 255,255,255背景图像:linear-gradient(to bottom,rgba(255,255,255,0.6)0%,rgba(255,255,255,0.6)100%),url(IMAGE_URL);
罗马

@罗马您可以将rgba颜色分量设置为元素或其嵌入父级中用作背景色的值(在Chrome 58.0.3029.81,Edge 38.14393.0.0上进行了测试)。
collapsar

46

我看到了这一点,现在在CSS3中可以像这样放置代码。可以说我希望它涵盖整个背景,我会做这样的事情。然后,使用hsla(0,0%,100%,0.70)或rgba,您可以使用具有任意百分比饱和度或不透明度的白色背景来获得所需的外观。

.body{
    background-attachment: fixed;
    background-image: url(../images/Store1.jpeg);
    display: block;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    background-color: hsla(0,0%,100%,0.70);
    background-blend-mode: overlay;
    background-repeat: no-repeat;
}

2
添加background-color和background-blend-mode对我有用。现在我有一个不透明度不影响其子级的div!
迭戈·维克多·德·耶稣

1
这很棒!我创建了一个jquery函数来更改滚动的不透明度,并在向下滚动时在背景图像上创建颜色。$(function(){$(window).scroll(function(){var scroll = $(window).scrollTop(); var current = 0; var now =(current-scroll)/ 7; $('。slide ').css('背景位置','50%'+现在+'px');变色= Math.round(scroll / 7)/ 100; $('。slide')。css('background- color','rgba(128,45,87,'+ color +')');});});
drooh

10

您可以使用CSS伪选择器:: after实现此目的。这是一个工作示例。

在此处输入图片说明

.bg-container{
  width: 100%;
  height: 300px;
  border: 1px solid #000;
  position: relative;
}
.bg-container .content{
  position: absolute;
  z-index:999;
  text-align: center;
  width: 100%;
}
.bg-container::after{
  content: "";
  position: absolute;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  z-index:-99;
  background-image: url(https://i.stack.imgur.com/Hp53k.jpg);
  background-size: cover;
  opacity: 0.4;
}
<div class="bg-container">
   <div class="content">
     <h1>Background Opacity 0.4</h1>
   </div>
</div>


4

我有一个类似的问题。我有一个图像,想降低透明度,并且在图像后面有一个黑色背景。而不是降低不透明度或创建黑色背景或任何辅助div,我在一行上都为图像设置了线性渐变:

background: linear-gradient(to bottom, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0.8) 100%), url("/img/picture.png");


3

我使用了@Dan Eastwell的答案,它的效果很好。该代码与他的代码相似,但有一些补充。

.granddata {
    position: relative;
    margin-left :0.5%;
    margin-right :0.5%;
}
.granddata:after {
    content : "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    background-image: url("/Images/blabla.jpg");
    width: 100%;
    height: 100%;
    opacity: 0.2;
    z-index: -1;
    background-repeat: no-repeat;
    background-position: center;
    background-attachment:fixed;
}

2

制作简单图像的一种好方法是仅使用CSS来设置HTML元素的背景,就像这样。

HTML {
    background: url('http://www.earthtimes.org/newsimage/eat-lead-by-example-obesity-expert-tells-parents_139.jpg');
    width: 100%;
    height: 100%;
}

如果要花哨的时间并设置其不透明度,那么在IE9 + *中,可以设置主体的透明背景色。这可以通过覆盖半透明的白色以使图像更白并显得更亮来实现。例如,不透明度为75%的白色(rgba(255,255,255,.75))将产生以下效果。

HTML {
  background: url('http://www.earthtimes.org/newsimage/eat-lead-by-example-obesity-expert-tells-parents_139.jpg');
  width: 100%;
  height: 100%;
  position: relative;
}

body {
  width: 100%;
  min-height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  margin: 0;
  background: rgba(255, 255, 255, .75);
}
<p>Lorem ipsum dolor sit amet, et id, maecenas a convallis, elit facilisi congue augue id ac. Suscipit luctus egestas rutrum, amet tincidunt porttitor ante convallis magna, vivamus amet at turpis, ante lacinia blandit vel metus mauris rutrum. Ipsum nam adipiscing.
  Est sapien quis sem vestibulum, quis cubilia turpis, suspendisse mattis vehicula enim risus pede, placerat suspendisse dui. Convallis nostrud pede, mollit lobortis, ornare ipsum tempor faucibus tortor, vel pede, porttitor nulla nonummy vestibulum purus.
  Eros placerat tenetur augue ipsum aliquam, pellentesque congue condimentum sed vitae lectus.</p>
<p>Aliquam venenatis curabitur pellentesque sociosqu quam. Tincidunt id erat vestibulum in, est fermentum ipsum et augue, nascetur etiam. Lorem elit, sed donec, leo vivamus ac id enim faucibus vel. Nullam sit feugiat sed massa consectetuer auctor, nulla
  et erat lacinia nec, eget ut ante nullam est non in, elit porttitor in in donec eget porttitor. Interdum ultricies sem morbi facilisis nibh erat. Id suspendisse, sed tincidunt fringilla sit, sapien odio vel, at culpa augue sed taciti neque inceptos,
  massa class non. Vel tristique condimentum at pellentesque, commodo nulla sagittis rhoncus lorem. Viverra maecenas tellus pretium urna mauris proin, vel libero morbi, ante volutpat vestibulum augue. Itaque leo mauris turpis, vivamus nisl congue nisl
  nulla in turpis, eget vitae accumsan dolor ipsum leo venenatis, feugiat vehicula in risus, donec eu pede vivamus itaque nam. Justo amet vitae pellentesque sed, posuere fusce sapien, sed nam placerat libero sed etiam curabitur, suspendisse justo, amet
  porttitor.</p>
<p>Condimentum eget. Sem elementum a et mauris sem a, velit justo consectetuer in turpis mauris, sit sed elit cum, fusce suspendisse pretium dictum, mattis dui tortor tellus. Volutpat ut ante lorem nec laoreet aliquam, lorem est magna amet, integer mauris
  purus tellus. Porta enim repellendus aliquam eros. Turpis posuere elementum suscipit wisi lobortis, nec nunc consequat dictum ut unde at, mi lorem amet nunc. Cubilia pede, integer dolor, eget platea felis elit enim rhoncus, integer proin quam in ipsum
  lorem, diam curabitur netus pretium pellentesque. Donec rutrum ultrices placerat, curabitur maecenas, feugiat pede varius accumsan quam lorem, dui et dictumst asperiores nulla, vivamus urna nam leo libero. Posuere non convallis amet justo parturient,
  imperdiet consectetuer arcu praesent cursus risus, quis pretium dolor.</p>

  • 提示:请注意HTML的样式position: relative,而主体是position: absolute。这是为了防止正文的背景颜色表现得更像正文中的荧光笔。

通过改变人体的RGBA颜色背景,甚至可以将其扩展到与CSS滤镜相当但仍非常不同的东西。例如,rgba(0,255,0,.75)您将在代码段中看到非常绿色的色调。

HTML {
    background: url('http://www.earthtimes.org/newsimage/eat-lead-by-example-obesity-expert-tells-parents_139.jpg');
    width: 100%;
    height: 100%;
    position: relative;
}
body {
    width: 100%;
    min-height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    margin: 0;
    background: rgba(0,255,0,.75);
}
<p>Aliquam venenatis curabitur pellentesque sociosqu quam. Tincidunt id erat vestibulum in, est fermentum ipsum et augue, nascetur etiam. Lorem elit, sed donec, leo vivamus ac id enim faucibus vel. Nullam sit feugiat sed massa consectetuer auctor, nulla et erat lacinia nec, eget ut ante nullam est non in, elit porttitor in in donec eget porttitor. Interdum ultricies sem morbi facilisis nibh erat. Id suspendisse, sed tincidunt fringilla sit, sapien odio vel, at culpa augue sed taciti neque inceptos, massa class non. Vel tristique condimentum at pellentesque, commodo nulla sagittis rhoncus lorem. Viverra maecenas tellus pretium urna mauris proin, vel libero morbi, ante volutpat vestibulum augue. Itaque leo mauris turpis, vivamus nisl congue nisl nulla in turpis, eget vitae accumsan dolor ipsum leo venenatis, feugiat vehicula in risus, donec eu pede vivamus itaque nam. Justo amet vitae pellentesque sed, posuere fusce sapien, sed nam placerat libero sed etiam curabitur, suspendisse justo, amet porttitor.</p>
<p>Condimentum eget. Sem elementum a et mauris sem a, velit justo consectetuer in turpis mauris, sit sed elit cum, fusce suspendisse pretium dictum, mattis dui tortor tellus. Volutpat ut ante lorem nec laoreet aliquam, lorem est magna amet, integer mauris purus tellus. Porta enim repellendus aliquam eros. Turpis posuere elementum suscipit wisi lobortis, nec nunc consequat dictum ut unde at, mi lorem amet nunc. Cubilia pede, integer dolor, eget platea felis elit enim rhoncus, integer proin quam in ipsum lorem, diam curabitur netus pretium pellentesque. Donec rutrum ultrices placerat, curabitur maecenas, feugiat pede varius accumsan quam lorem, dui et dictumst asperiores nulla, vivamus urna nam leo libero. Posuere non convallis amet justo parturient, imperdiet consectetuer arcu praesent cursus risus, quis pretium dolor.</p>
<p>Lorem ipsum dolor sit amet, et id, maecenas a convallis, elit facilisi congue augue id ac. Suscipit luctus egestas rutrum, amet tincidunt porttitor ante convallis magna, vivamus amet at turpis, ante lacinia blandit vel metus mauris rutrum. Ipsum nam adipiscing. Est sapien quis sem vestibulum, quis cubilia turpis, suspendisse mattis vehicula enim risus pede, placerat suspendisse dui. Convallis nostrud pede, mollit lobortis, ornare ipsum tempor faucibus tortor, vel pede, porttitor nulla nonummy vestibulum purus. Eros placerat tenetur augue ipsum aliquam, pellentesque congue condimentum sed vitae lectus.</p>

  • 提示: RGBA相当于řģ lpha。因此,浏览器将解释rgba(0,255,0,.75){red:0, green:255, blue:0, alpha:'75%'}


*可以在“我可以使用”中找到完整的兼容性表但是,还请注意,您需要单击“显示全部”以查看IE9是否支持它。


附录

由于我已经回答了这个问题,但是我想补充的更多,因此我在本节附录中添加了标题,并添加了一些可能有用的信息。因此,要进一步推断以上内容,您可以使用SVG作为背景图像来完成邪恶的事情。例如,您可以创建一个带有酷网站图标的加载屏幕背景,如下面以base64编码的SVG的示例所示。

HTML {
    background: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iNDAwIiBoZWlnaHQ9IjQwMCIgdmVyc2lvbj0iMS4xIiB2aWV3Qm94PSIwIDAgMSAxIiBwcmVzZXJ2ZUFzcGVjdFJhdGlvPSJ4TWluWU1pbiIgYXJpYS1oaWRkZW49InRydWUiPjxnIGlkPSJrIj48ZGVmcz48bGluZWFyR3JhZGllbnQgaWQ9ImMiIGdyYWR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QU1QTEUgV0VCU0lURTwvdHNwYW4+IExPR088L3RleHRQYXRoPjwvdGV4dD48L2c+PC9nPjwvc3ZnPg==');
    width: 100%;
    height: 100%;
    position: relative;
    background-size: cover;
}
body {
    width: 100%;
    min-height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    margin: 0;
    background: rgba(255,255,255,.5);
}
<p>Condimentum eget. Sem elementum a et mauris sem a, velit justo consectetuer in turpis mauris, sit sed elit cum, fusce suspendisse pretium dictum, mattis dui tortor tellus. Volutpat ut ante lorem nec laoreet aliquam, lorem est magna amet, integer mauris purus tellus. Porta enim repellendus aliquam eros. Turpis posuere elementum suscipit wisi lobortis, nec nunc consequat dictum ut unde at, mi lorem amet nunc. Cubilia pede, integer dolor, eget platea felis elit enim rhoncus, integer proin quam in ipsum lorem, diam curabitur netus pretium pellentesque. Donec rutrum ultrices placerat, curabitur maecenas, feugiat pede varius accumsan quam lorem, dui et dictumst asperiores nulla, vivamus urna nam leo libero. Posuere non convallis amet justo parturient, imperdiet consectetuer arcu praesent cursus risus, quis pretium dolor.</p>
<p>Lorem ipsum dolor sit amet, et id, maecenas a convallis, elit facilisi congue augue id ac. Suscipit luctus egestas rutrum, amet tincidunt porttitor ante convallis magna, vivamus amet at turpis, ante lacinia blandit vel metus mauris rutrum. Ipsum nam adipiscing. Est sapien quis sem vestibulum, quis cubilia turpis, suspendisse mattis vehicula enim risus pede, placerat suspendisse dui. Convallis nostrud pede, mollit lobortis, ornare ipsum tempor faucibus tortor, vel pede, porttitor nulla nonummy vestibulum purus. Eros placerat tenetur augue ipsum aliquam, pellentesque congue condimentum sed vitae lectus.</p>
<p>Aliquam venenatis curabitur pellentesque sociosqu quam. Tincidunt id erat vestibulum in, est fermentum ipsum et augue, nascetur etiam. Lorem elit, sed donec, leo vivamus ac id enim faucibus vel. Nullam sit feugiat sed massa consectetuer auctor, nulla et erat lacinia nec, eget ut ante nullam est non in, elit porttitor in in donec eget porttitor. Interdum ultricies sem morbi facilisis nibh erat. Id suspendisse, sed tincidunt fringilla sit, sapien odio vel, at culpa augue sed taciti neque inceptos, massa class non. Vel tristique condimentum at pellentesque, commodo nulla sagittis rhoncus lorem. Viverra maecenas tellus pretium urna mauris proin, vel libero morbi, ante volutpat vestibulum augue. Itaque leo mauris turpis, vivamus nisl congue nisl nulla in turpis, eget vitae accumsan dolor ipsum leo venenatis, feugiat vehicula in risus, donec eu pede vivamus itaque nam. Justo amet vitae pellentesque sed, posuere fusce sapien, sed nam placerat libero sed etiam curabitur, suspendisse justo, amet porttitor.</p>

  • 提示:background-size: coverCSS导致在后台SVG标志被调整到HTML元素的大小。

1

在您的CSS中添加...

 filter: opacity(50%);

在JavaScript中使用...

 element.style.filter='opacity(50%)';

注意:根据需要添加供应商前缀,但Chromium应该没有。


0

好吧,仅使背景透明的CSS方式就是通过,RGBa但是由于您要使用图像,我建议使用Photoshop或Gimp使图像透明,然后将其用作背景。


这是不正确的,有一个CSS3声明:opactiy: 1;,而不仅仅是RGBa。
凯尔(Kyle)2010年

4
他的意思是background-image在容器上设置样式,然后opacity在同一元素上设置样式。这也将使该元素的文本和其他内容透明。如果您不相信我,请尝试以下方法:w3schools.com/Css/tryit.asp?

0

我只是在#main中添加position = absolute,top = 0,width = 100%并将不透明度值设置为#background

#main{height:100%;position:absolute; top:0;width:100%}
#background{//same height as main;background-image:url(image URL);opacity:0.2}

我将背景应用到主体之前的div。


-1

我碰到了这个帖子,因为我遇到了同样的问题,然后我想为什么当您可以轻松地在Photoshop中调整不透明度时,为什么要对CSS进行调整,调整值并进行刷新?复制图像,将其粘贴为新图层,然后移动不透明度滑块。


2
ArtB正确,带有最后一个音符。这是另一个问题的完美解决方案。有时候这不是正确的解决方案,这就是其中之一。
2013年

-1

我遇到了类似的问题,我只是用photoshop拍摄了背景图片,并使用所需的不透明度创建了一个新的.png。解决了问题,无需担心我的CSS是否能在所有设备和浏览器上正常运行

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.