The CSS opacity property determines the transparency of an element on a scale of 0.0 to 1.0. The greater the opacity value, the more clear the element is. The element is totally transparent or clear when the value is 1.0
which is also the default value, at 0.5 the element displayed quite blur, and at 0.0 the element is totally opaque.
Syntax of opacity property in CSS
Example: Applying opacity to the HTML element
In the given example, we have created the three headings using the <h1>, <h2>, and <h3> element. In heading 1 we have specified the opacity value to 0.0, in heading 2 we have specified the opacity value to 0.5
, and in heading 3 we have specified the value to 1.0.
So after implementation what we can see is, the heading 1 with the value 0.0 is not visible at all. In heading 2 whose opacity value is 0.5, the content is quite blurred but visible while in heading 3 whose opacity value is 1.0 is clearly visible.
Transparent and Hover Effect in CSS
The CSS allows us to use the CSS opacity along with the
:hover effect. When we provide a hover effect to any element its opacity changes when the user hovers the mouse over it.
Example: Applying Transparent and Hover effect in CSS
In this example, there are three images and each image has a different opacity value along with the hover effect. When the user moves the cursor over any of the images, it will become transparent again.
<img src="studytonight.jpg" alt="Forest" width="250" height="150">
<img src="studytonight.jpg" alt="Mountains" width="250" height="150">
<img src="studytonight.jpg" alt="Italy" width="250" height="150">
As we can see, all three images are a blur because we have specified the CSS property opacity with the value of 0.5. When we move the cursor over any of the images it will become transparent again because we have specified the value of the opacity to 1 after the hover effect.
Adding Text in Transparent Box in CSS
With the help of CSS, we can also insert the text over the image along with the opacity property. Let's understand it with the help of a live example.
Example: Adding text in the transparent box using CSS opacity
In this example, we have created a <div> element with class
.background and specify the CSS property
border. Then we have created another <div> with class .
transbox inside the first <div>. Inside the
.transbox class we have added <p> element to insert the text in it.
In this lesson, we have learned how to make the images blur or invisible using the CSS opacity property. We have also learned how to make the image transparent by applying the opacity property. With the help of this property, we can also make the text blur.