CSS Opacity Properties

How to define Opacity by percentage in CSS?

If you really want to use a 0 to 100 range, you can calculate the decimal automatically:

element {
opacity: calc(40 / 100);
}

or you can use a variable to make it clearer:

element {
--opacity-percent: 40;
opacity: calc(var(--opacity-percent) / 100);
}

But both of these are less clear than just using a decimal like the standard says, so I wouldn't recommend them unless there's a really valid reason.

CSS opacity only to background color, not the text on it?

It sounds like you want to use a transparent background, in which case you could try using the rgba() function:

rgba(R, G, B, A)

R (red), G (green), and B (blue) can be either <integer>s or <percentage>s, where the number 255 corresponds to 100%. A (alpha) can be a <number> between 0 and 1, or a <percentage>, where the number 1 corresponds to 100% (full opacity).

RGBa example

background: rgba(51, 170, 51, .1)    /*  10% opaque green */ 
background: rgba(51, 170, 51, .4) /* 40% opaque green */
background: rgba(51, 170, 51, .7) /* 70% opaque green */
background: rgba(51, 170, 51, 1) /* full opaque green */

A small example showing how rgba can be used.

As of 2018, practically every browser supports the rgba syntax.

How to change text transparency in HTML/CSS?

opacity applies to the whole element, so if you have a background, border or other effects on that element, those will also become transparent. If you only want the text to be transparent, use rgba.

#foo {
color: #000; /* Fallback for older browsers */
color: rgba(0, 0, 0, 0.5);

font-size: 16pt;
font-family: Arial, sans-serif;
}

Also, steer far, far away from <font>. We have CSS for that now.

How to transition CSS display + opacity properties

Based on Michaels answer this is the actual CSS code to use

.parent:hover .child
{
display: block;

-webkit-animation: fadeInFromNone 0.5s ease-out;
-moz-animation: fadeInFromNone 0.5s ease-out;
-o-animation: fadeInFromNone 0.5s ease-out;
animation: fadeInFromNone 0.5s ease-out;
}

@-webkit-keyframes fadeInFromNone {
0% {
display: none;
opacity: 0;
}

1% {
display: block;
opacity: 0;
}

100% {
display: block;
opacity: 1;
}
}

@-moz-keyframes fadeInFromNone {
0% {
display: none;
opacity: 0;
}

1% {
display: block;
opacity: 0;
}

100% {
display: block;
opacity: 1;
}
}

@-o-keyframes fadeInFromNone {
0% {
display: none;
opacity: 0;
}

1% {
display: block;
opacity: 0;
}

100% {
display: block;
opacity: 1;
}
}

@keyframes fadeInFromNone {
0% {
display: none;
opacity: 0;
}

1% {
display: block;
opacity: 0;
}

100% {
display: block;
opacity: 1;
}
}

How to Apply Transparent Background Color to Division Without Using Opacity Property

I think what you're looking for is the mix-blend-mode property.

You can read more about it and its compatibility with different browsers in this page. Here's an example (tested on Chrome):

div {  background-color: #d35;  display: inline-block;  width: 150px;  height: 150px;  position: absolute;  top: 20px;  left: 40px;}
img { mix-blend-mode: multiply;}
<div></div><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcThfH4SD1Kod8gzTIO0WtldPqlDacHt2NLm5itWUPf7AHdbo9_2Dg" />

How do I apply opacity to a CSS color variable?

You can't take an existing color value and apply an alpha channel to it. Namely, you can't take an existing hex value such as #f0f0f0, give it an alpha component and use the resulting value with another property.

However, custom properties allow you to convert your hex value into an RGB triplet for use with rgba(), store that value in the custom property (including the commas!), substitute that value using var() into an rgba() function with your desired alpha value, and it'll just work:

:root {
/* #f0f0f0 in decimal RGB */
--color: 240, 240, 240;
}

body {
color: #000;
background-color: #000;
}

#element {
background-color: rgba(var(--color), 0.8);
}
<p id="element">If you can see this, your browser supports custom properties.</p>


Related Topics



Leave a reply



Submit