Introduction to CSS opacity usage
CSS opacity usage introduction

What is CSS opacity?
The opacity property in CSS is used to set an element’s opacity level. This property controls the element’s transparency, with a value range of 0 to 1. 0 represents complete transparency (i.e., the element is invisible), while 1 represents complete opacity (i.e., the element is completely visible). By varying the opacity of an element, you can create special effects such as fading in and out and semi-transparent backgrounds.
Using opacity
Using the opacity property is simple: simply assign a value between 0 and 1 to an element. Here is a simple example:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 200px;
height: 200px;
background-color: red;
opacity: 0.5;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
In this example, we create a red div element and set its opacity property to 0.5, making it semi-transparent. Running the above code, you’ll see a semi-transparent red square appear on the page.
Notes on opacity
While the opacity property is very convenient and useful, there are some caveats.
Affecting Child Elements
Note that setting the opacity property on an element affects all of its child elements. This means that child elements will inherit the same opacity and be affected when displayed. For example:
<!DOCTYPE html>
<html>
<head>
<style>
div.container {
width: 300px;
height: 200px;
background-color: yellow;
opacity: 0.5;
}
div.child {
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div class="container">
<div class="child"></div>
</div>
</body>
</html>
In the above code, we create a yellow parent div element and set its opacity property to 0.5. A red child div element is nested within the parent element. After running the code, you’ll see that the child elements are also affected by the parent element’s transparency, appearing as a semi-transparent red.
Affecting Text Content
This affects not only the element itself and its child elements, but also the text within them. Setting the opacity property on an element also affects the text within it. This means the text will also become semi-transparent. For example:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: cyan;
opacity: 0.5;
}
</style>
</head>
<body>
<div>
<h1>Hello World!</h1>
<p>This is a paragraph.</p>
</div>
</body>
</html>
In the above example, we set the opacity property of a div element to 0.5 and insert an h1 heading and a p paragraph within it. After running the code, you can see that the text content also becomes semi-transparent.
If you want to make only the background of an element semi-transparent without affecting the text itself, you can use RGBA color values. For example:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: rgba(0, 255, 0, 0.5);
}
</style>
</head>
<body>
<div>
<h1>Hello World!</h1>
<p>This is a paragraph.</p>
</div>
</body>
</html>
In this example, we set the background color of the div element to rgba(0, 255, 0, 0.5), where 0.5 indicates transparency. After running the code, you’ll see that only the background of the element becomes semi-transparent, while the text itself remains unchanged.
Combining opacity with other CSS properties
Combining with the z-index property
Combining the opacity property with the z-index property can create the illusion of depth. The z-index property defines the stacking order of elements, with higher values placing them at the front. Combined with the opacity property, you can create a fade-in/fade effect.
<!DOCTYPE html>
<html>
<head>
<style>
div {
position: absolute;
width: 200px;
height: 200px;
}
div.move {
background-color: red;
opacity: 0;
z-index: 1;
animation: fadein 3s forwards;
}
@keyframes fadein {
from {opacity: 0;}
to {opacity: 1;}
}
div.stay {
background-color: blue;
z-index: 2;
}
</style>
</head>
<body>
<div class="move"></div>
<div class="stay"></div>
</body>
</html>
In the above code, we create two div elements: a red, moving div (class “move”) with a fading effect, and a blue, fixed div (class “stay”). After running the code, you’ll see that the red div fades in over 3 seconds and appears behind the blue div, creating a sense of depth.
Combining the background property
The opacity property can be used in conjunction with the background property to create a semi-transparent background effect. This effect is achieved by setting the background color and opacity of the element.
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 200px;
height: 200px;
background: rgba(0, 0, 255, 0.5);
}
</style>
</head>
<body>
<div></div>
</body>
</html>
In the above code, we set the background color of a div element to rgba(0, 0, 255, 0.5), where 0.5 indicates transparency. After running the code, you can see that the div element has a semi-transparent blue background.
Summary
CSS The opacity property can be used to control the transparency of an element, with a value range of 0 to 1. By setting the level of opacity, various special effects can be achieved, such as fading in and out, translucent backgrounds, etc. However, it should be noted that elements with the opacity property set will affect the transparency of child elements and text content. To make only the background of an element translucent, you can use RGBA color values. You can combine other CSS properties, such as z-index and background, to achieve more complex effects.