Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set opacity on container div and not on children text?

Tags:

html

css

I have a DIV with the CSS style rule opacity: 0.4;.

Inside the div there is a a tag and the text also has an opacity.

How can I declare for the text: opacity :1 or any good idea.... You can see it in the following link:http://yagen.org/

The gallery In the above of the page.

like image 746
treenet Avatar asked Dec 11 '11 14:12

treenet


People also ask

How can I make a div transparent without affecting text?

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 you change opacity without affecting children's elements?

Answer: Use the CSS RGBA colors There is no CSS property like "background-opacity" that you can use only for changing the opacity or transparency of an element's background without affecting its child elements.

How do I change the opacity of an image without affecting the text?

One approach you can use is to put the background-image styles in a pseudo-element of the parent element. Since the pseudo-element is a sort of child of the parent, you can change the opacity of it without affecting the text content.

How do you override opacity in children?

Basically, you can't override the opacity of a child. The answer you might be looking for will depend on what it is you are actually trying to do. Paulie is right, opacity effects the entire element (including children) and can't be reversed by a child element.


2 Answers

If you set the opacity of an element, the opacity is set for all of its children as well. If you want opaque text on a transparent background, take a look at RGBa.

The result would look something like this:

.mycontainer {
    background: rgb(60, 60, 60);
    background: rgba(60, 60, 60, 0.4);
}

.mycontainer a {
    color: #fff;
}

The first background declaration serves as a fallback in case a browser doesn't support RGBa color - it will simply be a solid color instead.

Here's a great reference for RGBa color: https://css-tricks.com/rgba-browser-support/

like image 57
derekerdmann Avatar answered Sep 28 '22 18:09

derekerdmann


If you have your HTML of this sort:

<div id="container">
    <p>
        Darn fanatically far and tarantula jeepers meek a secret much so hence underneath monogamously interwove apart gosh spilled far where and badger.
    </p>
    <a href="#">This is a link</a>
</div>

Even if your CSS is this.

#container {
    background: #000;
    color: #fff;
    opacity: 0.4;
}

#container a {
    color: #ff0450;
    opacity: 1;
}

It will not make the link have a greater opacity than the container because opacity is inherited from the parent.

The only way to do it is using rgba values but it will not work in IE.

The correct way to do it is this -

#container {
    background: rgba(0,0,0,0.4);
    color: #fff;
}

Take a look at this fiddle

like image 45
Aniket Avatar answered Sep 28 '22 17:09

Aniket