HTML Colors

In HTML, we use colors for the text, background, shadows, borders, etc., of the HTML elements.

In HTML, we can specify color using any of the following.

  • Predefined Colors
  • RGB
  • HEX
  • HSL
  • RGBA
  • HSLA

HTML Predefined Color Names

There are 140 predefined color names. These color names are supported by all browsers.

All of these predefined color names are provided in the following example, displayed with the name against the actual color.

index.html

ADVERTISEMENT

HEX Color Values

The format of hexadecimal color value is

#RRGGBB

where RR, GG, and BB are the red, green, and blue components of the color.

We can also provide an optional transparency as shown in the following.

#RRGGBBAA

where AA is for transparency.

The range of values for RR, GG, BB, and AA components is [00, FF]. We can provide any value from 00 to FF (including these).

We can also use shortcut format of these color values. The shortcut format is

#RGB

and with transparency

#RGBA

The following are some of the examples for valid hexadecimal color values.

#644         /* #RGB       format    */
#ff07        /* #RGBA      format    */
#ff00aa      /* #RRGGBB    format    */
#ff00aa77    /* #RRGGBBAA  format    */

In the following HTML, we apply background colors for divs, using these hexadecimal color values.

index.html

RGB or RGBA Color Values

The format of RGB color value is

rgb(red, blue, green)

where red, blue, and green are integers in the range [0, 255] or percentage values in the range [0%, 100%] representing the respective components of the color.

The format of RGBA color value is

rgba(red, blue, green, alpha)

where alpha specifies the opacity. alpha takes value in the range [0.0, 1.0] where 0.0 is fully transparent, and 1.0 is fully opaque.

The following are some of the examples for valid hexadecimal color values.

rgb(24, 142, 255)
rgb(24, 142, 255, 0.5)

In the following HTML, we apply background colors for divs, using these RBG or RGBA color values.

index.html

HSL or HSLA Color Values

The format of HSL color value is

hsl(hue, saturation, lightness)

where

  • hue is degree on the color wheel. The value of hue ranges from 0 to 360, where 0 indicates red, 120 indicates green and 240 indicates blue.
  • saturation is the percentage of colorfulness. The value of saturation ranges from 0% to 100%, where 0% means no color and may result in a shade of gray, and 100% means full color.
  • lightness is the percentage of light in the color. The value of saturation ranges from 0% to 100%, where 0% means black, and 100% means white.

The format of HSLA color value is

hsla(hue, saturation, lightness, alpha)

where alpha specifies the opacity. alpha takes value in the range of [0.0, 1.0] where 0.0 is fully transparent, and 1.0 is fully opaque.

The following are some of the examples for valid hexadecimal color values.

hsl(24, 42%, 50%)
hsla(24, 42%, 50%, 0.6)

In the following HTML, we apply background colors for divs, using these HSL or HSA color values.

index.html

Conclusion

In this HTML Tutorial, we learned about Colors in HTML, and how to use them for elements in HTML.