Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Opaque text on low opacity div?

Tags:

css

opacity

I have a div with 60% opacity, to show part of a background image behind the div. Because the opacity is at 60%, the text in that div appears as grey.

Is there anyway to override this level and make the text appear black?

Any advice appreciated.

Thanks.

like image 264
Dan Avatar asked Mar 08 '10 14:03

Dan


People also ask

How do I reduce text 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 make opacity not affect my 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 make a div opaque in CSS?

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.

Is 0% opaque transparency?

The opacity-level describes the transparency-level, where 1 is not transparent at all, 0.5 is 50% see-through, and 0 is completely transparent.


2 Answers

Set the opacity on the background rather than the element.

background-color: rgba(255,0,0,0.6); 

A while ago I wrote about how to achieve this in a backwards compatible way.

like image 55
Quentin Avatar answered Sep 22 '22 07:09

Quentin


I've experimented with this in the past on my own website. By far the easiest method to achieve what you want is to create a single-pixel .PNG image with its opacity set to less than 100% (i.e., partly-transparent) and use it as a background image. By default it will fill the whole containing element - make sure that the CSS background-repeat attribute is set to 'repeat' if it doesn't.

Doing things this way you don't have to set transparency on the containing element itself, hence the opacity of its text will be unaffected.

Amazingly, there is just the tool for making a semi-transparent single-pixel .PNG here.

like image 27
JohnTheTinkerer Avatar answered Sep 23 '22 07:09

JohnTheTinkerer