TExcelTAD
Home › CSS › CSS Colors

CSS Colors

Color is one of the first things people notice on a page. CSS gives you several ways to write the same color, from simple names to precise numeric formats with transparency.

Where colors are used

The same color values work for any color-accepting property:

Property Colors...
color The text (and, by default, borders and text decorations)
background-color The element's background
border-color The border
outline-color, text-decoration-color, box-shadow, ... Various effects

Color names

CSS defines about 140 named colors, from the obvious (red, blue) to the poetic (papayawhip, rebeccapurple, mediumseagreen). Names are case-insensitive.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font: 16px sans-serif; display: flex; flex-wrap: wrap; gap: 8px; }
  div { width: 120px; height: 60px; display: grid; place-items: center; border-radius: 8px; color: white; }
</style>
</head>
<body>
  <div style="background: tomato">tomato</div>
  <div style="background: steelblue">steelblue</div>
  <div style="background: mediumseagreen">mediumseagreen</div>
  <div style="background: rebeccapurple">rebeccapurple</div>
  <div style="background: goldenrod">goldenrod</div>
  <div style="background: slategray">slategray</div>
</body>
</html>

Names are handy for quick experiments, but real designs usually need exact colors, which is where numeric formats come in.

Hexadecimal: #rrggbb

A hex color has a # followed by three pairs of hexadecimal digits (0–9, a–f) for red, green and blue. Each pair goes from 00 (none) to ff (full, 255).

  • #ff0000 = pure red, #000000 = black, #ffffff = white
  • #264de4 = the blue of this course
  • Short form: #f60 is the same as #ff6600 (each digit is doubled)
  • Add a fourth pair for alpha (opacity): #264de480 is about 50% transparent.

RGB: rgb()

rgb() takes the same three channels as decimal numbers from 0 to 255. Modern syntax separates them with spaces, and an optional alpha comes after a slash:

css
color: rgb(38 77 228);
color: rgb(38 77 228 / 0.5);   /* 50% opaque */
color: rgba(38, 77, 228, 0.5); /* older comma syntax, still valid */

HSL: hsl()

HSL describes color the way people think about it:

  • Hue – angle on the color wheel: 0 red, 120 green, 240 blue
  • Saturation – 0% gray to 100% vivid
  • Lightness – 0% black, 50% normal, 100% white

HSL makes it easy to build related shades: keep the hue, change the lightness.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font: 14px sans-serif; }
  .row { display: flex; gap: 6px; margin-bottom: 10px; }
  .row div { flex: 1; height: 70px; border-radius: 6px; display: grid; place-items: center; }
</style>
</head>
<body>
  <div class="row">
    <div style="background: hsl(225 77% 20%); color: white">20%</div>
    <div style="background: hsl(225 77% 35%); color: white">35%</div>
    <div style="background: hsl(225 77% 52%); color: white">52%</div>
    <div style="background: hsl(225 77% 70%)">70%</div>
    <div style="background: hsl(225 77% 90%)">90%</div>
  </div>
  <div class="row">
    <div style="background: hsl(0 80% 60%)">0°</div>
    <div style="background: hsl(60 80% 60%)">60°</div>
    <div style="background: hsl(120 80% 60%)">120°</div>
    <div style="background: hsl(180 80% 60%)">180°</div>
    <div style="background: hsl(240 80% 60%)">240°</div>
    <div style="background: hsl(300 80% 60%)">300°</div>
  </div>
</body>
</html>

The top row keeps the same hue and only changes lightness; the bottom row walks around the color wheel.

Same color, four spellings

Format Value
Name orange
Hex #ffa500
RGB rgb(255 165 0)
HSL hsl(39 100% 50%)

Transparency

Alpha lets the background show through. It is perfect for overlays and subtle tints.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body {
    margin: 0; height: 100vh;
    background: repeating-linear-gradient(45deg, #ffcc00 0 20px, #222 20px 40px);
    display: flex; align-items: center; justify-content: center; gap: 12px;
    font: bold 18px sans-serif;
  }
  .box { width: 110px; height: 110px; display: grid; place-items: center; border-radius: 10px; }
</style>
</head>
<body>
  <div class="box" style="background: rgb(38 77 228 / 1); color: white">100%</div>
  <div class="box" style="background: rgb(38 77 228 / 0.6); color: white">60%</div>
  <div class="box" style="background: rgb(38 77 228 / 0.25)">25%</div>
</body>
</html>

Special keywords

  • transparent – fully see-through (the default background).
  • currentColor – whatever the element's color is. Great for borders and SVG icons that should match the text.

Tip: Make sure text has enough contrast with its background. Light gray text on white may look elegant but is hard to read for many people. Aim for a contrast ratio of at least 4.5:1 for body text.

Test yourself

Which hex value is pure green?

  1. #ff0000
  2. #00ff00
  3. #0000ff
  4. #ffffff

In hsl(200 80% 50%), what does 200 represent?

  1. Brightness
  2. Hue angle on the color wheel
  3. Opacity
  4. Saturation

What does rgb(0 0 0 / 0.5) produce?

  1. Solid black
  2. Half-transparent black
  3. Gray at 50% lightness
  4. An invalid color