The Wayback Machine - https://web.archive.org/web/20250115225441/https://www.geeksforgeeks.org/css-rgba-function/
Open In App

CSS rgba() Function

Last Updated : 30 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The rgba() function is an inbuilt function in CSS that is used to define the colors using the Red-Green-Blue-Alpha (RGBA) model. It is an extension of rgb() color values containing an alpha channel that specifies the transparency of color.

Syntax:

rgba( red, green, blue, alpha )

Parameters: This function accepts 4 parameters as described below:

  • red: This parameter is used to define the intensity of the red color. It is an integer value that lies between 0 to 255, or as a percentage value between 0% to 100%.
  • green: This parameter is used to define the intensity of the green color. It is an integer value that lies between 0 to 255, or as a percentage value between 0% to 100%.
  • blue: This parameter is used to define the intensity of the blue color. It is an integer value that lies between 0 to 255, or as a percentage value between 0% to 100%.
  • alpha: This parameter is used to define the opacity and the value lies between 0.0 (completely transparent) to 1.0 (completely opaque).

Example: This example illustrates the rgba() function where different color intensities (ie., red, blue & green), are displayed with the specific value for each color, along with the alpha value for setting the opacity of the color.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>rgba function</title>
    <style>
        .gfg1 {
            background-color: rgba(1, 153, 0, 0.5);
            text-align: center;
        }

        .gfg2 {
            background-color: rgba(0, 255, 0, 0.5);
            text-align: center
        }

        .gfg3 {
            background-color: rgba(133, 150, 150, 0.5);
            text-align: center
        }

        .gfg {
            font-size: 40px;
            font-weight: bold;
            color: green;
            text-align: center;
        }

        h1 {
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <h1>The rgba() Function</h1>
  
    <p class="gfg1">Green</p>
    <p class="gfg2">Light green</p>
    <p class="gfg3">Light black</p>

</body>

</html>

Output:

Supported Browsers: The browser supported by rgba() function are listed below: 

  • Chrome
  • Edge
  • Firefox
  • Safari
  • Opera

CSS rgba() Function – FAQs

What does the rgba() function do in CSS?

The rgba() function defines colors using the Red, Green, and Blue color model with an additional Alpha channel for transparency.

How do you use the rgba() function in CSS?

Define colors using rgba(red, green, blue, alpha); where the alpha value ranges from 0 (fully transparent) to 1 (fully opaque).

What are common uses of the rgba() function?

Common uses include creating semi-transparent overlays, background colors, borders, and other visual effects where transparency is needed.

Can rgba() be used with percentages for color values?

No, rgba only accepts numerical values between 0 and 255 for the RGB components, and a decimal value between 0 and 1 for the alpha channel.

Is rgba() widely supported across browsers?

Yes, rgba is supported in all modern browsers, making it a reliable choice for handling colors with transparency in web design.

Dive deep into the world of JavaScript with our JavaScript Full Course Online, offering everything from the basics to advanced techniques. Learn how to build interactive websites, optimize your code, and earn a certification to showcase your skills.

Take the Three 90 Challenge! Complete 90% of the course in 90 days, and earn a 90% refund. Push your limits and stay motivated as you work towards mastering JavaScript. Join now and get started on your JavaScript learning journey!


Next Article

Similar Reads

three90RightbarBannerImg