Free HTML tutorial

How to Use CSS Colors in an HTML Page

Set text, background and border colors with CSS rules that target HTML elements. This guide compares named colors, HEX, RGB and HSL values.

How CSS adds color to a page

HTML provides the elements and content; CSS styles them. The color property sets text color, background-color sets an element's fill, and a border declaration can set its edge color.

For modern pages, put color values in CSS rather than relying on obsolete presentational HTML attributes. A CSS rule can live in an external stylesheet or in a page's <style> block.

Compare common CSS color formats

  • Named color: tomato
  • HEX: #f48024
  • RGB: rgb(244 128 36)
  • HSL: hsl(27 90% 55%)

RGB describes red, green and blue channels. HSL describes hue, saturation and lightness. RGB and HSL values can include an alpha value for transparency, written after a slash, such as rgb(244 128 36 / 30%).

Example: style a notice

<p class="notice">A short status message</p>
.notice {
  color: #243447;
  background-color: hsl(26 90% 54% / 20%);
  border-left: 4px solid rgb(190 84 20);
}

The first snippet is HTML. Put the .notice rule in a stylesheet; its selector connects the CSS to the element's class.

Prefer sufficient contrast between text and its background. Do not rely on color alone to communicate an important state or instruction.

For more color properties and CSS examples, continue to the CSS colors lesson.

Examples