HTML Colors
Colors in HTML can be represented in multiple ways. Mastering different color representation methods gives you flexible and precise control over every pixel in your web design.
Color Representation Overview
There are three main color representation methods in HTML and CSS: color names, hexadecimal, and RGB. They can all define colors, but each has its own use cases:
Example
<!-- All three represent a red background -->
<div style="background-color: red;">Color Name: red</div>
<div style="background-color: #FF0000;">Hex: #FF0000</div>
<div style="background-color: rgb(255,0,0);">RGB: rgb(255,0,0)</div>
Color Names
HTML supports over 140 standard color names. The most commonly used include: red, blue, green, black, white, gray, orange, purple, yellow, and more. Using color names is the most intuitive approach — no need to memorize encoding rules:
Common Color Names
| Color Name | Hex | RGB | Description |
|---|---|---|---|
red |
#FF0000 | rgb(255,0,0) | Red |
green |
#008000 | rgb(0,128,0) | Green (standard) |
lime |
#00FF00 | rgb(0,255,0) | Lime green (bright) |
blue |
#0000FF | rgb(0,0,255) | Blue |
black |
#000000 | rgb(0,0,0) | Black |
white |
#FFFFFF | rgb(255,255,255) | White |
gray |
#808080 | rgb(128,128,128) | Gray |
silver |
#C0C0C0 | rgb(192,192,192) | Silver |
orange |
#FFA500 | rgb(255,165,0) | Orange |
yellow |
#FFFF00 | rgb(255,255,0) | Yellow |
purple |
#800080 | rgb(128,0,128) | Purple |
cyan |
#00FFFF | rgb(0,255,255) | Cyan |
magenta |
#FF00FF | rgb(255,0,255) | Magenta |
pink |
#FFC0CB | rgb(255,192,203) | Pink |
brown |
#A52A2A | rgb(165,42,42) | Brown |
gold |
#FFD700 | rgb(255,215,0) | Gold |
navy |
#000080 | rgb(0,0,128) | Navy blue |
teal |
#008080 | rgb(0,128,128) | Teal |
olive |
#808000 | rgb(128,128,0) | Olive |
coral |
#FF7F50 | rgb(255,127,80) | Coral |
tomato |
#FF6347 | rgb(255,99,71) | Tomato |
skyblue |
#87CEEB | rgb(135,206,235) | Sky blue |
violet |
#EE82EE | rgb(238,130,238) | Violet |
Hexadecimal (Hex)
Hexadecimal colors start with # in the format #RRGGBB, where RR represents the red value, GG the green value, and BB the blue value, with a range of 00 to FF (hexadecimal). If each pair has identical digits, you can abbreviate to three characters #RGB:
Common Hex Colors
| Color | Hex | RGB Values | Description |
|---|---|---|---|
| Pure Red | #FF0000 | 255,0,0 | Red channel max, others 0 |
| Pure Green | #00FF00 | 0,255,0 | Green channel max, others 0 |
| Pure Blue | #0000FF | 0,0,255 | Blue channel max, others 0 |
| Pure Black | #000000 | 0,0,0 | All channels 0 |
| Pure White | #FFFFFF | 255,255,255 | All channels max |
| Sky Blue | #87CEEB | 135,206,235 | Light blue |
| Dark Blue | #00008B | 0,0,139 | Deep blue |
| Coral | #FF7F50 | 255,127,80 | Orange-pink |
| Tomato | #FF6347 | 255,99,71 | Red-orange |
| Gold | #FFD700 | 255,215,0 | Golden yellow |
| Olive | #808000 | 128,128,0 | Dark yellow |
| Maroon | #800000 | 128,0,0 | Dark red |
| Navy | #000080 | 0,0,128 | Deep blue |
| Violet | #EE82EE | 238,130,238 | Purple-pink |
| Teal | #00CED1 | 0,206,209 | Dark cyan |
Example
<!-- Six-digit hex -->
<div style="background-color: #FF5733;">#FF5733 (orange)</div>
<div style="background-color: #33A1FF;">#33A1FF (blue)</div>
<div style="background-color: #2ECC71;">#2ECC71 (green)</div>
<!-- Three-character shorthand (when each pair is same) -->
<div style="background-color: #F00;">#F00 equals #FF0000 (red)</div>
<div style="background-color: #0F0;">#0F0 equals #00FF00 (green)</div>
<div style="background-color: #00F;">#00F equals #0000FF (blue)</div>
#RGB automatically expands to #RRGGBB. For example, #F0A → #FF00AA. Only works when each pair has identical digits — #FF5733 cannot be shortened.
RGB and RGBA
The rgb(red, green, blue) function uses decimal values from 0 to 255 to represent the red, green, and blue channels. Compared to hexadecimal, RGB is closer to human intuitive understanding of numbers. rgba() adds an Alpha transparency channel on top of RGB:
RGB Values and Color Mapping
| RGB Value | Color | Description |
|---|---|---|
| rgb(255, 0, 0) | Red | Red channel max, others 0 |
| rgb(0, 255, 0) | Green | Green channel max, others 0 |
| rgb(0, 0, 255) | Blue | Blue channel max, others 0 |
| rgb(255, 255, 0) | Yellow | Red + Green |
| rgb(0, 255, 255) | Cyan | Green + Blue |
| rgb(255, 0, 255) | Magenta | Red + Blue |
| rgb(255, 255, 255) | White | All channels max |
| rgb(0, 0, 0) | Black | All channels 0 |
| rgb(128, 128, 128) | Gray | All channels medium |
| rgb(255, 165, 0) | Orange | Red + partial green |
| rgb(255, 192, 203) | Pink | Red + small amounts of green/blue |
| rgb(165, 42, 42) | Brown | Dark red + small green |
| rgb(0, 128, 128) | Teal | Green + Blue equally |
| rgb(128, 0, 128) | Purple | Red + Blue equally |
| rgb(0, 0, 128) | Navy | Deep blue |
Example
<div style="background-color: rgb(255, 99, 71);">
rgb(255, 99, 71) — Tomato
</div>
<div style="background-color: rgb(60, 179, 113);">
rgb(60, 179, 113) — Medium Sea Green
</div>
<!-- rgba adds transparency, last parameter 0~1 -->
<div style="background-color: rgba(255, 0, 0, 0.3);">
Semi-transparent red (30% opacity)
</div>
<div style="background-color: rgba(0, 0, 255, 0.6);">
Semi-transparent blue (60% opacity)
</div>
HSL
hsl(hue, saturation, lightness) is a color model that aligns better with human intuition. Hue is the color angle (0—360 degrees), Saturation is the intensity (percentage), and Lightness is the brightness (percentage). Similarly, hsla() supports transparency:
HSL Color Wheel
| Angle | Color | HSL Value | Description |
|---|---|---|---|
| 0° | Red | hsl(0, 100%, 50%) | Hue start |
| 30° | Orange | hsl(30, 100%, 50%) | Red→Yellow transition |
| 60° | Yellow | hsl(60, 100%, 50%) | Red + Green |
| 90° | Yellow-Green | hsl(90, 100%, 50%) | Yellow→Green transition |
| 120° | Green | hsl(120, 100%, 50%) | Hue midpoint |
| 150° | Teal | hsl(150, 100%, 50%) | Green→Cyan transition |
| 180° | Cyan | hsl(180, 100%, 50%) | Green + Blue |
| 210° | Sky Blue | hsl(210, 100%, 50%) | Cyan→Blue transition |
| 240° | Blue | hsl(240, 100%, 50%) | Hue endpoint |
| 270° | Purple | hsl(270, 100%, 50%) | Blue→Red transition |
| 300° | Magenta | hsl(300, 100%, 50%) | Blue + Red |
| 330° | Rose | hsl(330, 100%, 50%) | Magenta→Red transition |
| 360° | Red | hsl(360, 100%, 50%) | Back to start |
Saturation and Lightness Adjustment
| Adjustment | Effect | Example |
|---|---|---|
| Saturation 100% | Most vivid | hsl(0, 100%, 50%) = Pure red |
| Saturation 50% | Softer | hsl(0, 50%, 50%) = Dark red |
| Saturation 0% | Gray | hsl(0, 0%, 50%) = Gray |
| Lightness 50% | Normal | hsl(0, 100%, 50%) = Pure red |
| Lightness 100% | All white | hsl(0, 100%, 100%) = White |
| Lightness 0% | All black | hsl(0, 100%, 0%) = Black |
Example
<!-- HSL color mixing examples -->
<div style="background-color: hsl(0, 100%, 50%);">
hsl(0, 100%, 50%) — Pure red
</div>
<div style="background-color: hsl(120, 100%, 50%);">
hsl(120, 100%, 50%) — Pure green
</div>
<div style="background-color: hsl(240, 100%, 50%);">
hsl(240, 100%, 50%) — Pure blue
</div>
<div style="background-color: hsl(200, 80%, 60%);">
hsl(200, 80%, 60%) — Soft blue
</div>
<!-- hsla adds transparency -->
<div style="background-color: hsla(0, 100%, 50%, 0.4);">
hsla(0, 100%, 50%, 0.4) — Semi-transparent red
</div>
❓ FAQ
#FF5733) and RGB (rgb(255, 87, 51))?rgba(0, 0, 0, 0.5) means 50% transparent black.红色?red, blue, green), and there are only 147 standard color names. Chinese color names (like 红色) won't be recognized. Recommendations: 1) Use English color names for simple scenarios; 2) Use hex or RGB for precision; 3) Use CSS variables when managing many colors. Color name limitation: different browsers may render the same color name slightly differently.📖 Summary
- Color names (e.g., red, blue): Simple and intuitive, but limited in precision
- Hexadecimal (e.g., #FF5733): The most common precise notation in web development
- RGB / RGBA: rgb() uses 0—255 for three channels, rgba() adds transparency
- HSL / HSLA: hsl() uses hue/saturation/lightness to mix colors, hsla() adds transparency
- The Alpha channel (RGBA/HSLA) enables semi-transparency, with values from 0 (transparent) to 1 (opaque)
📝 Exercises
- Color Palette: Create three color blocks using three different color representations (color name, hex, RGB). Each block should be 100×100px, displayed side by side.
- Transparency Gradient: Use rgba() to create a horizontal gradient effect where the background transitions from fully opaque to fully transparent (hint: use
background: linear-gradient()).



