Set the opacity only to background color not on the text in CSS - GeeksforGeeks

30 Jul, 2021
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 the low value represents high transparent and high value represents low transparent. The percentage of opacity is calculated as Opacity% = Opacity * 100To set the opacity only to the background and not the text inside it. It can be set by using the RGBA color values instead of the opacity property because using the opacity property can make the text inside it fully transparent element.
Syntax:
element {
    background: rgba(red, green, blue, alpha);
    // CSS property
}

Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255. The RGBA color values are an extension of RGB color values with an alpha channel which specifies the opacity for a color. The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).
Example:
<!DOCTYPE html><html>    <head>        <title>rgba property</title>        <style>            h1 {                color: green;            }            h1, h2 {                text-align: center;            }            div {                background: rgb(0, 151, 19);                padding: 10px;                text-align:justify;             }                  div.first {                /*setting alpha = 0.1*/                background: rgba(0, 151, 19, 0.1);            }                  div.second {                /*setting alpha = 0.3*/                background: rgba(0, 151, 19, 0.3);            }                  div.third {                /*setting alpha = 0.6*/                background: rgba(0, 151, 19, 0.6);            }        </style>    </head>    <body>        <h1>GeeksforGeeks</h1>        <h2>Using RGBA color values:</h2>        <div class="first">            <p>This paragraph is shown at 10% opacity.</p></div>        <div class="second">            <p>This paragraph is shown at 30% opacity.</p></div>        <div class="third">            <p>This paragraph is shown at 60% opacity.</p></div>        <div><p>This is default.</p></div>    </body></html>                    
Output:
HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
CSS-Misc
CSS
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
How to create footer to stay at the bottom of a Web page?
How to update Node.js and NPM to next version ?
Types of CSS (Cascading Style Sheet)
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
How to set the default value for an HTML <select> element ?
How to update Node.js and NPM to next version ?
How to set input type date in dd-mm-yyyy format using HTML ?