Academic Block

CSS Opacity Get Certified

What is CSS Opacity?

The CSS opacity property controls the transparency of an element. Values range from 0 (completely transparent) to 1 (completely opaque). It affects the element and all its children.

Step 1: Make an Element Semi-Transparent

What this code does: Makes a blue box 50% see-through with opacity: 0.5. The browser draws the whole box, including its white text, at half strength, so the box looks light blue and the text looks faint against the white page. Notice in the preview that the text fades too, not only the background: opacity affects the element and everything inside it.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-box {
    width: 220px;
    padding: 20px;
    background: #007BFF;
    color: white;
    opacity: 0.5;
}
</style>
</head>
<body>
<div class="demo-box">This box is 50% transparent.</div>
</body>
</html>

Step 2: Image Transparency

What this code does: Fades an image by drawing it at 60% strength, so the white page shows through and the picture looks washed out. The image file is not changed: opacity only affects how the browser displays it, and removing the rule brings the full colours back.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-image {
    width: 300px;
    opacity: 0.6;
}
</style>
</head>
<body>
<img class="demo-image" src="https://picsum.photos/300/180" alt="Sample Image">
</body>
</html>

Step 3: Hover Effect Using Opacity

What this code does: Shows an image faded at 50% and brings it to full strength when the mouse is over it, fading in smoothly over 0.4 seconds. Move the pointer onto the picture and away again in the preview to watch it fade in and out. This “dim until hovered” effect is common in image galleries.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-image {
    width: 300px;
    opacity: 0.5;
    transition: 0.4s;
}
.demo-image:hover {
    opacity: 1;
}
</style>
</head>
<body>
<img class="demo-image" src="https://picsum.photos/300/180" alt="Nature">
</body>
</html>

Step 4: Transparent Button

What this code does: Draws a green button at 70% strength that becomes fully solid when you hover over it. The slightly faded button in its normal state makes the hover change easy to notice, as a signal that the button can be clicked. Note that this button has no transition, so the change is instant.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-button {
    padding: 12px 22px;
    background: #28a745;
    color: white;
    border: none;
    opacity: 0.7;
}
.demo-button:hover {
    opacity: 1;
}
</style>
</head>
<body>
<button class="demo-button">Hover Me</button>
</body>
</html>

Step 5: Transparent Card

What this code does: Creates a blue card at 80% strength on a light blue page. The whole card, including its heading and paragraph, is slightly faded, because opacity affects every child. If you want a see-through background with solid text, use an rgba() background colour instead of opacity.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
    background: #f0f4ff;
}
.demo-card {
    width: 260px;
    padding: 20px;
    background: #007BFF;
    color: white;
    opacity: 0.8;
}
</style>
</head>
<body>
<div class="demo-card">
<h3>Welcome</h3>
<p>This card uses CSS opacity.</p>
</div>
</body>
</html>

Complete CSS Opacity Example

What this code does: Builds a small interactive demo with a faded image and a faded button. Both become fully solid when you hover over them. The image fades in over half a second because it has a transition, while the button changes instantly because it has none. Hover over each one to compare.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
.demo-image {
    width: 300px;
    opacity: 0.5;
    transition: 0.5s;
}
.demo-image:hover {
    opacity: 1;
}
.demo-button {
    margin-top: 20px;
    padding: 12px 22px;
    background: #007BFF;
    color: white;
    border: none;
    opacity: 0.7;
}
.demo-button:hover {
    opacity: 1;
}
</style>
</head>
<body>
<img class="demo-image" src="https://picsum.photos/300/180" alt="Landscape">
<br>
<button class="demo-button">Learn CSS</button>
</body>
</html>

Advantages of CSS Opacity

Advantage
Benefit
Description
Modern Design
Professional Look
Creates clean transparency effects.
Interactive UI
Better User Experience
Works well with hover animations.
Easy to Use
Simple CSS
Requires only one CSS property.
Responsive
Works Everywhere
Supported by all modern browsers.

Best Practices

  • Use opacity values only between 0 and 1.
  • Combine opacity with transition for smooth hover effects.
  • Avoid making important text too transparent.
  • Use opacity for decorative effects, overlays, and image galleries.
  • Remember that opacity affects the entire element and its children.
  • Test transparency on different background colors.
  • Keep sufficient contrast for accessibility.
  • Use RGBA colors instead of opacity when only the background should be transparent.

Web Resources on CSS Opacity

1. MDN Web Docs : opacity CSS property
2. MDN Web Docs : opacity() CSS function
3. CSS Tricks.com : opacity
4. Dev.to : How to Change Background Opacity without Affecting Text

🧪 Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

📝 CSS Code
👁️ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.