Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make an input field opacity not effect the text color inside it?

I have a dark/black background image and a white input field. I gave the input field an opacity of 50% and I want the text/value to be solid white(#fff). BUT when I apply the opacity it effects both the background of the input element and the text. How to I only change the background of the input field?

like image 759
Jordan Simon Avatar asked Sep 10 '13 14:09

Jordan Simon


People also ask

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 change the opacity of text in HTML?

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 you use opacity in color?

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 you change the opacity of an element's background without affecting the child elements or text content?

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.


4 Answers

For that you could use background-color: rgba(0, 0, 0, 0.5). The first three numbers are the background color in rgb (red, green, blue) format and the fourth number is the opacity level on a scale from 0 to 1.

like image 84
Beniamin Szabo Avatar answered Oct 08 '22 11:10

Beniamin Szabo


From what you say, you only want the background to be affected.

For backgrounds to be (partially) transarent, you have to use a

a) PNG background

or

b) a RGBa background- see https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgba()

Like so: background:rgba(0,0,0,0.2);

This is not supported in IE8 and below.

like image 33
harley_woop Avatar answered Oct 08 '22 12:10

harley_woop


The problem is that you are changing the opacity on the entire element. As such, all child elements strictly inherit the transparent properties.

There are a few things you can do.

  1. You could target only the background and set it to an RGBA value:

    background: rgba(255, 255, 255, 0.5);
    

    This wont work in IE8 and before, so you can use a workaround using linear gradient filters:

    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#80ffffff', endColorstr='#80ffffff',GradientType=0 );
    

    You will notice that the first 2 hexadecimal places are #80. This is not a mistake and is not a decimal value. Hexadecimal is base 16, this makes #80 the median point therefore setting your opacity to 50%. It's a little confusing, I know!

  2. You could remove styling from the input field and, instead, add a wrapper around your input fields and style that instead.

  3. You could use a semi-transparent PNG as the background image and set it to repeat.

like image 2
Matthew R. Avatar answered Oct 08 '22 10:10

Matthew R.


Why not simply make a half-transparent png and use that as background image instead of setting the input opacity? Or if you don't have to support IE8- you can also use rgba().

like image 1
Simon Avatar answered Oct 08 '22 12:10

Simon