CSS Colors
CSS colors are used to define the color of text, backgrounds, borders, and many other elements on a web page. CSS provides multiple ways to specify colors, giving developers great flexibility in design.
Where CSS Colors Are Used
- Text color
- Background color
- Border color
- Outline color
- Box shadow and gradients
Color Names
CSS supports predefined color names such as red, blue, green, and black. Color names are easy to use but limited in variety.
HEX Color Values
HEX colors are written using a hash (#) followed by six hexadecimal characters.
Each pair represents the intensity of Red, Green, and Blue colors.
Hex values range from 00 (minimum) to FF (maximum),
which equals 0 to 255 in decimal.
HEX Format
#RRGGBB
RR = Red
GG = Green
BB = Blue
Examples
#ff0000 → Red
#00ff00 → Green
#0000ff → Blue
#ffffff → White
#000000 → Black
Color Mixing Example
#ff00ff → Red + Blue = Purple
#ffff00 → Red + Green = Yellow
Short HEX Values (3-Digit Format)
If both digits in each pair are the same, you can use the shorter 3-digit format. Each digit is automatically doubled.
#f00 → #ff0000
#0f0 → #00ff00
#00f → #0000ff
#fff → #ffffff
#333 → #333333
RGB Color Values
RGB stands for Red, Green, and Blue. Each value ranges from 0 to 255.
RGBA Color Values
RGBA colors add an alpha channel to control transparency. The alpha value ranges from 0 (transparent) to 1 (opaque).
HSL Color Values
HSL stands for Hue, Saturation, and Lightness. It provides a more intuitive way to control colors.
HSLA Color Values
HSLA is similar to HSL but includes transparency.
Background Color
The background-color property is used to set
the background color of an element.
Text Color
The color property defines the color of text.
Best Practices for Using Colors
- Maintain good contrast for readability
- Use consistent color themes
- Avoid using too many colors
- Test colors for accessibility
Common Mistakes with CSS Colors
- Low contrast between text and background
- Overusing bright colors
- Using color alone to convey meaning