CSS Basics

CSS Colors

CSS Colors are values that define the visual appearance of text, backgrounds, borders, and other elements using various formats including named colors, hexadecimal codes (#), RGB/RGBA functions, HSL/HSLA functions, and modern color functions like oklch() and color().

How do you specify colors in CSS?

CSS accepts colours as named keywords (red), hex codes (#ff0000), rgb(), hsl() and modern spaces like oklch(). Add transparency with an alpha value, for example rgb(0 0 0 / 50%) or the 8-digit hex #00000080. HSL and OKLCH are easiest to adjust by hand because lightness is a separate number.

color: tomato;
color: #3b82f6;
color: rgb(59 130 246);
color: rgb(59 130 246 / 50%);   /* 50% transparent */
color: hsl(217 91% 60%);
color: oklch(62% 0.19 260);

CSS Colors in Action

Named Colors

color: red;
color: blue;
color: transparent;
color: currentColor;

Pre-defined color keywords for common colors

Hexadecimal Notation

color: #ff0000; /* Red */
color: #00ff00; /* Green */
color: #0000ff; /* Blue */
color: #fff; /* Short form */

Six-digit hex codes representing RGB values

RGB/RGBA Functions

color: rgb(255, 0, 0);
color: rgba(255, 0, 0, 0.5);
color: rgb(100% 0% 0%);
color: rgba(100%, 0%, 0%, 50%);

Red, Green, Blue values with optional alpha transparency

HSL Color Space

color: hsl(0, 100%, 50%);
color: hsla(0, 100%, 50%, 0.5);
color: hsl(240deg 100% 50%);
color: hsl(240 100% 50% / 50%);

Hue, Saturation, Lightness with optional alpha

CSS Custom Properties

:root { --primary: #3b82f6; }
color: var(--primary);
color: var(--primary, blue);

Reusable color variables with fallbacks

Modern Color Functions

color: color(display-p3 1 0 0);
color: oklch(70% 0.25 142);
color: lab(50% 20 -30);

Next-generation color spaces and functions

Code Editor

<div class="swatches">
  <div style="background: tomato">named</div>
  <div style="background: #3b82f6">hex</div>
  <div style="background: rgb(16 185 129)">rgb</div>
  <div style="background: hsl(280 70% 55%)">hsl</div>
  <div style="background: rgb(0 0 0 / 40%)">alpha</div>
</div>
<label>Hue: <input type="range" id="hue" min="0" max="360" value="217"></label>
<div id="live">hsl(217 90% 55%)</div>

Preview

Your Learning Path

Master CSS colors and visual design

What You've Learned

  • ▶Color formats (hex, RGB, HSL)
  • ▶Named colors and transparency
  • ▶Gradient techniques
  • ▶Color accessibility principles

What's Next

  • ▶CSS Backgrounds - Images and patterns
  • ▶CSS Typography - Fonts and text styling
  • ▶CSS Box Model - Spacing and layout

CSS Colors — step by step

1 / 4
1

Color Values

CSS supports hex (#ff6b6b), rgb(), hsl(), and modern oklch() — each with different trade-offs for readability and manipulation.

1 of 4
💡Use oklch() for Modern Color
oklch() gives perceptually uniform brightness across hues — unlike hsl(), it produces consistent contrast when you change only the hue channel, making it ideal for design systems.

🎯 Quick Check

Which CSS function lets you blend two colors together?

CSS Colors: summary

Key points

  • Hex uses two digits each for red, green and blue (00–ff); #abc is shorthand for #aabbcc.
  • In hsl(hue saturation lightness), hue is an angle from 0 to 360 on the colour wheel.
  • currentColor reuses the element's text colour; it's great for icons and borders.
  • Body text needs a contrast ratio of at least 4.5:1 with its background (WCAG AA).

Frequently asked questions

›What is the difference between RGB and HSL?

RGB mixes red, green and blue light. HSL describes hue, saturation and lightness, which makes it easy to create lighter or darker shades of the same colour by changing one number.

›How do I make a color transparent?

Add an alpha channel: rgb(0 0 0 / 0.5), hsl(0 0% 0% / 50%) or the 8-digit hex #00000080. opacity makes the whole element transparent, including its children.