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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the semi-transparent box demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule that sets the box size, colors and opacity. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
.demo-box targets the box. width: 220px fixes its width, and padding: 20px keeps the text away from the edges. These two lines do not affect the transparency, but they make the box big enough to see.
On their own these lines would give a solid blue box with white text. The next line then fades both of them at once.
opacity takes a number from 0 to 1. 1 is fully solid (the default), 0 is invisible, and 0.5 is half see-through. The page behind shows through, so the blue looks light. This property fades the box and its text together, so the white text also becomes faint, and its contrast with the background drops.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one box.
A div with the demo-box class. The text inside is a child of the box, so it is faded by the same 50%. You cannot make the text solid again with a child rule, because opacity cannot be undone for children.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the image transparency demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has one rule for the image. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The image is shown 300px wide. The source photo is also 300px wide, so it is displayed at its natural size, and the height follows its ratio.
opacity: 0.6 shows the image at 60% strength and lets 40% of the background through. Lower values make the picture fainter, and values near 1 change it very little. Faded images are often used as backgrounds, so that text on top stays readable.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one image.
class="demo-image" applies the rule. src loads a 300 by 180 sample photo, and alt describes it for screen readers. Opacity does not affect the alt text, so keep it meaningful.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the opacity hover effect demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has a normal rule and a hover rule for the image. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
In its normal state the image is 300px wide and 50% transparent.
transition: 0.4s makes every property that changes, here the opacity, animate over 0.4 seconds instead of jumping at once. It is written in the normal state, so the animation plays both when the mouse enters and when it leaves.
:hover applies only while the mouse pointer is over the image. opacity: 1 makes it fully solid. Only this one property is changed, and everything else comes from the normal rule.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one image.
The image has the demo-image class, so it gets both the faded state and the hover effect. Touch screens have no real hover, so do not hide important content this way.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the transparent button demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo styles a button and its hover state. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
padding: 12px 22px makes the button roomy, and background: #28a745 gives it a green color.
White text for contrast, and border: none to remove the default border. opacity: 0.7 shows the button at 70% strength, so it looks slightly softened. The label is faded too, because opacity affects the whole button.
When the mouse is over the button it becomes fully solid. Without a transition the change is instant. Add transition: 0.3s to the normal rule to make it smooth.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: one button.
A normal button with the demo-button class. Move the pointer onto it and off it in the preview.
</body> closes the body.
</html> is the final closing tag and ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the transparent card demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets the font and 24px of padding. The light blue page background (#f0f4ff) is what shows through the card, which makes the transparency visible.
The card is 260px wide, with 20px of space inside it.
A blue card with white text. opacity: 0.8 shows the card at 80% strength, so the page color shows through a little. Because 0.8 is close to 1, the text is still easy to read, but much lower values would make the white text hard to see.
</style> closes the CSS.
</head> closes the head. The visible content comes next.
<body> starts the visible content: one card with a heading and a paragraph.
The div is the card container. Everything inside it is faded together.
The heading is a child of the card, so it is drawn at 80% strength too.
The paragraph is faded by the same amount. A child can never be more opaque than its parent.
</div> closes the card.
</body> closes the body.
</html> ends the document.
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> tells the browser to use modern HTML5 rules. Without it the browser can switch to quirks mode, where box sizes and floats may not behave the way this lesson shows.
<html> wraps the whole page. Everything for the opacity demo demo sits between this tag and the closing </html>.
<head> holds information about the page rather than visible content: the character encoding and the CSS rules that control the floated elements.
<meta charset="utf-8"> makes the browser read the file as UTF-8, so letters and symbols display correctly.
<style> opens the internal CSS. This demo has rules for an image and a button, each with a hover state. Every class starts with demo-, so these rules can never change the tutorial page around the preview.
The body rule sets a readable font and adds padding: 24px so the demo does not touch the edge of the preview window. It only affects this small preview page, not the tutorial.
The image is 300px wide and shown at 50% strength.
transition: 0.5s animates the opacity change over half a second, so the image fades in and out smoothly.
While the mouse is over the image it becomes fully solid.
margin-top: 20px adds space between the image and the button. It works because a button is an inline-block element. padding: 12px 22px makes the button roomy.
A blue button with white text and no border, drawn at 70% strength. The text is faded too.
The button becomes solid on hover. There is no transition here, so the change is instant, unlike the image.
</style> closes the CSS. The browser now has all the rules it needs for the float demo.
</head> closes the head. The visible content comes next, inside <body>.
<body> starts the visible content: an image, a line break and a button.
A 300 by 180 sample photo with the demo-image class.
<br> moves the button to the next line. The gap between them comes from the button’s margin-top.
A button with the demo-button class.
</body> closes the body.
</html> is the final closing tag and ends the document.
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.