October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
ThatPainter
accessibility

HTML RGB and RGBA Colors: A Practical CSS Guide

Learn how CSS rgb() and rgba() work, when to use modern space-and-slash syntax or legacy comma syntax, and how alpha affects transparency. Includes practical examples and guidance for checking color contrast.

By ThatPainter Team 4 min read
Worth Installing

Crashes, No Sound, or Screen Glitches?

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Outbyte Driver Updater · freeRandom freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteFind the Right Drivers →

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

ThatPainter is reader-supported. When you buy through links on our site, we may earn an affiliate commission. Learn More

In CSS, rgb() sets a color with red, green, and blue components; it can also include alpha to control transparency. rgba() is an alias for rgb(), so the practical difference is syntax: modern CSS uses space-separated channels and an optional slash before alpha, while the older form uses commas.

To set transparency with modern rgb(), write a slash followed by an alpha value: rgb(255 0 0 / 0.5) is red at 50% opacity. If the color is fully opaque, omit alpha.

What RGB and RGBA mean

RGB stands for red, green, blue. CSS rgb() expresses a color in the sRGB color space. Each channel can be a number from 0 through 255 or a percentage from 0% through 100%.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Alpha controls transparency, not the red, green, or blue components. An alpha value of 0 is fully transparent; 1 or 100% is fully opaque. If alpha is omitted, the color is fully opaque. MDN describes rgba() as an alias for rgb().

Notation Example Use
Modern, opaque rgb(40 80 120) Recommended for new CSS
Modern, translucent rgb(40 80 120 / 0.35) Use a slash before alpha
Legacy, opaque rgb(40, 80, 120) Recognize in existing stylesheets
Legacy, translucent rgba(40, 80, 120, 0.35) Recognize in existing stylesheets; rgba() is an alias

Modern RGB syntax

Modern CSS separates the three channels with spaces. When you include alpha, put a slash before it:

rgb(255 0 0) is opaque red. rgb(255 0 0 / 0.5) is red at 50% opacity. You can also write the channels and alpha as percentages: rgb(100% 0% 0% / 50%).

For new examples, MDN recommends the modern syntax and the shorter rgb() name. Omit alpha when it is fully opaque.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Legacy comma-separated syntax

Older CSS commonly separates channels with commas. For example, rgb(255, 0, 0) is opaque red, while rgba(255, 0, 0, 0.5) is red at 50% opacity. This legacy syntax remains documented; it is useful to recognize when maintaining existing CSS.

Rank #2
Sale
Charlotte's Web: A Newbery Honor Award Winner – The Beloved Classic Novel About a Pig, a Spider, and the Power of Friendship
  • These are the words in Charlotte's web, high in the barn
  • Her spiderweb tells of her feelings for a little pig named Wilbur, as well as the feelings of a little girl named Fern … who loves Wilbur, too
  • Their love has been shared by millions of readers

Do not mix the two forms. Use either space-separated channels with a slash before alpha, or the complete comma-separated form.

Understanding alpha and transparency

Alpha affects how a color blends with what is behind it. It does not turn the color into one fixed, lighter opaque color: the visible result depends on the background.

Alpha value Meaning
0 or 0% Fully transparent
0.25 or 25% 25% opaque
0.5 or 50% 50% opaque
0.75 or 75% 75% opaque
1 or 100% Fully opaque

For example, rgb(255 0 0 / 0.2) is 20% opaque and 80% transparent. Alpha can be useful for translucent backgrounds, borders, shadows, and overlays.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Using RGB colors in HTML

RGB is a CSS color value, not an HTML element or special HTML color attribute. Apply it in a style attribute, an embedded stylesheet, or an external CSS file.

Inline CSS

<p style="color: rgb(34 34 34);">Dark gray text</p>

Inline styles are convenient for small demonstrations, but repeated colors are harder to maintain this way.

Embedded or external CSS

In a stylesheet, declare the color on a selector. For example: .notice { color: rgb(20 40 80); background-color: rgb(80 160 240 / 0.2); }. Use a shared stylesheet when you want styles to be reusable.

RGB and hexadecimal colors

The same opaque red can be written as rgb(255 0 0) or #ff0000. CSS hexadecimal colors can also include alpha: the eight-digit form is #RRGGBBAA, with the alpha component last. For example, #ff000080 is approximately 50% alpha.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For more on hexadecimal syntax, see MDN’s hexadecimal color reference.

Transparency and accessible color choices

A translucent color looks different over different backgrounds. Check text and background colors in their actual rendered context, including when a background is an image or changes across a page.

  • Test the complete text-and-background combination, not just the foreground color.
  • Check hover and keyboard focus states separately.
  • Do not use transparency to conceal low-contrast text.
  • Keep focus indicators clearly visible.

MDN’s CSS style guidance recommends aiming for at least a 4.5:1 contrast ratio between text and its background. Alpha alone does not guarantee accessible contrast; the final composition matters.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistakes

Using RGB as though it were an HTML element

RGB belongs in a CSS declaration, such as color: rgb(255 0 0);, not as an HTML tag.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Adding alpha without a separator

In modern syntax, use a slash before alpha: rgb(255 0 0 / 0.5). In legacy syntax, use the complete comma-separated form: rgba(255, 0, 0, 0.5).

Mixing modern and legacy syntax

Keep each color function consistent: use spaces and slash-alpha together, or commas throughout the legacy form.

Assuming alpha lightens a color

Alpha controls transparency. The resulting appearance depends on the color behind it.

FAQ

What is the difference between RGB and RGBA in CSS?

There is no separate color model: rgba() is an alias for rgb(). The name rgba() is commonly seen in legacy comma-separated syntax; modern syntax can include alpha in rgb().

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How do I set transparency with rgb() in CSS?

Use a slash and an alpha value, such as rgb(255 0 0 / 0.5) for red at 50% opacity. Alpha can be a number from 0 to 1 or a percentage from 0% to 100%.

What happens if I leave out alpha?

The color is fully opaque. For new CSS, MDN recommends omitting alpha when it is 1.

Should I use commas or spaces in rgb()?

For new code, use space-separated channels and a slash before alpha when needed. Comma-separated syntax remains useful to recognize in existing code; do not mix the forms.

Is every newer CSS color feature supported everywhere?

Basic RGB is widely available, but newer parts of CSS color syntax can have different browser support. Check compatibility for the specific feature you plan to use rather than assuming all color features have the same support.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

More from the Paint Desk

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.