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.

CSS Custom Properties (Variables) | CSS Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Course

Lesson 24 of 50

·css

CSS Custom Properties (Variables)

9 mins

Course progress48%

What you'll learn

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

3 points
  • Custom properties are **inherited**, so you can define a theme at `:root` and override locally.
  • They work inside `calc()`, `var()` can have a fallback value (`var(--missing, #f00)`).
  • They enable **runtime theming** without recompiling CSS.

Custom properties are reusable values that cascade just like any other property.

:root { --primary:#0066cc; --spacing:1rem; }
.button { background:var(--primary); margin:var(--spacing); }

You can update them at runtime with JavaScript (element.style.setProperty('--primary','#ff6600')).


Example: Dark‑mode toggle using CSS variables

:root { --bg:#fff; --text:#111; }
[data-theme="dark"] { --bg:#111; --text:#eee; }
body { background:var(--bg); color:var(--text); }
Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS CourseBrowse activities
Previous lessonResponsive Design – Media QueriesContinue to lesson 25CSS Functions – calc(), clamp(), min(), max()Back to CSS Course curriculum