Tutorials CSS Colors
Ch 5 / 60
Chapter 5 Beginner 7 min read CSS

CSS Colors

Color is one of the most powerful tools in web design. CSS gives you six distinct color formats, full alpha transparency control, and a simple path to building dark-mode ready theming systems — all without any design software.

What you'll learn
Named colors, HEX, RGB, RGBA, HSL and HSLA formats
How to read and write HEX color codes
Alpha transparency with RGBA and HSLA
CSS color properties: color, background-color, border-color
The opacity property vs alpha channel
CSS variables for a reusable color system

CSS Color Properties

Before exploring color formats, you need to know which CSS properties accept a color value. Any of the six formats covered in this chapter work in all of them.

Property What it colors Example
color Text (foreground) color color: #264de4;
background-color Element background fill background-color: #f0f4ff;
border-color Border line color border-color: rgba(38,77,228,.3);
outline-color Outline ring color (focus state) outline-color: #264de4;
text-decoration-color Underline / strikethrough color text-decoration-color: red;
box-shadow Shadow color (4th value) box-shadow: 0 4px 16px rgba(0,0,0,.1);
caret-color Input text cursor color caret-color: #264de4;
fill / stroke SVG element fill and outline fill: #264de4;
color
bg-color
border
gradient
rgba
dark bg

All CSS Color Formats

CSS supports six main color formats. All six produce valid colors; the best choice depends on your use case.

Format 1
Named Colors
color: tomato;
147 predefined keywords. Human-readable but limited palette. Good for quick prototyping.
Format 2
HEX
#RRGGBB or #RGB
Most commonly used. Compact 6-digit (or 3-digit shorthand) hexadecimal code. Direct output from design tools like Figma.
Format 3
RGB
rgb(R, G, B)
Channels 0–255 for red, green, blue. Human-readable and easy to adjust programmatically.
Format 4
RGBA
rgba(R, G, B, A)
Same as RGB plus an alpha (opacity) channel from 0 (fully transparent) to 1 (fully opaque).
Format 5
HSL
hsl(H, S%, L%)
Hue (0–360°), Saturation (0–100%), Lightness (0–100%). Most intuitive for creating color variations.
Format 6
HSLA
hsla(H, S%, L%, A)
HSL with alpha transparency. Best for theming systems where you need transparent variants of brand colors.

Named Colors

CSS defines 147 named color keywords. You can use them directly by name — no codes needed. They are case-insensitive: Red, red and RED all work.

red
orangered
gold
yellow
lime
green
cyan
dodgerblue
blue
blueviolet
violet
deeppink
crimson
tomato
hotpink
lightseagreen
steelblue
saddlebrown
gray
whitesmoke
black
white
transparent
currentColor
CSS
h1       { color: tomato; }
p        { color: dimgray; }
.badge   { background-color: gold; color: black; }
.danger  { background-color: crimson; color: white; }
.overlay { background-color: transparent; }

/* Special keyword — inherits the element's own color value */
.icon {
  color:  #264de4;
  border: 1px solid currentColor; /* border uses the same blue */
}
currentColor
currentColor is a special keyword that references the element's own color value. It lets borders, shadows and SVG fills automatically match the text color — great for icons and consistent component styling.

HEX Colors

HEX (hexadecimal) is the most widely used CSS color format. A HEX color code starts with # followed by six characters — two each for Red, Green and Blue channels, in base-16 (0–9 + A–F).

#
26 Red
4D Green
E4 Blue

Each pair is a hexadecimal number from 00 (0 decimal = no channel) to FF (255 decimal = full channel).

CSS
/* 6-digit HEX — most common */
h1 { color: #264de4; }        /* CSS blue */
p  { color: #52524a; }        /* dark grey */

/* 3-digit shorthand — only when both digits are identical */
/* #rgb is shorthand for #rrggbb */
.white  { color: #fff; }       /* same as #ffffff */
.black  { color: #000; }       /* same as #000000 */
.red    { color: #f00; }       /* same as #ff0000 */

/* 8-digit HEX — includes alpha channel (CSS4) */
.overlay { background: #264de480; } /* 80 hex = 50% opacity */

/* Common palette examples */
:root {
  --brand:   #264de4;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger:  #ef4444;
  --dark:    #0f172a;
  --light:   #f8fafc;
}
#ef4444
#f97316
#f59e0b
#22c55e
#06b6d4
#264de4
#a855f7
#0f172a
HEX is case-insensitive
#264DE4, #264de4 and #264De4 are all identical. Most developers prefer lowercase for consistency. Design tools like Figma export uppercase — both are valid.

RGB & RGBA

RGB defines a color by specifying its Red, Green and Blue channel intensities as integers from 0 (no light) to 255 (full intensity). RGBA adds a fourth Alpha channel for transparency.

R · 38
38 / 255
G · 77
77 / 255
B · 228
228 / 255

Above: channel breakdown of rgb(38, 77, 228) — the CSS blue used throughout this course.

CSS
/* RGB — channels 0–255 */
h1    { color: rgb(38, 77, 228); }   /* CSS blue */
p     { color: rgb(82, 82, 74); }    /* dark grey */
.red  { color: rgb(255, 0, 0); }    /* pure red */
.white{ color: rgb(255, 255, 255); } /* white */
.black{ color: rgb(0, 0, 0); }       /* black */

/* RGBA — 4th value is alpha: 0 (transparent) → 1 (opaque) */
.overlay    { background: rgba(0, 0, 0, 0.5); }       /* 50% black */
.card-glow  { box-shadow: 0 8px 24px rgba(38, 77, 228, 0.3); }
.badge      { background: rgba(38, 77, 228, 0.12); }    /* tinted bg */
.transparent{ background: rgba(255, 255, 255, 0); }   /* invisible */

/* Modern space-separated syntax (CSS4, all browsers 2022+) */
.modern { color: rgb(38 77 228 / 80%); } /* commas optional */

Alpha channel values from 0 → 1 on the brand blue:

0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

HSL & HSLA

HSL stands for Hue, Saturation and Lightness. Unlike RGB which mixes light channels, HSL maps to how humans actually perceive color — making it the most intuitive format for creating color palettes and variations.

H
Hue
The base color on the wheel. Red at 0°, green at 120°, blue at 240°.
0° — 360°
S
Saturation
Intensity of the color. 0% is grey, 100% is fully vivid.
0% — 100%
L
Lightness
0% is black, 50% is the pure color, 100% is white.
0% — 100%
CSS
/* HSL — intuitive color variations */
.brand  { color: hsl(228, 72%, 52%); }  /* CSS blue */
.green  { color: hsl(142, 71%, 45%); }  /* success green */
.yellow { color: hsl(38, 92%, 50%); }   /* warning amber */
.grey   { color: hsl(228, 5%, 50%); }   /* desaturated = grey */
.black  { color: hsl(0, 0%, 0%); }      /* L=0 = black */
.white  { color: hsl(0, 0%, 100%); }   /* L=100% = white */

/* Easy color scale — just change the L value */
.blue-100 { background: hsl(228, 72%, 95%); } /* very light */
.blue-300 { background: hsl(228, 72%, 75%); } /* light */
.blue-500 { background: hsl(228, 72%, 52%); } /* base */
.blue-700 { background: hsl(228, 72%, 35%); } /* dark */
.blue-900 { background: hsl(228, 72%, 18%); } /* very dark */

/* HSLA — with alpha transparency */
.modal-bg  { background: hsla(228, 72%, 10%, 0.85); }
.badge-tint{ background: hsla(228, 72%, 52%, 0.12); }

Hue value (0° → 360°) at full saturation and 50% lightness:

0°
30°
60°
90°
120°
180°
228°
270°
300°
330°
Why HSL is great for design systems
With HSL, creating a color scale is trivial — keep the same H and S, just change L. For example, hsl(228, 72%, 95%) to hsl(228, 72%, 10%) gives you a complete blue scale from near-white to near-black, all perfectly on-brand.

The opacity Property vs Alpha Channel

There are two ways to make an element semi-transparent in CSS. They look similar but behave very differently:

opacity: 0.5;
Makes the entire element and all its children semi-transparent. Child elements cannot override this — their content, text and background all fade together. Value: 0 (invisible) to 1 (fully visible).
rgba(0,0,0,0.5)
Makes only that specific color value semi-transparent. Children are unaffected. This is why RGBA is preferred for overlays, shadows and tinted backgrounds — the text inside stays fully opaque.
CSS
/* opacity — fades the WHOLE element including children */
.modal-overlay {
  background: #000;
  opacity: 0.5; /* text inside will also be 50% visible — bad! */
}

/* rgba — fades ONLY the background, text stays fully opaque */
.modal-overlay {
  background: rgba(0, 0, 0, 0.5); /* text inside is still 100% visible — correct */
}

/* opacity is useful for disabling/fading an entire component */
.btn:disabled {
  opacity: 0.4;
  cursor:  not-allowed;
}

/* opacity 0 = invisible but still takes up space */
/* visibility: hidden = same as opacity 0 */
/* display: none = invisible AND removed from layout */
Common mistake
Using opacity: 0.5 on a card or modal hoping only the background becomes transparent — but all the text, buttons and images inside also fade. Use rgba() or hsla() on the background-color property instead.

CSS Variables for a Color System

The most powerful pattern for managing colors in a real project is to define all colors as CSS Custom Properties (variables) on the :root element. This gives you a single source of truth for your entire palette — and makes dark mode trivially easy.

CSS — Color system with dark mode
/* ── Light theme (default) ── */
:root {
  --color-brand:   #264de4;
  --color-text:    #1a1a14;
  --color-text-2:  #52524a;
  --color-bg:      #ffffff;
  --color-bg-2:    #f8fafc;
  --color-border:  #e5e7eb;
  --color-success: #22c55e;
  --color-warning: #f59e0b;
  --color-danger:  #ef4444;
}

/* ── Dark theme — just override the same variables ── */
[data-theme="dark"] {
  --color-brand:   #4a8af4;
  --color-text:    #f0f0e8;
  --color-text-2:  #a8a89c;
  --color-bg:      #0f172a;
  --color-bg-2:    #1e293b;
  --color-border:  #334155;
}

/* ── Components reference variables, not raw values ── */
body  { background: var(--color-bg);   color: var(--color-text); }
a     { color: var(--color-brand); }
.card { background: var(--color-bg-2); border: 1px solid var(--color-border); }
☀️ Light Theme
CSS Colors
CSS variables make switching between light and dark themes a single line of JavaScript.
Learn More
🌙 Dark Theme
CSS Colors
CSS variables make switching between light and dark themes a single line of JavaScript.
Learn More
Toggle dark mode with one line
document.documentElement.setAttribute('data-theme', 'dark'); — this single JS line switches every color on the page because all components use CSS variables, not hard-coded values.

Modern CSS Color Formats

CSS Color Level 4 (2022+) introduced newer formats with wider color gamuts and more predictable perceptual results. They are supported in all modern browsers.

CSS — Modern color formats
/* oklch() — perceptually uniform, widest gamut, recommended for 2025+ */
.brand  { color: oklch(55% 0.2 264); }   /* L% · C · H° */
.green  { color: oklch(65% 0.18 142); }

/* color-mix() — mix two colors by percentage */
.mixed  { color: color-mix(in srgb, #264de4 70%, white); }
.dimmed { color: color-mix(in srgb, currentColor 40%, transparent); }

/* light-dark() — automatic light/dark switching (CSS4) */
body {
  color-scheme: light dark;
  background:   light-dark(#ffffff, #0f172a);
  color:         light-dark(#1a1a14, #f0f0e8);
}
Beginner recommendation
Stick with HEX and RGBA when you are starting out — they are what design tools export and what you'll see in most tutorials. Once comfortable, try HSL for creating color palettes, then explore oklch for the most accurate color work.
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
One printable page covering every CSS3 property, selector type, unit and layout system including all color formats and properties.
Download PDF

Try It Yourself

The editor below shows all six color formats used together. Try changing the --brand variable value and watch every component update at once.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js — all in one industry-ready curriculum. Build real projects with AI-powered tools and get placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5 CSS3 JavaScript React.js Node.js Tailwind CSS AI Tools

Quick Check — Chapter 5

Questions & Answers
Named colors (e.g. tomato) — 147 keywords, great for quick prototyping or well-known colors.

HEX (e.g. #264de4) — most commonly used, direct output from design tools like Figma, compact notation.

RGB (e.g. rgb(38,77,228)) — explicit channel control, easy to adjust programmatically.

RGBA (e.g. rgba(38,77,228,0.5)) — best for transparent overlays, tinted backgrounds, shadows. Children stay fully opaque.

HSL (e.g. hsl(228,72%,52%)) — best for design systems and color scales. Change only L to get a complete shade range.

HSLA (e.g. hsla(228,72%,52%,0.12)) — HSL with transparency, ideal for theming systems.
A 6-digit HEX code is split into three pairs: #RR GG BB.

#264de4 means: Red = 26 hex (38 decimal), Green = 4d hex (77 decimal), Blue = e4 hex (228 decimal).

Each pair goes from 00 (0, no channel) to ff (255, full channel). A higher blue value means more blue — which gives this particular code its vivid blue appearance.

The 3-digit shorthand (#fff) works only when both digits of each pair are identical — #fff expands to #ffffff.
opacity: 0.5 applies transparency to the entire element and all its children. Text, images and child elements all become 50% transparent — you cannot override this on a child element.

rgba(0,0,0,0.5) applies transparency only to that specific color value. If used on a card's background-color, the text and other content inside the card remain fully opaque.

Use rgba() / hsla() for overlays and tinted backgrounds. Use opacity for fading entire components like a disabled button.
With HEX or RGB, generating a color scale (light to dark versions of the same hue) requires manually calculating each shade — the numbers do not map intuitively to "lighter" or "darker".

With HSL, you keep the Hue (H) and Saturation (S) constant and simply increase or decrease the Lightness (L) value. For example, hsl(228, 72%, 95%) through hsl(228, 72%, 10%) gives you a perfect blue scale — all perfectly on-brand with no manual calculation.
Define all colors as CSS custom properties on :root for the light theme. Then in a [data-theme="dark"] selector, redefine the same variable names with dark theme values.

Every component that uses var(--color-bg) instead of a hard-coded #ffffff will automatically update when the theme switches — because the variable's value changes but the component code stays identical.

Switching dark mode then becomes one line of JavaScript: document.documentElement.setAttribute('data-theme', 'dark');
Multiple Choice Questions
Question 1 of 5
What does the RGBA format add compared to RGB?
Question 2 of 5
What is the 3-digit shorthand for #ffcc00?
Question 3 of 5
In HSL, what does changing only the L value do?
Question 4 of 5
What does opacity: 0.5 affect?
Question 5 of 5
What special keyword inherits the element's own color value?
Chapter Summary
  • CSS supports six color formats: Named colors, HEX, RGB, RGBA, HSL and HSLA. All work in any property that accepts a color value.
  • Named colors — 147 keywords including transparent and currentColor. Good for quick work, limited palette.
  • HEX #RRGGBB — most widely used. Compact, direct from design tools. 3-digit shorthand when pairs repeat (#fff).
  • RGBA rgba(r,g,b,a) — best for transparent overlays, tinted backgrounds and shadows. Children stay fully opaque unlike opacity.
  • HSL hsl(H,S%,L%) — most intuitive for design systems. Vary only L to generate a complete color scale.
  • opacity fades the whole element including children. RGBA/HSLA alpha fades only that color value — children stay opaque.
  • CSS variables on :root are the best practice for color systems. Redefine the same variables in [data-theme="dark"] for one-switch dark mode.
~7 min read
Beginner level
Chapter 5 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel