Named, hex and RGB colors
Definition and purpose
CSS colors describe foregrounds, backgrounds and other painted features. Named colors, hexadecimal notation and rgb() can specify the same sRGB color.
Equivalent reds
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>#a { color: red; } #b { color: #f00; }</style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>Both resolve to the same red.
Syntax and mental model
#RRGGBB uses two hexadecimal digits per channel; #RGB duplicates each digit. rgb(255 0 0) is red. A slash introduces alpha in modern rgb() syntax; #RRGGBBAA places alpha last. Alpha 0 is transparent and 1 is opaque.
Translucent background
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { background-color: rgb(0 0 255 / 50%); color: black; } #b { background-color: #0000ff; } </style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>Only the first background has half alpha; its text remains opaque.
Common mistakes
Do not read the final hex pair as blue in eight-digit notation. These color samples illustrate notation, not recommended text palettes. Check foreground contrast against the actual background.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Other dimensions and URL values. Next lesson: HSL and alpha compositing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Named, hex and RGB colors — knowledge check
Five questions on named, hex and rgb colors.
What does #abc expand to?
Which is full-intensity blue?
What is the final pair in #11223380?
What does alpha 0 mean?
What is needed to assess readable text?