codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎨

HTML & CSS

31 / 59 topics
28CSS Animation29CSS Transitions30CSS Transforms31CSS Variables32CSS Pseudo-elements33CSS Pseudo-classes
Tutorials/HTML & CSS/CSS Variables
🎨HTML & CSS

CSS Variables

Updated 2026-05-15
10 min read

CSS Variables

Introduction

In the world of web development, maintaining consistent styles across a large codebase can be challenging. This is where CSS variables come in handy. Also known as custom properties, they allow you to define reusable values that can be referenced throughout your stylesheet. This not only makes your CSS more maintainable but also enhances theming capabilities.

Concept

CSS variables are defined using the -- prefix followed by a name of your choice. They are declared at any level in the CSS hierarchy—global, within a class, or even inline—and can be accessed and modified using the var() function.

Declaring Variables

Variables can be declared globally in the :root selector, which targets the root element (<html>), making them available throughout your entire document. Alternatively, you can declare them within specific selectors to limit their scope.

:root {
  --primary-color: #3498db;
  --font-size: 16px;
}

.header {
  background-color: var(--primary-color);
  color: white;
  font-size: var(--font-size);
}

### Accessing Variables

To use a CSS variable, you simply call it using the `var()` function. For example:

```css
body {
  font-size: var(--font-size);
  background-color: var(--background-color, #f0f0f0); /* Default value if --background-color is not defined */
}

### Modifying Variables

CSS variables can be modified at any time using JavaScript or directly in the CSS. This makes them highly dynamic and responsive to user interactions.

```javascript
document.documentElement.style.setProperty('--primary-color', '#e74c3c');

Examples

Let's dive into some practical examples to illustrate how CSS variables can be used effectively.

Example 1: Basic Theming

Suppose you want to create a simple theming system where users can switch between light and dark modes. You can define variables for colors and toggle their values based on user preference.

:root {
  --background-color: #ffffff;
  --text-color: #000000;
}

.dark-mode {
  --background-color: #333333;
  --text-color: #ffffff;
}

```html
<div class="theme-switcher">
  <button onclick="document.documentElement.classList.toggle('dark-mode')">Toggle Theme</button>
</div>

<div class="content">
  <p>This is a sample text.</p>
</div>

Example 2: Responsive Typography

CSS variables can also be used to create responsive typography. By defining variables for font sizes and using media queries, you can adjust the values based on screen size.

:root {
  --base-font-size: 16px;
}

@media (min-width: 768px) {
  :root {
    --base-font-size: 18px;
  }
}

body {
  font-size: var(--base-font-size);
}

Example 3: Dynamic Backgrounds

You can use CSS variables to create dynamic backgrounds that change based on user interactions or other conditions.

:root {
  --background-gradient-start: #ff7e5f;
  --background-gradient-end: #feb47b;
}

.dynamic-background {
  background: linear-gradient(to right, var(--background-gradient-start), var(--background-gradient-end));
  transition: background 0.3s ease;
}

.dynamic-background:hover {
  --background-gradient-start: #feb47b;
  --background-gradient-end: #ff7e5f;
}

What's Next?

In the next section, we will explore HTML Entities, which are used to display special characters in HTML documents. This knowledge will be essential for creating well-formed and error-free HTML content.

Stay tuned!


PreviousCSS TransformsNext CSS Pseudo-elements

Recommended Gear

CSS TransformsCSS Pseudo-elements