Tutorialspoint.dev

CSS | Opacity / Transparency

The opacity is the property of an element which describes the transparency of the element.

Image Opacity: The opacity property is used in the image to describes the transparency of the image. The value of opacity lies between 0.0 to 1.0 where a low value represents high transparent and high value represents low transparent. The percentage of opacity is calculated as Opacity% = Opacity * 100

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Opacity property</title>
        <style>
        .forest {
            opacity: 0.5;
        }
        p {
            font-size:25px;
            font-weight:bold;
            margin-bottom:5px;
        }
        .opacity {
            text-align:center;
        }
        </style>
    </head>
    <body>
        <div class = "opacity">
            <p>Image with 100% opacity (original image)</p>
            <img src=
 class="forest1">
            <br><br>
            <p>Image with 50% opacity</p>
            <img src=
class="forest">
        </div>
    </body>
</html>                    

Output:
image opacity

Image Hover Opacity: The hover opacity property is applied on the image when mouse put over the image otherwise opacity property change.
The value of opacity can easily reverse the process by setting the opacity as a higher value at first and then lowering it when hovering over it like:



.hightolow {
    opacity: 1.0;
}

.hightolow:hover {
    opacity: 0.5;
}

Example:

<!DOCTYPE html>
<html>
    <head>
        <title>Image Hover Opacity</title>
        <style>
            .gfg_opacity {
                opacity: 0.5;
            }
              
            .gfg_opacity:hover {
                opacity: 1.0;
            }
            .main {
                text-align:center;
            }
        </style>
    </head>
    <body>
        <div class ="main">
            <h1>Image Hover Opacity:</h1>
            <img src=
class="gfg_opacity"><br>
            <img src=
class="gfg_opacity"><br><br>
        </div>
    </body>
</html>                    

Output:
hover image opacity

Transparency box and transparency using RGBA values: In transparency box, child property inherit the property from the parent property but in case of transparency using RGBA, only opacity property is used or applied to add transparency to the background of an element.

Example:

<!DOCTYPE html>
<html>
<head>
    <title>Transparent box</title>
    <style>
        .geeks {
            background: rgb(0, 153, 0);
            padding: 15px;
            text-align:center;
            width:300px;
        }
  
        #geek {
            padding: 15px;
            text-align:center;
            width:300px;
        }
        .rgba1 {
            background: rgba(0, 153, 0, 0.1);
        }
          
        .rgba2 {
            background: rgba(0, 153, 0, 0.5);
        }
          
        .rgba3 {
            background: rgba(0, 153, 0, 0.8);
        }
        .rgba4 {
            background: rgba(0, 153, 0, 1.0);
        }
        .g1 {
            float:left;
            margin-left:50px;
        }
        .g2 {
            margin-top:-40px;
            margin-left:50px;
            float:left;
        }
    </style>
</head>
<body>
<div class ="g1">
    <p style = "font-size:24px;font-weight:bold;">Transparent Box</p>
    <div class = "geeks" style="opacity:0.1;"><p>10% opacity</p></div>
    <div class = "geeks" style="opacity:0.5;"><p>50% opacity</p></div>
    <div class = "geeks" style="opacity:0.8;"><p>80% opacity</p></div>
    <div class = "geeks"><p>100% opacity</p></div></div><br><br>
      
 <div class = "g2">
    <p style = "font-size:24px;font-weight:bold;">RGBA color values</p>
    <div class="rgba1" id = "geek"><p>10% opacity</p></div>
    <div class="rgba2" id = "geek"><p>50% opacity</p></div>
    <div class="rgba3" id = "geek"><p>80% opacity</p></div>
    <div class="rgba4" id = "geek"><p>100% opacity</p></div></div>
</body>
</html>                                    

opacity

Text In Transparent Box: The opacity property can be used to decrease or increase the opacity of a box and put text inside of it for making the most egregious posts.

Example:

<!DOCTYPE html>
<html>
    <head>
        <style>
        div.bg {
            background: url("
            width:550px;
            height:300px;
            border: 1px solid;
        }
          
        div.box {
            margin:50px 20px;
            text-align:center;
            width:500px;
            height:150px;
            background-color: #000000;
            border: 3px solid white;
            opacity: 0.7;
        }
          
        div.box p {
            margin: 5%;
            font-family: Arial;
            color:#009900;
            font-weight: bold;
            font-size: 25px;
        }
        </style>
    </head>
    <body>
        <div class="bg">
            <div class="box">
                <p>GeeksforGeeks</p>
            </div>
        </div>
    </body>
</html>                    

Output:
text-box-opacity



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter