Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

We use the CSS opacity property to describe the transparency of an element. The value of opacity lies between 0.0 to 1.0 where the low value represents high transparency and the high value represents low transparency.

The opacity property is applied to an element and changes the opacity of the element’s content as well. To change the opacity of the background color without affecting the content we will use the background property.

Approach to Set CSS Background Color Opacity Without Affecting Text

To set the opacity only to the background color and not the text inside it we use the RGBA color values instead of the opacity property because using the opacity property can change the opacity of the text inside the element.

CSS background property provides rgba color notations, which we will be using to change background color opacity.

Let’s see the syntax and example of how to use rgba() method of background property to set CSS background color opacity.

Syntax:

background: rgba(red, green, blue, alpha)

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: This example demonstrates the use of rgba values of background property to set the opacity of the background color and not the text.

html
<!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: using rgba color values to change background color opacity output

CSS is the foundation of webpages and 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.

Note: To change background image opacity CSS without affecting text, we can create a separate div for content, and keep image in it’s parent div. Change image div opacity using opacity property and use z-indexing to place content div at desired postion.

Summing Up

In this guide, we have explained how users can change the opacity of the background color without affecting the text or content. We have used ‘rgba‘ values to set the opacity of the background color, but users can also use the ‘hsla‘ values to perform this task. To use hsla values we use background-color property [ background-color: hsla(0, 100%, 30%, opacity)].

CSS background property allows to use rgba() color notations. Using RGB values we can set the background color, and the alpha component adjusts the opacity of the background color.



Last Updated : 15 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads