Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Colors & Color Functions | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 7 of 50

·css

Colors & Color Functions

8 mins

Course progress14%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Prefer HSL/HSLA for easier hue‑adjustments.
  • `rgba` and `hsla` let you create semi‑transparent layers.
  • Use CSS variables for themeable color systems.

From Hex to Modern Color Functions

CSS offers a toolbox of color formats — each useful for different situations.

FormatExampleWhen to use
Hex#ff6600Quick, familiar, works everywhere.
RGBrgb(255,102,0)When you need integer values; good for JS interop.
RGBArgba(255,102,0,0.8)Add opacity without extra opacity property.
HSLhsl(30,100%,50%)Hue‑saturation‑lightness lets you tweak hues easily.
HSLAhsla(30,100%,50%,0.7)Same as HSL but with opacity.
currentColorborder:1px solid currentColor;Inherit the element’s defined color.
CSS 4 color functionscolor(), lab(), lch(), color-mod() (experimental)High‑precision color spaces, useful for UI design systems.

Building a Dark‑Mode Friendly Palette with HSL

:root {
  --hue-primary: 210;   /* Cool blue */
  --sat-primary: 60%;
  --light-primary: 50%;
  --primary: hsl(var(--hue-primary), var(--sat-primary), var(--light-primary));
  --primary-dark: hsl(var(--hue-primary), var(--sat-primary), 30%);
}
body { color: var(--primary); }
@media (prefers-color-scheme: dark) {
  body { color: var(--primary-dark); }
}

By storing hue and saturation separately you can change the entire brand colour with a single line.


Pro Tip – Semantic Naming

Use semantic custom‑property names (--text-primary, --bg-surface) instead of raw colour values. This decouples your design tokens from the actual colour and makes theme switching trivial.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonBox‑Sizing PropertyContinue to lesson 8Typography BasicsBack to CSS Basics to Advanced curriculum