CSS Variables

Introduction

CSS Variables, also known as custom properties, allow you to store values and reuse them throughout your stylesheet.

👉 They help you write clean, reusable, and maintainable CSS.

What You’ll Learn

  • What are CSS variables
  • How to declare variables
  • How to use var()
  • Benefits of variables
  • Real-world usage

What are CSS Variables?

CSS variables are used to store values like colors, fonts, and spacing.

👉 Instead of repeating values, you define them once and reuse them.

🔹 Creating a Variable

Variables are declared using -- (double dash).

:root {
  --primary-color: blue;
  --font-size: 16px;
}

👉 :root represents the top-level element (HTML document).

🔹 Using a Variable

Use the var() function to apply variables.

p {
  color: var(--primary-color);
  font-size: var(--font-size);
}

👉 This makes your CSS more flexible.

🔹 Why Use CSS Variables?

✔ Reusability

Define once, use many times

✔ Easy updates

Change value in one place

✔ Cleaner code

Less repetition

Without variables:

h1 {
  color: blue;
}

button {
  background-color: blue;
}

If you change brand color, you must update everywhere.

With variables:

:root {
  --primary-color: blue;
}

h1 {
  color: var(--primary-color);
}

button {
  background-color: var(--primary-color);
}

Now change only once.

🔹 Using Variables Inside Variables

:root {
  --base-size: 16px;
  --heading-size: calc(var(--base-size) * 2);
}

👉 You can combine variables with functions like calc().

🔹 Local vs Global Variables

Global (using :root)

:root {
  --color: red;
}

👉 Available everywhere

Local Variables

.card {
  --color: green;
  color: var(--color);
}

👉 Works only inside .card

🔹 Multiple Variables Example

:root {
  --primary-color: #3498db;
  --secondary-color: #2ecc71;
  --font-size-large: 24px;
}

Use them:

h1 {
  color: var(--primary-color);
  font-size: var(--font-size-large);
}

🔹 Fallback Value

color: var(--main-color, black);

If variable not found → use black.

🔹 Organizing Variables (Important for Projects)

Group related variables together.

:root {
  /* Colors */
  --primary-color: #3498db;
  --text-color: #333;  /* Spacing */
  --padding: 10px;
  --margin: 20px;
}

👉 This makes your CSS structured and easy to manage.

🔹 Real-World Usage

CSS variables are used in:

  • Theme systems (light/dark mode)
  • Design systems
  • Reusable UI components
  • Large projects

👉 Very important for modern web development.

🔹 Modern Usage: Dark Mode Example

:root {
  --bg-color: white;
  --text-color: black;
}

.dark-mode {
  --bg-color: black;
  --text-color: white;
}

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

👉 Switching class changes entire theme.

🔹 Real-World Example: Button System

:root {
  --btn-bg: blue;
  --btn-color: white;
}button {
  background-color: var(--btn-bg);
  color: var(--btn-color);
}

👉 Changing variables updates all buttons instantly.

🔹 Advantages of CSS Variables

  • Easy to update styles
  • Reduces repetition
  • Improves code readability
  • Makes design consistent
  • Useful in large projects

🔹 Limitations of CSS Variables

  • Can be confusing for beginners
  • Overuse can make code complex
  • Not supported in very old browsers

👉 Use them wisely.

🔹 When to Use CSS Variables

Use variables when:

  • You repeat values frequently
  • You build reusable components
  • You want theme switching
  • You work on large projects

Avoid when:

  • Small one-time styles
  • Very simple pages

🔹 Performance and Flexibility

  • Reduce duplicate code
  • Easier maintenance
  • Better scalability

👉 Especially useful in large websites.

🔹 Common Mistakes

  • Forgetting -- in variable names
  • Using variables without var()
  • Overusing too many variables
  • Not organizing variables properly

Practical Tasks

Task 1: Basic Variables

Create variables for:

  • Color
  • Font size

Task 2: Theme Change

Change one variable and update entire design.

Task 3: Local Variables

Use variables inside a class.

Task 4: Dark Mode

Create light/dark theme using variables.

Task 5: Mini Project

Create a UI with:

  • Buttons
  • Text
  • Background

👉 Use variables for all values

Task 6: Fallback Value

Use var() with fallback.

Task 7: Spacing System

Create spacing variables and apply them.

Task 8: Button Theme

Create buttons using variables.

Task 9: Organize Variables

Group variables into sections.

Important Points to Remember

✔ Variables start with --
✔ Use var() to access them
:root is used for global variables
✔ Makes CSS reusable and scalable

Conclusion

CSS variables make your code cleaner, reusable, and easier to manage. They are an essential part of modern CSS and are widely used in real-world projects.

👉 Use variables in all projects
👉 Keep them organized
👉 Build scalable designs

Related Tutorials

  • CSS Introduction

    Introduction CSS stands for Cascading Style Sheets. It is used to style and design web pages created with HTML. While HTML is used to structure content, CSS is used to control how that content looks on the screen. With CSS, you can change colors, fonts, layouts, spacing, and overall appearance of a website. In this…

  • CSS Syntax & Selectors

    Introduction CSS syntax defines how styles are written and applied to HTML elements. Selectors are used to target specific elements on a webpage and apply styles to them. Understanding CSS syntax and selectors is essential for writing effective and clean CSS code. In this lesson, you will learn the basic structure of CSS and different…

  • Colors, Units & Backgrounds

    Introduction CSS allows you to control the appearance of web pages using colors, units, and background properties. These properties are essential for creating visually appealing, readable, and responsive websites. Colors help you define the appearance of text, borders, and other elements. CSS units control sizes such as width, height, padding, margin, and font size. Background…

  • CSS Borders & Box Model

    Introduction CSS borders and the box model are essential concepts for designing layouts and controlling spacing in web development. Every HTML element is treated as a rectangular box, and understanding how this box works helps you create clean and structured designs. In this lesson, you will learn how to style borders and understand the CSS…

  • CSS Display & Position

    Introduction CSS display and position properties control how elements are shown and placed on a web page. These properties are essential for creating layouts, aligning elements, and building modern web designs. In this lesson, you will learn how different display types work and how positioning helps control element placement. What You’ll Learn 🔹 CSS Display…

  • CSS Flexbox

    Introduction CSS Flexbox is a powerful layout system used to design flexible and responsive layouts. It makes it easy to align items, distribute space, and create modern UI designs without using complex positioning or floats. Flexbox works on a one-dimensional layout (row or column) and is widely used in real-world web development. Why learn Flexbox?…

Leave a Reply

Your email address will not be published. Required fields are marked *