Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background image opacity

I have a div element with text blocks and a parent div in which I have set a background image. Now I want to reduce the opacity of the background image. Please suggest how I can do that.

Thanks in advance.

EDIT:

I am looking to change the way my blog post looks at blogger.com by editing the html content. The html code looks as follows:

<div>  //my blog post </div> 

I tried to surround the whole code above with a div element and set opacity of each div separately as below:

<div style="background-image:url("image.jpg"); opacity:0.5;"> <div style="opacity:1;">  //my blog post </div> </div> 

But it is not working.

like image 361
Victor Mukherjee Avatar asked Sep 26 '12 16:09

Victor Mukherjee


People also ask

How do I change the background opacity?

Approach: We can use the opacity property in CSS which is used to change the opacity of an element. The value of the property can be set in a range of 0 to 1, where “0” is fully transparent and “1” is opaque. Any decimal value can be used in between to set the opacity accordingly.

Can we give opacity to background image?

There is no background-opacity property in CSS, but you can fake it by inserting a pseudo element with regular opacity the exact size of the element behind it.

How do you change the transparency of the background image in CSS?

opacity is a CSS property that allows you to change the opaqueness of an element. By default, all elements have a value of 1 . By changing this value closer to 0 , the element will appear more and more transparent.


2 Answers

Nowadays, it is possible to do it simply with CSS property "background-blend-mode".

<div id="content">Only one div needed</div>  div#content {     background-image: url(my_image.png);     background-color: rgba(255,255,255,0.6);     background-blend-mode: lighten;     /* You may add things like width, height, background-size... */ } 

It will blend the background-color (which is white, 0.6 opacity) into the background image. Learn more here (W3S).

like image 180
Carlos2W Avatar answered Oct 09 '22 18:10

Carlos2W


You can't use transparency on background-images directly, but you can achieve this effect with something like this:

http://jsfiddle.net/m4TgL/

HTML:

<div class="container">     <div class="content">//my blog post</div> </div>​ 

CSS:

.container {  position: relative; }  .container:before {     content: "";     position: absolute;     top: 0;     bottom: 0;     left: 0;     right: 0;     z-index: 1;     background-image: url('image.jpg');    opacity: 0.5; }  .content {     position: relative;      z-index: 2; }​ 
like image 30
Alexey Ivanov Avatar answered Oct 09 '22 20:10

Alexey Ivanov