What are HTML Colors? #
HTML Colors are values used in CSS and HTML to display colors on a web page. They can be defined by:
- Names (like red, blue)
- Codes (like #ff0000)
- Functions (like rgb (255, 0, 0) or hsl (0, 100%, 50%))
Color on screens is created by mixing light using the RGB model: Red, Green, and Blue. Each channel ranges from 0 to 255, allowing millions of color combinations.
HTML Color Formats (HEX, RGB, RGBA, HSL, HSLA) #
1) HEX Colors #
- Format: #RRGGBB
- Each pair (RR, GG, BB) is a hexadecimal value (00–FF).
- Example:
- Red: #FF0000
- Green: #00FF00
- Blue: #0000FF
- Short HEX: #RGB (e.g., #f00 for #ff0000)
When to use: Compact and common in design systems and style guides.
2) RGB Colors #
- Format: rgb(red, green, blue)
- Each value is 0–255
- Example:
- Pure red: rgb(255, 0, 0)
- Black: rgb(0, 0, 0)
- White: rgb(255, 255, 255)
When to use: When fine-tuning light-based color mixing or animating color channels.
3) RGBA Colors (RGB + Alpha/Opacity) #
- Format: rgba(r, g, b, a)
- a is opacity from 0 (transparent) to 1 (opaque)
- Example:
- Semi-transparent red: rgba(255, 0, 0, 0.5)
When to use: Overlays, glass effects, shadows, and layered UIs.
4) HSL Colors (Hue, Saturation, Lightness) #
- Format: hsl(hue, saturation%, lightness%)
- Hue: 0–360 (0=red, 120=green, 240=blue)
- Saturation: 0–100% (gray to vivid)
- Lightness: 0–100% (black to white)
- Example:
- hsl(0, 100%, 50%) equals pure red
- hsl(200, 100%, 50%) is a vivid blue-cyan tone
When to use: Easy to adjust tints, shades, and themes by changing lightness or saturation.
5) HSLA Colors (HSL + Alpha) #
- Format: hsla(h, s%, l%, a)
- Example:
- hsla(200, 100%, 50%, 0.3)
When to use: Same benefits as HSL with transparency for modern UI design.
Color Names (140 Standard Web Colors) #
Browsers support 140 predefined HTML color names like red, tomato, dodgerblue, rebeccapurple, and lightgray. These are easy to remember and great for quick styling. Example:
- color: tomato;
- background: lightblue;
- border-color: violet;
Tip: For consistency across teams, prefer HEX/HSL in production, and use names for quick prototypes.
How to Use HTML Colors in CSS and HTML #
Inline style (quick demo)
<p style="color: #ff6347;">Hello with HEX color</p>
<p style="color: rgb(0, 128, 255);">Hello with RGB color</p>
<p style="color: hsl(210, 100%, 50%);">Hello with HSL color</p>
Internal CSS
<style>
.btn {
background: hsl(200, 100%, 45%);
color: #fff;
}
.muted {
color: rgba(0, 0, 0, 0.6);
}
</style>
External CSS
:root {
--brand: #4f46e5; /* indigo-600 */
--brand-contrast: #ffffff;
}
.header {
background: var(--brand);
color: var(--brand-contrast);
}
Backgrounds and Borders
<div style="background: #f0f8ff; border: 2px solid dodgerblue;">
Card with background and border colors
</div>
Choosing Colors: Tools and Tips #
- Use a color picker to find HEX/RGB/HSL values.
- Build a palette with base, accent, and neutral colors.
- Keep consistent spacing of lightness in HSL for harmonious UIs.
- Use transparency (RGBA/HSLA) for overlays and depth.
Practical method for theme building: #
- Pick a base hue in HSL (e.g., 210 for blue).
- Create light/dark variants by changing lightness: 95%, 75%, 50%, 35%, 20%.
- Desaturate for neutral backgrounds: saturation 5–15%.
Accessibility: Contrast and Readability #
- Aim for WCAG contrast ratios:
- Normal text: 4.5:1 or higher
- Large text (18px+ bold or 24px+): 3:1 or higher
- Avoid low-contrast combinations (e.g., light gray on white).
- Test hover/focus states for visible differentiation.
- Prefer pure black text replaced with very dark gray for comfortable reading.
Simple rule of thumb: #
- Dark text on light backgrounds: use near-black (#111–#333).
- Light text on dark backgrounds: use near-white (#f7f7f7–#ffffff).
Practical Examples #
Button Variants with HSL
<style>
.btn { padding: 10px 16px; border-radius: 8px; color: #fff; border: 0; cursor: pointer; }
.btn-primary { background: hsl(220, 90%, 56%); }
.btn-primary:hover { background: hsl(220, 90%, 50%); }
.btn-ghost { background: hsla(220, 90%, 56%, 0.12); color: hsl(220, 90%, 32%); }
</style>
<button class="btn btn-primary">Primary</button>
<button class="btn btn-ghost">Ghost</button>
Transparent Overlay with RGBA
<div style="position: relative;">
<img src="hero.jpg" alt="HTML Colors hero image" />
<div style="position:absolute; inset:0; background: rgba(0,0,0,0.35);"></div>
<h1 style="position:absolute; bottom:20px; left:20px; color:#fff;">Readable Heading</h1>
</div>
Palette Using CSS Variables
:root{
--bg: #ffffff;
--text: #1f2937; /* slate-800 */
--muted: rgba(31,41,55,0.6);
--primary: hsl(220, 90%, 56%);
--primary-weak: hsla(220, 90%, 56%, 0.12);
--accent: #ef4444; /* red-500 */
}
body { background: var(--bg); color: var(--text); }
a { color: var(--primary); }
a:hover { color: hsl(220, 90%, 46%); }
.note { background: var(--primary-weak); border-left: 4px solid var(--primary); padding: 12px; }