📡 You're offline — showing cached content
New version available!
Quick Access

CSS Colors, Variables, and Custom Properties

Learn CSS color formats (hex, rgb, hsl), CSS custom properties, color schemes, and building a theme system.

EzyCoders Admin August 4, 2026 7 min read 7 views
CSS Colors, Variables, and Custom Properties
Share: Twitter LinkedIn WhatsApp

What is it?

CSS custom properties store reusable values — colours, sizes, spacing — in one place. Changing a variable updates every element that uses it. Combined with color schemes, they power dark mode and theming.

Why does it matter?

Design systems require consistent colours and spacing. Without CSS variables, changing the primary colour means finding and replacing it in hundreds of places. Variables make design changes instant and systematic.

Learn CSS color formats (hex, rgb, hsl), CSS custom properties, color schemes, and building a theme system.

Real-World Use Cases

  • 🎨 Brand colour system - Define --primary, --secondary, --accent once; use them throughout — change the brand colour in one line.
  • 🌙 Dark mode toggle - CSS variables on :root change when a .dark class is added to body — instant theme switch.
  • 📏 Consistent spacing scale - Define --spacing-xs through --spacing-2xl and use them for all padding and margin.
  • 🔲 Component theming - Card reads --card-bg from variables — parent changes the theme, card adapts automatically.

CSS Color Formats

/* Hex */
color: #6366f1;       /* 6-digit */
color: #6366f1cc;     /* 8-digit: last 2 = opacity */

/* RGB and RGBA */
color: rgb(99, 102, 241);
color: rgba(99, 102, 241, 0.8);

/* HSL — most intuitive for adjusting */
color: hsl(239, 84%, 67%);         /* indigo */
color: hsl(239, 84%, 57%);         /* darker */
color: hsl(239, 84%, 77%);         /* lighter */

CSS Custom Properties

:root {
    --primary:    #6366f1;
    --success:    #10b981;
    --danger:     #ef4444;
    --text:       #1e293b;
    --bg:         #ffffff;
    --card-bg:    #f8fafc;
    --border:     #e2e8f0;
    --space-2:    8px;
    --space-4:    16px;
    --space-6:    24px;
    --radius:     8px;
}
.btn-primary {
    background: var(--primary);
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius);
}

Dark Mode with CSS Variables

:root {
    --bg:      #ffffff;
    --text:    #1e293b;
    --card-bg: #f8fafc;
}

/* System dark mode */
@media (prefers-color-scheme: dark) {
    :root {
        --bg:      #0f172a;
        --text:    #f1f5f9;
        --card-bg: #1e293b;
    }
}

/* Manual toggle with .dark class on html */
html.dark {
    --bg:      #0f172a;
    --text:    #f1f5f9;
    --card-bg: #1e293b;
}

body  { background: var(--bg); color: var(--text); }
.card { background: var(--card-bg); }

Q: What is the difference between CSS variables and Sass variables?

CSS custom properties are live in the browser — changed at runtime with JavaScript, scoped to elements, work with media queries. Sass variables are compile-time only — replaced during build, cannot be changed dynamically. Use CSS custom properties for theming; use Sass variables for build-time constants like breakpoints.

🧩
Test Your Knowledge
50 questions · Takes ~1500 seconds

Answer these questions to confirm you understood the article. Instant feedback on every answer.

Q1
What is the primary purpose of this topic in DevOps development?
Q2
In a DevOps technical interview, what do interviewers test?
Q3
What is the time/space complexity consideration for this DevOps topic?
Q4
When should a DevOps developer apply this technique?
Q5
What is the most common mistake when using this DevOps pattern?
Q6
Best practice for DevOps concept #6?
Q7
Best practice for DevOps concept #7?
Q8
Best practice for DevOps concept #8?
Q9
Best practice for DevOps concept #9?
Q10
Best practice for DevOps concept #10?
Q11
Best practice for DevOps concept #11?
Q12
Best practice for DevOps concept #12?
Q13
Best practice for DevOps concept #13?
Q14
Best practice for DevOps concept #14?
Q15
Best practice for DevOps concept #15?
Q16
Best practice for DevOps concept #16?
Q17
Best practice for DevOps concept #17?
Q18
Best practice for DevOps concept #18?
Q19
Best practice for DevOps concept #19?
Q20
Best practice for DevOps concept #20?
Q21
Best practice for DevOps concept #21?
Q22
Best practice for DevOps concept #22?
Q23
Best practice for DevOps concept #23?
Q24
Best practice for DevOps concept #24?
Q25
Best practice for DevOps concept #25?
Q26
Best practice for DevOps concept #26?
Q27
Best practice for DevOps concept #27?
Q28
Best practice for DevOps concept #28?
Q29
Best practice for DevOps concept #29?
Q30
Best practice for DevOps concept #30?
Q31
Best practice for DevOps concept #31?
Q32
Best practice for DevOps concept #32?
Q33
Best practice for DevOps concept #33?
Q34
Best practice for DevOps concept #34?
Q35
Best practice for DevOps concept #35?
Q36
Best practice for DevOps concept #36?
Q37
Best practice for DevOps concept #37?
Q38
Best practice for DevOps concept #38?
Q39
Best practice for DevOps concept #39?
Q40
Best practice for DevOps concept #40?
Q41
Best practice for DevOps concept #41?
Q42
Best practice for DevOps concept #42?
Q43
Best practice for DevOps concept #43?
Q44
Best practice for DevOps concept #44?
Q45
Best practice for DevOps concept #45?
Q46
Best practice for DevOps concept #46?
Q47
Best practice for DevOps concept #47?
Q48
Best practice for DevOps concept #48?
Q49
Best practice for DevOps concept #49?
Q50
Best practice for DevOps concept #50?
EzyCoders Admin
Written by
EzyCoders Admin

Team Lead and Full-Stack Developer with experience in PHP, JavaScript, SQL, DSA, and System Design. Passionate about software engineering, scalable web technologies, and helping developers prepare for coding interviews and tech careers through practical tutorials and professional guidance.

Comments (0)

No comments yet. Be the first!

Leave a Comment