RGB and RGBA let you pick colors by mixing red, green, and blue light. RGBA adds transparency so you can see through colors.
RGB and RGBA in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
rgb(red, green, blue) rgba(red, green, blue, alpha)
Red, green, and blue values go from 0 to 255.
Alpha in rgba is a number from 0 (fully transparent) to 1 (fully opaque).
background-color: rgb(255, 0, 0);color: rgba(0, 128, 0, 0.5);
border-color: rgb(0, 0, 255);background-color: rgba(255, 165, 0, 0.3);
This page shows three colored boxes. The first uses rgb for a solid red color. The second uses rgba for a green color that is half see-through. The third uses rgba for a blue color that is mostly see-through. Each box has some padding and rounded corners for a neat look.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>RGB and RGBA Example</title> <style> body { font-family: Arial, sans-serif; margin: 2rem; } .box1 { background-color: rgb(255, 0, 0); color: white; padding: 1rem; margin-bottom: 1rem; border-radius: 0.5rem; } .box2 { background-color: rgba(0, 128, 0, 0.5); color: black; padding: 1rem; margin-bottom: 1rem; border-radius: 0.5rem; } .box3 { background-color: rgba(0, 0, 255, 0.3); color: black; padding: 1rem; border-radius: 0.5rem; } </style> </head> <body> <section class="box1" aria-label="Bright red background"> This box has a bright red background using rgb(255, 0, 0). </section> <section class="box2" aria-label="Semi-transparent green background"> This box has a semi-transparent green background using rgba(0, 128, 0, 0.5). </section> <section class="box3" aria-label="Light transparent blue background"> This box has a light transparent blue background using rgba(0, 0, 255, 0.3). </section> </body> </html>
Use rgb when you want solid colors without transparency.
Use rgba when you want to add see-through effects to colors.
Values outside 0-255 for rgb or 0-1 for alpha will be ignored or cause unexpected colors.
RGB mixes red, green, and blue light to create colors.
RGBA adds an alpha value for transparency.
Use these to control colors and see-through effects in your webpage design.
Practice
rgba(255, 0, 0, 0.5) color value represent in CSS?Solution
Step 1: Understand RGBA components
The first three numbers (255, 0, 0) represent red, green, and blue. Here, red is full (255), green and blue are zero.Step 2: Interpret the alpha value
The last number 0.5 means 50% transparency, so the color is semi-transparent.Final Answer:
A semi-transparent red color -> Option DQuick Check:
RGBA = red + transparency = semi-transparent red [OK]
- Confusing RGB with RGBA
- Ignoring the alpha transparency value
- Mixing up color order (RGB)
- Thinking 0.5 means fully opaque
Solution
Step 1: Recall RGB syntax
RGB uses three numbers for red, green, and blue. The format is rgb(red, green, blue).Step 2: Check the options
rgb(0, 0, 255) uses rgb with three values and sets blue to 255, which is correct for blue color.Final Answer:
rgb(0, 0, 255) -> Option BQuick Check:
RGB needs 3 values, no alpha [OK]
- Using rgba without alpha value
- Adding extra values in rgb
- Mixing order of colors
- Using rgba with missing parameters
background-color: rgba(0, 128, 0, 0.3);
Solution
Step 1: Identify RGB values
The values (0, 128, 0) mean no red, medium green, no blue, so the color is green.Step 2: Interpret alpha transparency
The alpha value 0.3 means 30% opacity, so the color is mostly transparent but visible.Final Answer:
A semi-transparent green color -> Option AQuick Check:
RGBA = green + 30% opacity = semi-transparent green [OK]
- Assuming alpha 0.3 means fully opaque
- Mixing up RGB color order
- Confusing green with red or blue
- Ignoring alpha value
color: rgba(255, 255, 255, 2);
Solution
Step 1: Check alpha value range
Alpha in rgba must be between 0 (transparent) and 1 (opaque). Here it is 2, which is invalid.Step 2: Verify other parts
RGB values 255 are valid, semicolon is present, and rgba takes 4 parameters, so no other errors.Final Answer:
Alpha value cannot be greater than 1 -> Option CQuick Check:
Alpha must be 0 to 1 [OK]
- Using alpha > 1
- Confusing RGB max values
- Forgetting semicolon (not error here)
- Adding extra parameters
Solution
Step 1: Choose correct function for transparency
Onlyrgba()supports alpha transparency as the fourth parameter.rgb()does not.Step 2: Verify color and transparency
background-color: rgba(255, 0, 0, 0.4); uses red (255, 0, 0) with alpha 0.4, which means 40% opacity (60% transparent), exactly what is needed.Step 3: Check other options
background-color: rgb(255, 0, 0, 0.4); uses rgb with 4 parameters (invalid). background-color: rgba(255, 255, 255, 0.4); uses white color, not red. background-color: rgb(255, 0, 0); opacity: 0.4; sets opacity on the whole element, which affects all content, not just background.Final Answer:
background-color: rgba(255, 0, 0, 0.4); -> Option AQuick Check:
RGBA with alpha = 0.4 for semi-transparent red [OK]
- Using rgb() with alpha value
- Setting opacity on element instead of color
- Choosing wrong color values
- Confusing rgba parameters order
