CSS Opacity / Transparency


The opacity property tells how see-through or not something is on a webpage.


Transparent Image

The opacity property uses values between 0.0 and 1.0 When the value is lower, it makes things more see-through.

sunset

opacity 0.2

sunset

opacity 0.5

sunset

opacity 1
(default)


Transparent Hover Effect

The opacity property is frequently used along with the :hover selector to adjust how transparent an element appears when the mouse hovers over it.

Northern Lights
Mountains
Italy

Example explained

The initial CSS block resembles the code in Example 1. Furthermore, we've included instructions for what should occur when a user places their cursor over one of the images. In this scenario, we desire the image to become non-transparent when the user hovers over it. The CSS code for achieving this effect is opacity:1;.

When you move the mouse away from the picture, the picture becomes see-through once more.

An example of reversed hover effect:

Northern Lights
Mountains
Italy

Transparent Box

When you use the 'opacity' property to make something see-through in the background of an object, everything inside that object also becomes see-through. This can make it difficult to read text inside an object that is completely see-through.

opacity 1

opacity 0.6

opacity 0.3

opacity 0.1


Transparency using RGBA

If you don't want to make things see-through for child parts, as we showed earlier, you can employ RGBA colors. For instance, the next example adjusts the see-through quality for the background color but not for the text:

100% opacity

60% opacity

30% opacity

10% opacity

In our CSS Colors Chapter, you discovered that RGB is a way to choose colors. But besides RGB, there's another way called RGBA. RGBA lets you pick a color and also decide how transparent it should be.

An RGBA color is made by mixing red, green, and blue, with an additional factor called "alpha." This alpha can be any number between 0.0 (completely see-through) and 1.0 (completely solid).

Tip: You will learn more about RGBA Colors in our CSS Colors Chapter.


Text in Transparent Box

This is some text that is placed in the transparent box.

Example explained

First, we make a <div> box (with the class "background") that has a picture in the background and a line around it.

Next, we make a new <div> element with the class "transbox" inside the initial <div>.

The <div class="transbox"> has a background color and a border. The div is see-through.

Within a transparent <div> box, we place text within a <p> paragraph.