Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set an opacity only to the background image of a div?

Let's say I have

<div class="myDiv">Hi there</div> 

I want to put a background-image and give it an opacity of 0.5 – but I want that the text I have written will have full opacity (1).

If I would write the CSS like this

.myDiv { opacity:0.5 } 

everything will be in low opacity – and I don't want that.

So my question is – How can I get low-opacity background image with full opacity text?

like image 970
Alon Avatar asked Aug 30 '11 09:08

Alon


People also ask

How do you apply opacity to the background only?

The percentage of opacity is calculated as Opacity% = Opacity * 100 To set the opacity only to the background and not the text inside it. It can be set by using the RGBA color values instead of the opacity property because using the opacity property can make the text inside it fully transparent element.

How do I set a Div background opacity?

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).

Can you set an opacity to a 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 can I set background image transparency in HTML?

First, we create a <div> element (class="background") with a background image, and a border. Then we create another <div> (class="transbox") inside the first <div>. The <div class="transbox"> have a background color, and a border - the div is transparent.


1 Answers

So here is an other way:

background-image: linear-gradient(rgba(255,255,255,0.5), rgba(255,255,255,0.5)), url("your_image.png"); 
like image 119
GrmXque Avatar answered Sep 26 '22 05:09

GrmXque