You can control the transparency (or opacity) of an element in CSS using the following three primary methods:
#The opacity property:
The CSS opacity property is a measure of an element's transparency. It controls the degree to which an element, along with all its content and children, is visually hidden or see-through.
It is the opposite of transparency:
-
High Opacity = Low Transparency (More visible, less see-through)
-
Low Opacity = High Transparency (Less visible, more see-through)
🎨 Value and Syntax:
The opacity property accepts a value between 0.0 and 1.0:
| Value |
Meaning |
Description |
| 1.0(Default) |
Fully opaque |
The element is 100% visible (solid). |
| 0.5 |
Semi-transparent |
The element is 50% visible, allowing content behind it to show through. |
| 0.0 |
Fully transparent |
The element is 0% visible (invisible). Note: The element still occupies space and responds to events like clicks. |
Example:
<style>
.background-content {
color: white;
background-color: #000;
text-align: center;
}
.box {
opacity: 1;
width: 200px;
height: 100px;
display: inline-block;
border: 1px solid black;
color: white;
}
.opacity-demo {
background-color: #000000;
opacity: 0.5;
}
</style>
<div class="background-content">
Background Text Layer
</div>
<div class="box opacity-demo">
<p>Text is also faded.</p>
</div>
Output:
#RGBA (Red, Green, Blue, Alpha) 🔴🟢🔵
rgba() is based on the additive color model used by electronic screens. Colors are created by mixing varying intensities of red, green, and blue light.
| Component |
Range |
Purpose |
| R(Red) |
0 to 255 |
Intensity of the red light. |
| G(Green) |
0 to 255 |
Intensity of the green light. |
| B(Blue) |
0 to 255 |
Intensity of the blue light. |
| A(Alpha) |
0.0 to 1.0 |
The transparency level (0.0 is fully transparent; 1.0 is fully opaque). |
<style>
background-color: rgba(255, 100, 100, 0.7);
<style>
<div class="rgba"> A light-red background with 70% opacity (30% transparent)</div>
Output:
A light-red background with 70% opacity (30% transparent)
#HSLA
HSLA stands for Hue, Saturation, Lightness, Alpha. It is a model for defining colors in CSS based on how humans perceive color, making it very intuitive for design work.
It is an extension of the HSL (Hue, Saturation, Lightness) model that includes an Alpha channel for transparency.
🌈 Components of HSLA:
The hsla() function takes four values to define the final color:
| Component |
Range |
Unit |
Description |
| H(Hue) |
0 to 360 |
Degrees (deg or unitless) |
The actual color tone (position on the color wheel). 0 is Red, 120 is Green, 240 is Blue. |
| S(Saturation) |
0% to 100% |
% |
The intensity or purity of the color. 100% is pure color; 0% is gray (grayscale). |
| L(Lightness) |
0% to 100% |
% |
How light or dark the color is. 0% is pure black; 100% is pure white; 50% is the "normal" color. |
| A(Alpha) |
0.0 to 1.0 |
Decimal |
The transparency level. 1.0 is fully opaque (solid); 0.0 is fully transparent (invisible). |
Example:
<style>
.main-color {
background-color: hsla(120, 100%, 50%, 1.0);
}
.lighter-shade {
background-color: hsla(120, 100%, 75%, 1.0);
}
.transparent-shade {
background-color: hsla(120, 100%, 50%, 0.4);
}
</style>
<div class="main-color">Green: Hue 120, fully saturated 100%, normal lightness 50%, fully opaque 1.0 </div>
<div class="lighter-shade">Lighter Green: Same Hue (120), but Lightness increased to 75%
</div>
<div class="transparent-shade"> Transparent Green: Same color, but Alpha reduced to 0.4
</div>
Output:
Green: Hue 120, fully saturated 100%, normal lightness 50%, fully opaque 1.0
Lighter Green: Same Hue (120), but Lightness increased to 75%
Transparent Green: Same color, but Alpha reduced to 0.4
Transparency on images:
When it comes to transparency on images in web development, we're typically talking about two main scenarios:
-
Making an image itself transparent (or semi-transparent).
-
Using images that already have transparency built into them (like PNGs with transparent backgrounds).
You can control the overall transparency of an entire <img> element using the opacity CSS property. This works the same way it does for any other HTML element.
How it works:
-
The opacity property takes a value between 0.0 (fully transparent/invisible) and 1.0 (fully opaque/solid).
-
Setting opacity on an <img> tag will make the entire image (all its pixels) fade.
Example:
<style>
/* Makes the image 50% transparent */
.semi-transparent-image {
opacity: 0.5;
}
/* Makes the image completely invisible */
.invisible-image {
opacity: 0;
}
</style>
Original Image:
<img src="logo.png">
Semi-transparent Image:
<img src="logo.png" class="semi-transparent-image">
Invisible-image:
<img src="logo.png" class="invisible-image">
Output:
Original image:

Semi transparent image:
Invisible-image: