The transparency or opacity of an element is specified by its opacity property.
The range of values for the opacity property is 0.0 to 1.0. The more transparent, the lower the value:
img {
opacity: 0.5;
}
To adjust the opacity upon mouse-over, the opacity property is frequently combined with the :hover selector:
img {
opacity: 0.5;
}
img:hover {
opacity: 1.0;
}
The code in Example 1 is comparable to the first CSS block. We’ve also included the expected behavior for when a user hovers over one of the photos. When the user hovers over this image, we want it to remain opaque. This is using opacity:1; as the CSS.
The image will revert to transparency when the mouse pointer leaves it.
An instance of the hover effect in reverse:
img:hover {
opacity: 0.5;
}
All of an element’s child elements inherit the same transparency when the element’s background is made transparent via the opacity attribute. This may make it difficult to read text contained within a fully translucent element:
div {
opacity: 0.3;
}
Use RGBA color values if you don’t want to apply opacity to child elements, as we did in the previous example. The example below adjusts the opacity of the backdrop color rather than the text:
RGB can be used as a color value, as you discovered in our CSS Colors Chapter. You can utilize an RGB color value with an alpha channel (RGBA), which defines a color’s opacity, in addition to RGB.
The notation for an RGBA color value is rgba(red, green, blue, alpha). A number between 0.0 (completely transparent) and 1.0 (totally opaque) represents the alpha parameter.
Advice: Check out our CSS Colors Chapter for additional information on RGBA colors.
div {
background: rgba(76, 175, 80, 0.3) /* Green background with 30% opacity */
}
This is some text that is placed in the transparent box.
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.
Inside the transparent <div>, we add some text inside a <p> element.
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.