简体   繁体   English

用css更改背景图像的不透明度

[英]Change background image opacity with css

I want to change header's background image opacity with css. 我想用css更改标题的背景图像不透明度。 Could you help me please. 请问你能帮帮我吗。

 .intro { display: table; width: 100%; height: auto; padding: 100px 0; text-align: center; color: #fff; background: url(http://lorempixel.com/1910/500/nature/) no-repeat bottom center scroll; background-color: #000; -webkit-background-size: cover; -moz-background-size: cover; background-size: cover; -o-background-size: cover; } 
 <header class="intro"> ... </header> 

An alternative would be that you convert this background image file to the .PNG format and make the original image 0.2 opacity using a photo editing program but if you insist on using CSS, you can use the method below: 另一种方法是将此背景图像文件转换为.PNG格式,并使用照片编辑程序将原始图像设置为0.2不透明,但如果您坚持使用CSS,则可以使用以下方法:

Since CSS does not directly support background image opacity, you can try using a pseudo-class to overlay another on your header and set opacity on it. 由于CSS不直接支持背景图像不透明度,因此您可以尝试使用伪类覆盖标题上的另一个并在其上设置不透明度。 Something along the lines of this should help: 根据这一点,应该有所帮助:

<header class="intro">
...
</header>

.intro {
    position: relative;
    background: #5C97FF;
    overflow: hidden;
}
/* You could use :after - it doesn't really matter */
.intro:before {
    content: ' ';
    display: block;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    opacity: 0.6;
    background-image: url('../img/bg.jpg');
    background-repeat: no-repeat;
    background-position: 50% 0;
    -ms-background-size: cover;
    -o-background-size: cover;
    -moz-background-size: cover;
    -webkit-background-size: cover;
    background-size: cover;
}

HTML code HTML代码

<header class="intro">
<img class="IntroImg" src="../img/bg.jpg">
</header>

CSS code CSS代码

.intro introIimg:hover {
opacity: 1.0;//Set your opacity
...

}

Hope it will help you. 希望它会对你有所帮助。

There is no CSS property background-opacity, but you can fake it by inserting a pseudo element with regular opacity the exact size of the element behind it. 没有CSS属性background-opacity,但你可以通过插入一个具有常规不透明度的伪元素来伪造它,后面是元素的确切大小。

 .intro { position: relative; z-index: 0; display: table; width: 100%; height: auto; padding: 100px 0; text-align: center; color: #fff; background-color: #000; } .intro:after { content : ""; width: 100%; height: 100%; display: inline-block; position: absolute; top: 0; left: 0; opacity : 0.2; z-index: -1; background: url(https://t1.ftcdn.net/jpg/00/81/10/58/240_F_81105881_pmBwtzXqFmtFx6rfhujAqTnhpWZf8fXn.jpg) no-repeat bottom center scroll; background-size: cover; -webkit-background-size: cover; -moz-background-size: cover; background-size: cover; -o-background-size: cover; } 
 <header class="intro"> ... </header> 

See the link here 请点击此处链接

You can change the opacity in programs like Photoshop or GIMP. 您可以在Photoshop或GIMP等程序中更改不透明度。

Or you can do that with opacity in css. 或者你可以用css中的opacity来做到这一点。 But you probably don't want that since you will have some content in your .intro which will then also be affected by it. 但是你可能不希望这样,因为你的.intro会有一些内容,然后它也会受到影响。

So I suggest following solution 所以我建议遵循解决方案

.intro {
    position: relative;
    z-index: 0;
    display: table;
    width: 100%;
    height: auto;
    padding: 100px 0;
    text-align: center;
    color: black;
    background-color: transparent;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
}


.intro:after {
    content : "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    background: url('http://www.planwallpaper.com/static/images/canberra_hero_image.jpg'); 
    background-size: cover;
    width: 100%;
    height: 100%;
    opacity : 0.2;
    z-index: -1;
}

https://jsfiddle.net/q63nf0La/ https://jsfiddle.net/q63nf0La/

Basically you add :after element that will be a background image , you position it absolute ( your .intro will need to be position:relative; ) and then you set the z-index and opacity . 基本上你添加:after作为背景图像的元素:after ,你将它定位为absolute (你的.intro将需要position:relative; )然后你设置z-indexopacity

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

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