CSS Colors
Learn how to add colors to text, backgrounds, and borders
🎨 CSS Colors
Colors bring life to your web pages! CSS provides several ways to specify colors for text, backgrounds, borders, and other elements.
/* Different ways to set colors */
h1 {
color: red; /* Color name */
background-color: #ff0000; /* Hex code */
border-color: rgb(255, 0, 0); /* RGB values */
}
Ways to Specify Colors
Color Names
Use predefined color names
color: red;
Hex Colors
Use hexadecimal color codes
color: #ff0000;
RGB Colors
Use red, green, blue values
color: rgb(255, 0, 0);
HSL Colors
Use hue, saturation, lightness
color: hsl(0, 100%, 50%);
🔹 Color Names
CSS provides 147 predefined color names, offering a human‑readable and intuitive way to specify colors without memorizing complex hexadecimal or RGB values. These names range from basic primaries like red, blue, and green to more descriptive hues such as crimson, navy, and forestgreen. While convenient for prototyping and readability, for production‑grade design systems, hex, RGB, or HSL values are often preferred due to their precision and consistency. Nevertheless, color names remain a valuable part of the CSS specification for quick styling and educational purposes.
/* Basic Colors */
.red-text { color: red; }
.blue-text { color: blue; }
.green-text { color: green; }
.yellow-text { color: yellow; }
.purple-text { color: purple; }
.orange-text { color: orange; }
.pink-text { color: pink; }
.brown-text { color: brown; }
/* Shades */
.dark-red { color: darkred; }
.light-blue { color: lightblue; }
.dark-green { color: darkgreen; }
/* Neutral Colors */
.black-text { color: black; }
.white-text { color: white; }
.gray-text { color: gray; }
.silver-text { color: silver; }
Color Examples:
Red text
Blue text
Green text
Purple text
Orange text
Dark red text
Light blue text
🔹 Hex Colors
Hex colors use a # followed by six digits (or three for shorthand) to define RGB values. The format #RRGGBB represents red, green, and blue from 00 to FF. Shorthand like #F0A expands to #FF00AA. This system offers access to over 16 million colors in a compact format. Hex codes are the standard for web design because they are precise, easily copied from design tools, and widely supported. They provide the exact color control needed for branding and detailed interfaces, though they are less human-readable than named colors or HSL.
/* Hex Color Format: #RRGGBB */
.hex-red { color: #FF0000; } /* Pure red */
.hex-green { color: #00FF00; } /* Pure green */
.hex-blue { color: #0000FF; } /* Pure blue */
.hex-white { color: #FFFFFF; } /* White */
.hex-black { color: #000000; } /* Black */
/* Common Hex Colors */
.brand-blue { color: #007cba; } /* Professional blue */
.success-green { color: #28a745; } /* Success green */
.warning-orange { color: #ffc107; } /* Warning orange */
.danger-red { color: #dc3545; } /* Danger red */
/* Short Hex (3 digits) */
.short-red { color: #f00; } /* Same as #ff0000 */
.short-white { color: #fff; } /* Same as #ffffff */
Hex Color Examples:
#FF0000 - Pure red
#00FF00 - Pure green
#0000FF - Pure blue
#007cba - Professional blue
#28a745 - Success green
#ffc107 - Warning orange
🔹 RGB Colors
RGB colors use rgb(red, green, blue) with values from 0-255. This format offers intuitive color mixing and transparency when extended to RGBA. RGB is particularly useful for programmatic color manipulation and creating color variations. Proper color implementation enhances visual appeal and readability, keeping users engaged with your content longer—positively impacting bounce rates and SEO metrics.
/* RGB Format: rgb(red, green, blue) */
.rgb-red { color: rgb(255, 0, 0); } /* Pure red */
.rgb-green { color: rgb(0, 255, 0); } /* Pure green */
.rgb-blue { color: rgb(0, 0, 255); } /* Pure blue */
/* Mixed Colors */
.rgb-purple { color: rgb(128, 0, 128); } /* Purple */
.rgb-orange { color: rgb(255, 165, 0); } /* Orange */
.rgb-pink { color: rgb(255, 192, 203); } /* Pink */
/* Gray Shades (equal R, G, B values) */
.light-gray { color: rgb(200, 200, 200); }
.medium-gray { color: rgb(128, 128, 128); }
.dark-gray { color: rgb(64, 64, 64); }
RGB Color Examples:
rgb(255, 0, 0) - Pure red
rgb(0, 255, 0) - Pure green
rgb(0, 0, 255) - Pure blue
rgb(128, 0, 128) - Purple
rgb(255, 165, 0) - Orange
🔹 Background Colors
Use background-color to set the background color of elements:
/* Background Colors */
.bg-red {
background-color: #ff6b6b;
color: white;
padding: 10px;
}
.bg-blue {
background-color: #4ecdc4;
color: white;
padding: 10px;
}
.bg-yellow {
background-color: #ffe66d;
color: #333;
padding: 10px;
}
.bg-gradient {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
color: white;
padding: 15px;
}
<div class="bg-red">Red background</div>
<div class="bg-blue">Blue background</div>
<div class="bg-yellow">Yellow background</div>
<div class="bg-gradient">Gradient background</div>
Background Color Examples:
🔹 Transparency with RGBA
RGBA colors allow you to create transparent backgrounds and overlays using the alpha channel for opacity control. Unlike standard RGB, RGBA includes a fourth value (alpha) ranging from 0 (fully transparent) to 1 (fully opaque). For example, background-color: rgba(255, 0, 0, 0.5); creates a semi-transparent red layer. This is ideal for modern UI effects like modal backdrops, hover states, and gradient overlays without affecting text readability. Using RGBA improves visual depth, enhances interactivity, and maintains accessibility when layered over images. It also reduces the need for extra image assets, improving page load times and supporting SEO through faster performance.
/* RGBA Format: rgba(red, green, blue, alpha) */
/* Alpha: 0 = transparent, 1 = opaque */
.transparent-red {
background-color: rgba(255, 0, 0, 0.3);
padding: 10px;
}
.semi-transparent-blue {
background-color: rgba(0, 123, 255, 0.6);
color: white;
padding: 10px;
}
.overlay {
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 20px;
}
Transparency Examples:
🔹 Color Best Practices
🎯 Tips for Using Colors:
- Contrast: Ensure good contrast between text and background
- Consistency: Use a consistent color scheme throughout your site
- Accessibility: Consider colorblind users - don't rely only on color
- Brand Colors: Use your brand colors consistently
- Readability: Dark text on light backgrounds is easiest to read
🔸 Good Color Combinations:
/* High Contrast - Easy to Read */
.good-contrast {
background-color: white;
color: #333;
}
/* Professional Blue Theme */
.professional {
background-color: #f8f9fa;
color: #2c3e50;
border-left: 4px solid #3498db;
}
/* Success Message */
.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}