HTML Colors
What are HTML Colors?
HTML colors are used to add color to text, backgrounds, borders, and other elements in HTML documents. Colors can be specified using color names, RGB values, HEX codes, or HSL values. Colors are typically applied using CSS (Cascading Style Sheets).
Color Methods in HTML
There are several ways to specify colors in HTML:
1. Color Names
HTML supports 140 standard color names. These are predefined color names that browsers recognize:
<p style="color: red;">Red text</p>
<p style="color: blue;">Blue text</p>
<p style="color: green;">Green text</p>
<h1 style="background-color: yellow;">Yellow background</h1>
2. RGB Colors
RGB (Red, Green, Blue) values specify colors using numeric values from 0 to 255 for each color channel:
<p style="color: rgb(255, 0, 0);">Red using RGB</p>
<p style="color: rgb(0, 255, 0);">Green using RGB</p>
<p style="color: rgb(0, 0, 255);">Blue using RGB</p>
See HTML Colors RGB for more details.
3. HEX Colors
HEX (Hexadecimal) colors use a 6-digit code preceded by a hash (#) symbol. Each pair of digits represents red, green, and blue values:
<p style="color: #ff0000;">Red using HEX</p>
<p style="color: #00ff00;">Green using HEX</p>
<p style="color: #0000ff;">Blue using HEX</p>
<p style="color: #4b3190;">Purple using HEX</p>
See HTML Colors HEX for more details.
4. HSL Colors
HSL (Hue, Saturation, Lightness) values specify colors using hue (0-360), saturation (0-100%), and lightness (0-100%):
<p style="color: hsl(0, 100%, 50%);">Red using HSL</p>
<p style="color: hsl(120, 100%, 50%);">Green using HSL</p>
<p style="color: hsl(240, 100%, 50%);">Blue using HSL</p>
See HTML Colors HSL for more details.
Common Color Names
HTML supports 140 standard color names. Here are some common ones:
| Color Name | HEX Code | RGB | Example |
|---|---|---|---|
| Red | #ff0000 | rgb(255, 0, 0) | Red Text |
| Green | #008000 | rgb(0, 128, 0) | Green Text |
| Blue | #0000ff | rgb(0, 0, 255) | Blue Text |
| Yellow | #ffff00 | rgb(255, 255, 0) | Yellow Text |
| Orange | #ffa500 | rgb(255, 165, 0) | Orange Text |
| Purple | #800080 | rgb(128, 0, 128) | Purple Text |
| Black | #000000 | rgb(0, 0, 0) | Black Text |
| White | #ffffff | rgb(255, 255, 255) | White Text |
Applying Colors to Elements
Text Color
Use the color property to change text color:
<p style="color: red;">Red text</p>
<p style="color: #4b3190;">Purple text</p>
<p style="color: rgb(0, 128, 0);">Green text</p>
Background Color
Use the background-color property to change background color:
<p style="background-color: yellow;">Yellow background</p>
<p style="background-color: #4b3190; color: white;">Purple background, white text</p>
<div style="background-color: rgb(200, 200, 200); padding: 10px;">Gray background</div>
Border Color
Use the border-color property to change border color:
<p style="border: 2px solid red;">Red border</p>
<p style="border: 3px dashed #4b3190;">Purple dashed border</p>
<div style="border: 1px solid rgb(0, 255, 0); padding: 10px;">Green border</div>
RGBA and HSLA (Transparency)
RGBA and HSLA add an alpha channel for transparency (0.0 = fully transparent, 1.0 = fully opaque):
<p style="background-color: rgba(255, 0, 0, 0.5);">Red with 50% opacity</p>
<p style="background-color: rgba(0, 255, 0, 0.3);">Green with 30% opacity</p>
<div style="background-color: hsla(240, 100%, 50%, 0.7); padding: 10px;">Blue with 70% opacity</div>
Color Resources
For more information about specific color formats, see:
- HTML Colors RGB - Learn about RGB color values
- HTML Colors HEX - Learn about hexadecimal color codes
- HTML Colors HSL - Learn about HSL color values
Best Practices
- Use HEX or RGB for consistency: More precise than color names
- Consider accessibility: Ensure sufficient contrast between text and background colors
- Use a color palette: Maintain consistent colors throughout your website
- Test in different browsers: Some color names may render differently
- Use CSS classes: Instead of inline styles, use CSS classes for reusable colors