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

HTML
<!-- 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>
▶ Try it Yourself

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
📌 Tip: Color names are simple and intuitive, great for quick prototyping and teaching. However, in production environments, color names have a limited selection and lack precision — hexadecimal or RGB notation is recommended instead.

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

HTML
<!-- 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>
▶ Try it Yourself
💡 Shorthand Rule: #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

HTML
<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>
▶ Try it Yourself

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
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

HTML
<!-- 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>
▶ Try it Yourself
💡 Designer's Choice: HSL is more designer-friendly. Simply adjust the hue to cycle through different colors, tweak saturation to control vibrancy, and modify lightness to control brightness — three dimensions are all you need to easily generate a complete color scheme.

❓ FAQ

Q What's the difference between hexadecimal (#FF5733) and RGB (rgb(255, 87, 51))?
A Both represent the same color, just written differently. Hex uses 6 hexadecimal digits, with each pair representing red, green, and blue channels (00-FF). RGB uses three decimal numbers from 0-255. Hex is more concise (6 characters), RGB is more intuitive (directly seeing values). In practice, hex is more common; use RGBA when you need transparency.
Q When should I use RGBA? How do I calculate transparency?
A RGBA adds an Alpha channel (transparency) to RGB, with values from 0 (fully transparent) to 1 (fully opaque). Use cases: 1) Semi-transparent backgrounds (overlays, frosted glass effects); 2) Text overlays on images for readability; 3) Hover tooltip backgrounds. Transparency calculation: rgba(0, 0, 0, 0.5) means 50% transparent black.
Q How is HSL better than RGB?
A HSL (Hue, Saturation, Lightness) is more intuitive for humans. RGB requires adjusting three values simultaneously to get the desired color, while HSL lets you: 1) Just adjust hue (0-360 degrees) to cycle through the entire spectrum; 2) Just adjust saturation to control vibrancy; 3) Just adjust lightness to control brightness. Designers typically use HSL to generate color schemes; developers typically use RGB/hex.
Q Can I use Chinese for color names, like 红色?
A No. HTML/CSS only supports English color names (like 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

📝 Exercises

  1. 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.
  2. 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()).
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏