SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
Lesson 30 of 30

CSS Variables

4 min read · Free · No login needed

Imagine your website uses the same green colour in twenty places — headings, buttons, links, borders, and the header background. Then one day you want to change that green to blue. Without variables, you must hunt down all twenty places and change each one, hoping you do not miss any. With CSS variables, you change one line and the whole site updates at once.

Diagram — see how it fits together
Diagram: CSS Variables

A CSS variable, also called a custom property, is a value you store under a name and reuse anywhere in your stylesheet. You define it once, and every rule that uses it picks up the value automatically.

Defining and using a variable

A variable name always starts with two dashes, like --main-color. The usual place to define it is :root, which means the top of the document — a variable defined there is available everywhere. You read the value back with the var() function.

Syntax — one colour, used everywhere

:root {
  --main-color: #04aa6d;
  --dark-color: #333;
}
h1 { color: var(--main-color); }
button {
  background-color: var(--main-color);
  color: white;
}

Now the heading and the button share the same green. Change --main-color in :root from green to blue, and both change together. That single line is the whole theme.

Changing a theme with one value

Because a variable is just a value, you can build an entire colour theme from a handful of them: --main-color for the brand colour, --bg-color for page backgrounds, --text-color for text, and --accent-color for highlights. A dark section of the page can even redefine the same variables locally — inside that section, --bg-color becomes dark and --text-color becomes white, while the rest of the page stays light.

.dark-box {
  --bg-color: #333;
  --text-color: white;
  background-color: var(--bg-color);
  color: var(--text-color);
}
Note: A variable only works inside the element where it is defined and its children. A variable defined in :root works everywhere, but one defined inside .dark-box only works inside that box. This is why :root is the standard place for theme values.
Tip: Name variables by their purpose, not by their value — --main-color is better than --green. If the brand colour changes from green to blue one day, --green holding a blue value would confuse everyone who reads the code.

Common mistakes

  • Forgetting the two dashes — main-color without -- is not a variable, and the browser will ignore it.
  • Writing var(main-color) without the dashes inside var() — the name inside must match exactly, dashes included.
  • Defining a variable inside one element and trying to use it somewhere else on the page — it only works in that element and its children.
  • Naming a variable after its value, like --green — when the colour changes, the name lies.
  • Forgetting that variable names are case-sensitive — --Main-Color and --main-color are two different variables.

Remember

  • A CSS variable is a custom property: a value stored under a name starting with --.
  • Define theme variables in :root so the whole page can use them.
  • Read a variable with var(--name), for example var(--main-color).
  • Change one value in :root and every rule using it updates at once.
  • Name variables by purpose (--main-color), not by value (--green).

Example explained

  • :root { --main-color: #04aa6d; } — the theme is defined once at the top of the document: the brand green, a dark grey, a light background, and a text colour.
  • h1 { color: var(--main-color); } — the heading reads the brand colour from the variable instead of writing the colour code directly.
  • button { background-color: var(--main-color); } — the button uses the very same variable, so the heading and the button always match.
  • .dark-box { --bg-color: #333; --text-color: white; } — inside the dark box the background and text variables are redefined, so only that box turns dark while the rest of the page stays light.
  • var(--main-color, blue) — the second value inside var() is a fallback: if the variable were ever missing, blue would be used instead.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>CSS Variables Theme</title>
<style>
:root {
  --main-color: #04aa6d;
  --dark-color: #333;
  --bg-color: #f2f2f2;
  --text-color: #333;
}
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: var(--bg-color); color: var(--text-color); }
h1 { color: var(--main-color); text-align: center; }
.card {
  background-color: white;
  max-width: 500px;
  margin: 20px auto;
  padding: 25px;
  border-radius: 10px;
  text-align: center;
}
button {
  background-color: var(--main-color);
  color: white;
  padding: 12px 24px;
  border: none;
  border-radius: 6px;
  font-size: 18px;
  cursor: pointer;
}
.dark-box {
  --bg-color: #333;
  --text-color: white;
  background-color: var(--bg-color);
  color: var(--text-color);
  max-width: 500px;
  margin: 20px auto;
  padding: 25px;
  border-radius: 10px;
  text-align: center;
}
.dark-box button { background-color: var(--main-color, blue); }
</style>
</head>
<body>

<h1>One Change, Whole Theme</h1>

<div class="card">
<h2>Light Card</h2>
<p>This card uses the theme colours from :root. Change --main-color once and the heading and button change together.</p>
<button>Join Now</button>
</div>

<div class="dark-box">
<h2>Dark Box</h2>
<p>This box redefines --bg-color and --text-color locally, so only this box turns dark.</p>
<button>Learn More</button>
</div>

</body>
</html>

Output

Output — how it looks in the browser
Output of CSS Variables

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 30 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

AI Power Skills in 2 Days | Learn AI Text, Image, Video and Voice Tools
View
Full Stack PHP Development Course 2026
₹3,000
View
Master Python + AI Skills in 30 Days
₹499
View
Browse all courses
CSS — Lessons
1. CSS Introduction 2. CSS Syntax 3. CSS Selectors 4. CSS How To 5. CSS Colors 6. CSS Backgrounds 7. CSS Borders 8. CSS Margins 9. CSS Padding 10. CSS Height and Width 11. CSS Box Model 12. CSS Text 13. CSS Fonts 14. CSS Links 15. CSS Lists 16. CSS Tables 17. CSS Display 18. CSS Position 19. CSS Overflow 20. CSS Float 21. CSS Pseudo-classes 22. CSS Pseudo-elements 23. CSS Forms 24. CSS Navigation Bar 25. CSS Website Layout 26. CSS Flexbox 27. CSS Grid 28. CSS Responsive 29. CSS Transitions and Animations 30. CSS Variables
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🛡️ 7-Day Money-Back Guarantee
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Download karne ke liye login karo. Login ke baad yahi page pe rahoge!

Naya ho? Register karo →