Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Background Opacity [duplicate]

Tags:

html

css

opacity

I am using something similar to the following code:

<div style="opacity:0.4; background-image:url(...);">     <div style="opacity:1.0;">         Text     </div> </div> 

I expected this to make the background have an opacity of 0.4 and the text to have 100% opacity. Instead they both have an opacity of 0.4.

like image 367
John Wheal Avatar asked May 02 '12 23:05

John Wheal


People also ask

How do I change the background opacity in CSS?

To set the opacity of a background, image, text, or other element, you can use the CSS opacity property. Values for this property range from 0 to 1. If you set the property to 0, the styled element will be completely transparent (ie. invisible).

How do I change the background opacity without affecting text?

Simply use rgba to define your background color and specify opacity with it at the same time by adjusting the last value, for alpha, in your rgba code. For scaling, bringing the value closer to 0 increases transparency. To simplify your HTML, you don't even need the parent div around your block of text to do this.

How can I add background color to opacity?

Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.

How do I reduce 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.


2 Answers

Children inherit opacity. It'd be weird and inconvenient if they didn't.

You can use a translucent PNG file for your background image, or use an RGBa (a for alpha) color for your background color.

Example, 50% faded black background:

<div style="background-color:rgba(0, 0, 0, 0.5);">     <div>        Text added.     </div>  </div>
like image 104
AlienWebguy Avatar answered Oct 07 '22 19:10

AlienWebguy


You can use pseudo-elements ::before or ::after to get a semi-transparent background and you can do this with just one container. Use something like this:

<article>   Text. </article> 

Then apply some CSS:

article {   position: relative;   z-index: 1; }  article::before {   content: "";   position: absolute;   top: 0;    left: 0;   width: 100%;    height: 100%;     opacity: .4;    z-index: -1;   background: url(path/to/your/image); } 

Example:

body {   background: red; }  article {   position: relative;   z-index: 1; }  article:before {   content: " ";   position: absolute;   top: 0;    left: 0;   width: 100%;    height: 100px;     opacity: .4;    z-index: -1;   background: url(https://31.media.tumblr.com/8ec07e49f33088c2e32c158ca4262eb2/tumblr_n5wav6Tz4R1st5lhmo1_1280.jpg); }
<article>   Text. </article>

Note: You might need to adjust the z-index values.

like image 20
daniels Avatar answered Oct 07 '22 20:10

daniels