Colors & BackgroundsCore· 30 min read

CSS Colors

Set text and background colours four different ways — names, HEX, RGB and HSL.

What you will learn

  • Set color and background-color
  • Use names, HEX, RGB, RGBA and HSL
  • Add transparency

color and background-color

The color property sets text colour; background-color sets the background.

Four colour notations + a coloured box
<style>
  .a { color: tomato; }
  .b { color: #4338ca; }
  .c { color: rgb(6, 182, 212); }
  .d { color: hsl(160, 84%, 39%); }
  .box { background-color: #4338ca; color: white; padding: 14px; border-radius: 8px; }
</style>

<p class="a">Named: tomato</p>
<p class="b">HEX: #4338ca</p>
<p class="c">RGB: rgb(6,182,212)</p>
<p class="d">HSL: hsl(160,84%,39%)</p>
<div class="box">White text on a coloured background</div>
Live preview

The first four lines are four different ways to write a colour — they all produce a colour the same way, just with different notation:

  • tomato — a named colour. CSS has about 140 of these handy names.
  • #4338ca — a HEX code. The six characters after # mix red, green and blue. This is what designers use most.
  • rgb(6, 182, 212)RGB: red, green, blue each from 0 to 255.
  • hsl(160, 84%, 39%)HSL: hue (the colour on a wheel), saturation, lightness — easy for tweaking shades.

The last rule, .box, shows both colour properties working together: background-color paints the box indigo and color: white makes the text readable on top. Always pair a dark background with light text (or vice-versa) so it stays legible.

Transparency with RGBA / HSLA

Add a fourth value from 0 (invisible) to 1 (solid) for opacity.

RGBA adds see-through colour
<style>
  .wrap { background: #4338ca; padding: 20px; }
  .glass { background: rgba(255,255,255,0.85); padding: 12px; border-radius: 8px; }
</style>
<div class="wrap"><div class="glass">Semi-transparent white box</div></div>
Live preview

The inner box uses rgba(255,255,255,0.85) — white with an alpha (transparency) of 0.85. Because it is slightly see-through, the indigo of the wrapper behind it bleeds through, giving that frosted-glass look. Drop the 0.85 to 0.3 and you would see much more of the indigo underneath. 1 would be fully solid white; 0 would be invisible.

Tip: Designers mostly use HEX (#4338ca). Pick any colour from a “color picker” (search it on Google) and copy its HEX code.

Q. Which property sets the colour of TEXT?

Answer: color sets text colour. background-color sets the background. (There is no text-color or font-color property.)

✍️ Practice

  1. Make a box with a coloured background and white text.
  2. Write the same colour three ways: name (if it has one), HEX and RGB.

🏠 Homework

  1. Choose a 3-colour brand palette and write each as a HEX value with a comment naming it.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →