HTML Colors – A Complete Guide with Examples & Best Practices
What Are HTML Colors?
HTML colors define the appearance of web elements using various formats like color names, HEX codes, RGB values, and HSL values. Colors are commonly used in CSS properties such as:
✅ color
– Changes text color.
✅ background-color
– Sets the background color.
✅ border-color
– Defines the color of borders.
Using Color Names in HTML
HTML supports 140+ predefined color names. You can set colors using simple names like red, blue, green, black, white, etc.
Example:
<!DOCTYPE html>
<html>
<head>
<title>HTML Colors Example</title>
</head>
<body style="background-color: lightblue;">
<h1 style="color: red;">This is a Red Heading</h1>
<p style="color: green;">This is a Green Paragraph.</p>
</body>
</html>
✅ Best Practice: Use color names for basic colors, but for precision, prefer HEX or RGB.
Using HEX Color Codes
A HEX color code is a 6-character code that represents colors using a #RRGGBB format (Red, Green, Blue).
Example:
<p style="color: #ff5733;">This is a paragraph with HEX color #ff5733.</p>
HEX Code | Color |
---|---|
#FF0000 | Red |
#00FF00 | Green |
#0000FF | Blue |
#FFFF00 | Yellow |
#000000 | Black |
#FFFFFF | White |
✅ Best Practice: Use HEX codes for web design consistency and branding.
Using RGB Color Values
RGB stands for Red, Green, Blue and takes values from 0 to 255. The format is rgb(red, green, blue)
.
Example:
<p style="color: rgb(255, 87, 51);">This text uses RGB(255, 87, 51).</p>
RGB Value | Color |
---|---|
rgb(255, 0, 0) | Red |
rgb(0, 255, 0) | Green |
rgb(0, 0, 255) | Blue |
rgb(255, 255, 0) | Yellow |
✅ Best Practice: Use RGB when you need transparency (with RGBA).
Using HSL Color Values
HSL stands for Hue, Saturation, and Lightness. The format is hsl(hue, saturation%, lightness%)
.
Example:
<p style="color: hsl(30, 100%, 50%);">This text uses HSL(30, 100%, 50%).</p>
HSL Value | Color |
---|---|
hsl(0, 100%, 50%) | Red |
hsl(120, 100%, 50%) | Green |
hsl(240, 100%, 50%) | Blue |
hsl(60, 100%, 50%) | Yellow |
✅ Best Practice: HSL is useful for adjusting color tones dynamically.
Setting Background Colors
Use background-color
to change the background color of an element.
Example:
<p style="background-color: lightgray;">This is a paragraph with a light gray background.</p>
✅ Best Practice: Use light background colors for better readability.
Setting Border Colors
Use border-color
to define the color of an element’s border.
Example:
<p style="border: 2px solid blue;">This paragraph has a blue border.</p>
✅ Best Practice: Combine border-style
, border-width
, and border-color
for custom styling.
Using Opacity and Transparency
Use rgba()
or hsla()
to add transparency to colors.
Example:
<p style="color: rgba(255, 0, 0, 0.5);">This text is semi-transparent red.</p>
✅ Best Practice: Use rgba()
for elements requiring opacity effects.
Full Example: HTML Colors in Action
<!DOCTYPE html>
<html>
<head>
<title>HTML Colors Example</title>
</head>
<body style="background-color: #f0f0f0;">
<h1 style="color: blue;">HTML Colors</h1>
<p style="color: red;">This is red text.</p>
<p style="color: #00ff00;">This text is in HEX #00ff00.</p>
<p style="color: rgb(0, 0, 255);">This text is RGB(0,0,255).</p>
<p style="color: hsl(120, 100%, 50%);">This text is HSL(120,100%,50%).</p>
<p style="background-color: yellow;">This text has a yellow background.</p>
<p style="border: 2px solid black;">This text has a black border.</p>
</body>
</html>
Best Practices for HTML Colors
✔️ Use HEX codes for standard web design colors.
✔️ Use RGB or HSL for dynamic color adjustments.
✔️ Use background-color
wisely to enhance readability.
✔️ Ensure sufficient contrast between text and background.