HTML Tutorial menu

HTML Colors

You can colour text, backgrounds and borders using names, HEX codes, RGB or HSL values.

Colour Names

Browsers understand about 140 colour names, such as red, orange, teal, navy and tomato.

Example

html
<!DOCTYPE html>
<html>
<body>

<h2 style="background-color:tomato;">Tomato</h2>
<h2 style="background-color:orange;">Orange</h2>
<h2 style="background-color:teal; color:white;">Teal</h2>
<h2 style="background-color:navy; color:white;">Navy</h2>

</body>
</html>
Try it Yourself »

Text, Background and Border Colours

Example

html
<!DOCTYPE html>
<html>
<body>

<p style="color:darkgreen;">Green text</p>
<p style="background-color:lightblue;">Light blue background</p>
<p style="border:2px solid crimson;">Crimson border</p>

</body>
</html>
Try it Yourself »

HEX Colours

A HEX colour starts with # and has six characters: two for red, two for green, two for blue. Each pair goes from 00 (none) to ff (full).

HEXColour
#ff0000Red
#00ff00Green
#0000ffBlue
#000000Black
#ffffffWhite
#e34c26The orange-red of the HTML logo

Example

html
<h1 style="color:#e34c26;">HTML is fun</h1>

HEX is the most common format in real projects. Designers usually give you colours as HEX codes from tools like Figma.

RGB Colours

rgb(red, green, blue) uses numbers from 0 to 255 for each part.

Example

html
<!DOCTYPE html>
<html>
<body>

<p style="background-color:rgb(255, 99, 71);">rgb(255, 99, 71)</p>
<p style="background-color:rgb(0, 128, 0); color:white;">rgb(0, 128, 0)</p>
<p style="background-color:rgb(200, 200, 200);">rgb(200, 200, 200) is light gray</p>

</body>
</html>
Try it Yourself »

When all three numbers are equal, you get a shade of gray.

Transparency with RGBA

Add a fourth number, the alpha, from 0 (fully see-through) to 1 (solid).

Example

html
<p style="background-color:rgba(0, 0, 255, 0.2);">Soft blue background</p>
<p style="background-color:rgba(0, 0, 255, 0.8); color:white;">Strong blue background</p>

HSL Colours

hsl(hue, saturation, lightness) describes colours the way people think about them:

  • Hue: a position on the colour wheel, 0 to 360 (0 is red, 120 is green, 240 is blue)
  • Saturation: 0% is gray, 100% is full colour
  • Lightness: 0% is black, 50% is normal, 100% is white

Example

html
<p style="background-color:hsl(30, 100%, 50%);">Orange</p>
<p style="background-color:hsl(30, 100%, 85%);">Light orange</p>

HSL makes it easy to create lighter and darker shades of one colour.

Exercise

Exercise

What colour is #000000?

Learn HTML with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp