Definition:
CSS Opacity is also known as CSS transparency. It specifies the transparency of an element. You can apply transparency wherever you needed like at the image, hover the image, text in a transparent box, and so on.
Syntax:
opacity: 0.4;
Image Opacity:
The default opacity value is 1. The opacity value starts from 0.0 to 1. The lower value is the most transparent element.
Source Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
}
</style>
</head>
<body>
<h3>Image Transparent 50% </h3>
<img src="https://www.elsebazaar.com/blog/wp-content/uploads/2019/03/About-Us-image.jpg" alt="Image Opacity" width="170" height="100">
</body>
</html>
Output:
Transparent Image Hover Effect:
It shows a transparent image when the mouse hovers over an image. See the source code with output in the below example.
Source Code:
<!DOCTYPE html>
<html>
<head>
<style>
img {
opacity: 0.5;
}
img:hover {
opacity: 1.0;
}
</style>
</head>
<body>
<h3>Image Transparent is 50% <br> Transparent Hover effect is 100%</h3>
<img src="https://www.elsebazaar.com/blog/wp-content/uploads/2019/03/About-Us-image.jpg" alt="Image Opacity" width="170" height="100">
</body>
</html>
Output:
Transparent Container:
When you use the opacity property to add transparency in the background container, all of its child elements inherit the same transparent. As a result, if you have text inside the container will have the same parent opacity and it will make it a bit hard to read the text.
See an example below of using transparent in parent container and its child text elements also have same transparent.
Source Code:
<!DOCTYPE html>
<html>
<head>
<style>
.title{
text-align: center;
}
div {
background-color: blue;
padding: 5px;
}
div.first {
opacity: 0.3;
}
div.second {
opacity: 0.5;
}
div.third {
opacity: 0.8;
}
</style>
</head>
<body>
<h3 class="title">Transparent Container</h3>
<div class="first"><p>Opacity 0.3</p></div>
<div class="second"><p>Opacity 0.5</p></div>
<div class="third"><p>Opacity 0.8</p></div>
<div><p>opacity 1</p></div>
</body>
</html>
Output:
Transparent Using RGBA
In the above example, both parent and child elements use the same opacity value. If you want the child element not to use its parent element opacity then the best way to use transparent is by using the RGBA value in your parent background element.
Source Code:
<!DOCTYPE html>
<html>
<head>
<style>
.title{
text-align: center;
}
div {
background: rgba(0, 0, 255, 1.0);
padding: 5px;
}
div.first {
background: rgba(0, 0, 255, 0.1);
}
div.second {
background: rgba(0, 0, 255, 0.3);
}
div.third {
background: rgba(0, 0, 255, 0.6);
}
</style>
</head>
<body>
<h3 class="title">Opacity applies only on background element, not at text element</h3>
<div class="first"><p>Opacity 0.1</p></div>
<div class="second"><p>Opacity 0.3</p></div>
<div class="third"><p>Opacity 0.6</p></div>
<div><p>opacity 1</p></div>
</body>
</html>
Output:
You may be interested in the following topics:
- How to deactivate an Instagram account from an app?
- How to delete an Instagram account from an app?
- How to delete an Instagram account?
- When is Father’s Day in Australia in 2024?
- What are the leap years?
- How to uninstall WhatsApp on Android Phone?
- How to transfer WhatsApp from one device to another device on Android Phone?
- How to download or install WhatsApp on Android Phone?
- How to fix you don’t have permission to delete these folders or files?
- What is my IP Address?
- How to cancel your Capcut Subscriptions?
- How to delete your Capcut Account Permanently?
- How to delete your business account in Meta Business Manager?
- How to create a business account on Facebook?
- How to make Google Chrome your default browser?
- How to create a straight line in Premiere Pro and animate it
- Zoom in / Zoom out effect in Adobe Premiere Pro 2021
- Video not showing edges in Premiere Pro
- How to remove the green screen in Premiere Pro 2021
- How to create an unfilled circle in Premiere Pro?
- How to swap faces in Photoshop
- Quickly change hair color in Photoshop
- Create perfect circular shapes around objects in Photoshop
- How to wrap text in a coffee cup in Photoshop
- How to wrap text around the image like Magazines in Photoshop
- Wrap Text around the Image using Pen Tool in Photoshop
- How to disable the “/feed” URL in robots.txt?
- How to wrap text around objects in Photoshop
- How to apply RAIN EFFECTS by using Photoshop – Beginners Tutorial
- Change a Green apple into Red apple in Photoshop 2021 – Quick Tutorial
- How to remove a stain in Photoshop – Quick Tutorial
- How to change any color glasses into black in Photoshop?
- Change Eye Color Step-by-Step Tutorial in Photoshop
- How to remove the fence in Photoshop: Step by Step Tutorial
- The fastest way to change a background color in Photoshop?
- Quickly Change Clothing Color in 1 minute in Photoshop?
- How to change the background color in Photoshop 2021?
- How to draw a spiral in Photoshop 2021?
- How to draw a square spiral in Photoshop 2021?
- How to create a WRONG green screen mp4 video in Photoshop – step by step tutorial?
- How to rotate words in Photoshop?
- How to flip Letters in Photoshop?
- How to add multiple-stroke text effects in Photoshop?
- Double exposure effects in Photoshop
- How to remove pimples using Photoshop?
- Difference between Open Image Vs. Place the Embedded Image in Photoshop.
- How to crop a diagonal shape in Photoshop?
- How to draw a perfect star shape in Photoshop using the Polygon tool?
- How to use crop tools in different ways?
- How to crop a photo in a circle shape in Photoshop?