Hackforge Academy

Category: html_css

CSS variables

Published on 29 Jun 2026

Explanation

CSS variables, also called custom properties, allow you to store reusable values such as colors, fonts, and spacing.

Code:

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

Explanation

Variables are accessed using the var() function, making styles easier to maintain.

Code:

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

Explanation

Multiple variables can be defined in the :root selector for use throughout the entire website.

Code:

:root {
  --primary: #2563eb;
  --secondary: #10b981;
  --padding: 20px;
}

Explanation

CSS variables simplify theme changes because updating the variable automatically updates every element that uses it.

Code:

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

Explanation

Variables can also include fallback values if the custom property is not defined.

Code:

p {
  color: var(--text-color, black);
}

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community