Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define Opacity by percentage in CSS?

Tags:

html

css

Is it possible to define opacity in CSS by percentage (eg 30%) in CSS? does not seem to be working, right now I can only conduct it by decimal point.

https://css-tricks.com/almanac/properties/o/opacity/

.test{
   opacity: 0.3;
}

Intended Goal:

.test{
   opacity: 30%;
}
like image 368
jerrythomas38 Avatar asked Jul 12 '19 18:07

jerrythomas38


People also ask

Can I use percentage in opacity CSS?

A <number> in the range 0.0 to 1.0 , inclusive, or a <percentage> in the range 0% to 100% , inclusive, representing the opacity of the channel (that is, the value of its alpha channel). Any value outside the interval, though valid, is clamped to the nearest limit in the range.

How do I set opacity in CSS?

In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color. An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

What is opacity percentage?

Opacity is usually measured as a percentage in design tools, with zero percent being fully transparent, and one hundred percent being fully opaque.

How is opacity calculated?

The opacity of a coating is a measure of light that is incident on the coating divided by the amount of light that is transmitted.


1 Answers

Using a percentage is fully valid css, according to the spec: https://developer.mozilla.org/en-US/docs/Web/CSS/opacity#Values

alpha-value A number in the range 0.0 to 1.0, inclusive, or a percentage in the range 0% to 100%, inclusive, representing the opacity of the channel (that is, the value of its alpha channel). Any value outside the interval, though valid, is clamped to the nearest limit in the range.

So, either of these should be okay, according to spec:

.foo {
  opacity: .3;
}

.foo {
  opacity: 30%;
}

Keep in mind, though, that if you are using Sass, it will not handle the percentage value, and will compile it to 1%.

like image 197
tiennen07 Avatar answered Sep 21 '22 21:09

tiennen07