HTML Colors
In this page:
Standard Color Names
Browsers recognize 140 official color names natively. This allows you to apply simple color styles like blue, red, or darkslategray directly in your style declarations without needing to look up complex coordinate codes.
Note: Color names are wonderful for fast drafting, but they limit your design choices to the predefined system catalog.
Warning: Avoid using rare color names because different browsers may render subtle shade differences on different monitor configurations.
Example: Standard Color Names
<p style="color: darkslategray;">Named color text</p>
Hexadecimal Colors (HEX)
Hexadecimal values are the industry standard for web design. They represent color coordinates using a six-digit code preceded by a hash symbol (#RRGGBB). The letters and numbers represent values from 00 to FF, representing the intensity of red, green, and blue respectively.
Note: You can use three-digit shorthand codes like #F00 for #FF0000 when the double digits are identical.
Warning: Always remember to prefix your hexadecimal code with a hash symbol, or the browser will completely fail to display the color.
Example: Hexadecimal Colors (HEX)
<p style="color: #FF0000;">Full hex code</p>
<p style="color: #F00;">Shorthand hex code</p>
RGB and RGBA Colors
RGB values define color blends using the intensity of Red, Green, and Blue on a scale from 0 to 255. By upgrading to RGBA, you gain access to an Alpha channel that controls transparency from 0.0 (completely invisible) to 1.0 (fully opaque).
Note: Use RGBA colors when you want to create beautiful, semi-transparent overlays that show content underneath.
Warning: Do not confuse RGB values with HSL values since their parameter scales and measurement methods are entirely different.
Example: RGB and RGBA Colors
<p style="color: rgb(255,0,0);">RGB red</p>
<p style="color: rgba(255,0,0,0.5);">Semi-transparent RGBA red</p>
HSL and HSLA Colors
HSL stands for Hue, Saturation, and Lightness. Hue represents the base color on a 360-degree color wheel, Saturation is the intensity of gray from 0% to 100%, and Lightness represents black or white saturation. Adding the Alpha channel (HSLA) introduces adjustable opacity.
Note: HSL is highly intuitive because adjusting the lightness percentage instantly brightens or darkens your color without changing its shade.
Warning: Always write the percentage symbols inside the Saturation and Lightness parameters, or the rule will be discarded by the browser.
Example: HSL and HSLA Colors
<p style="color: hsl(0, 100%, 50%);">HSL red</p>
<p style="color: hsla(0, 100%, 50%, 0.5);">Semi-transparent HSLA red</p>
Color Contrast and Web Accessibility
Designing websites is not just about choosing your favorite aesthetics. Web Content Accessibility Guidelines (WCAG) require that your text colors have sufficient contrast against the background fill. This ensures that users with visual impairments or people reading screens under bright sunlight can read your content.
Note: A standard contrast checker tool should show a ratio of at least 4.5:1 for regular paragraph text to meet minimum requirements.
Warning: Using light gray text on white backgrounds makes your page inaccessible, causing search engine bots to mark down your quality score.
Example: Color Contrast and Web Accessibility
<p style="color: #000000; background-color: #FFFFFF;">
High contrast text for accessibility
</p>
CSS Custom Properties for Colors
CSS custom properties, also called CSS variables, let you define a color once with a name like --primary-color and reuse it everywhere in your stylesheet. If the brand color ever changes, you update it in one single place instead of hunting through every rule.
Note: Define your color palette as custom properties on the :root selector so they are available everywhere in your CSS.
Warning: Custom properties are not supported in very old browsers like Internet Explorer 11, so check your audience's browser support before relying on them exclusively.
Example: CSS Custom Properties for Colors
<style>
:root {
--primary-color: #0000ff;
}
p {
color: var(--primary-color);
}
</style>
<p>Styled with a custom property</p>
- Forgetting to write the hashtag symbol before hex codes, causing them to break.
- omitting percentage symbols inside HSL and HSLA color arguments.
- Combining low-contrast colors that make your content unreadable and violate accessibility guidelines.
- HTML handles colors through names, hexadecimal coordinates, RGB systems, and HSL layouts.
- The RGBA and HSLA variants introduce alpha transparency parameters to blend containers.
- Maintaining high color contrast is structurally essential for SEO rankings and accessibility support.
Standard color formatting systems are universally supported by all modern and legacy browsers.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- HTML Favicon
- HTML Page Title
- HTML Tables
- HTML Lists
- HTML Block & Inline
- HTML Comments
- HTML Colors
- HTML CSS
- HTML Links
- HTML Images
- HTML RGB Colors
- HTML HEX Colors
- HTML HSL Colors
- HTML Link Colors
- HTML Link Bookmarks
- HTML Background Images
- HTML Table Borders
- HTML Table Sizes
- HTML Table Headers
- HTML Table Padding & Spacing
- HTML Colspan & Rowspan
- HTML Table Styling
- HTML Table Colgroup
- HTML Unordered Lists
- HTML Ordered Lists
- HTML Other Lists / Description Lists