---
title: "Explain the CSS Custom Properties"  
description: "By creating CSS custom property you can make your CSS more manageable and maintainable, especially for large projects."  
author: "Ashutosh Patel"  
published: 2024-06-11  
updated: 2024-06-11  
canonical: https://www.mindstick.com/blog/304343/explain-the-css-custom-properties  
category: "css-css3"  
tags: ["css-css3", "css", "css property"]  
reading_time: 3 minutes  

---

# Explain the CSS Custom Properties

### CSS Custom Property

CSS [custom properties](https://www.mindstick.com/forum/157966/what-is-the-role-of-css-custom-properties-in-bootstrap-5), [often called](https://answers.mindstick.com/qa/100590/which-country-is-often-called-the-land-of-the-rising-sun) CSS variables, can store reusable values ​​throughout CSS. This can make your CSS more manageable and maintainable, especially for [large projects](https://www.mindstick.com/forum/159481/how-does-c-plus-plus-handle-namespaces-and-what-is-their-significance-in-large-projects).

**Syntax**\
The `--` prefix is ​​used to define custom properties, and the `var()` function to define custom properties.

**For example**\
**Defining Custom Properties**\
You can define custom properties at different levels: globally in the `:root` pseudo-class, or locally in specific selectors.

```css
:root {
   --primary-color: #3498db;
   --secondary-color: #2ecc71;
   --font-size: 16px;
   --padding: 10px;
}
```

**Using Custom Properties**\
You can use custom properties in other CSS properties.

```css
body {
   font-size: var(--font-size);
   color: var(--primary-color);
}
button {
   background-color: var(--secondary-color);
   padding: var(--padding);
   border: none;
   color: white;
   cursor: pointer;
}
button:hover {
   background-color: var(--primary-color);
}
```

#### Advanced Example

Let's create a more complex example with a simple theming system.

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS Custom Properties Example</title>
    <style>
        /* Define css custom properties for light theme */
        :root {
            --background-color: white;
            --text-color: black;
            --button-bg-color: #3498db;
            --button-text-color: white;
            --button-hover-bg-color: #2980b9;
            --padding: 10px;
            --border-radius: 5px;
        }

        /* Define css custom properties for dark theme */
        body.dark-theme {
            --background-color: #2c3e50;
            --text-color: #ecf0f1;
            --button-bg-color: #e74c3c;
            --button-text-color: white;
            --button-hover-bg-color: #c0392b;
        }

        /* Apply the css custom properties */
        body {
            background-color: var(--background-color);
            color: var(--text-color);
            font-family: Arial, sans-serif;
            padding: var(--padding);
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
        }

        h1 {
            font-size: 2em;
        }

        button {
            background-color: var(--button-bg-color);
            color: var(--button-text-color);
            padding: var(--padding);
            border: none;
            border-radius: var(--border-radius);
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: var(--button-hover-bg-color);
        }

    </style>
</head>
<body>
    <div class="container">
        <h1>Defining CSS Custom Property</h1>
        <button>Click Here</button>
    </div>
</body>
</html>
```

#### In the above example

\
**Custom Properties Definition**

The global custom [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) for the light theme are defined in the `:root selector`.\
Additional custom properties for the dark theme are defined in the `.dark-theme` class used in the body.

## Applying Custom Properties

Custom properties are used throughout the CSS including the `var()` function.\
`Background-color`, `color`, `padding`, and other elements are set strategically by these custom elements.

## Theming

By turning on the `.dark-theme` class on the `body`element, you can switch light and dark themes without changing individual CSS properties. This makes it easier to manage and update topics.

**JavaScript [Interaction](https://yourviews.mindstick.com/view/80771/technology-is-destroying-human-interaction-how)**

You can dynamically change custom properties using JavaScript, adding interactivity and dynamic styling to your [web applications](https://www.mindstick.com/blog/11464/improve-your-understanding-of-web-applications).

## Output-

## Light Theme

![Explain the CSS Custom Properties](https://www.mindstick.com/blogs/8c02e6b7-bf6f-4da1-a64c-50f87b4567da/images/9ff91d15-3c86-4c35-bab2-82b79e0e3f42.jpg)

**Dark Theme![Explain the CSS Custom Properties](https://www.mindstick.com/blogs/8c02e6b7-bf6f-4da1-a64c-50f87b4567da/images/09b90037-dfd8-4f13-851c-fe37b214f7a1.jpg)**

The **JavaScript** turns the **dark theme** **on** and **off** when a [button is clicked](https://www.mindstick.com/forum/157821/write-a-jquery-code-that-selects-all-the-checkboxes-in-a-form-when-a-select-all-button-is-clicked), demonstrating how easily you can change the theme with custom properties.

[CSS custom](https://www.mindstick.com/forum/160673/css-custom-properties-variables-vs-preprocessor-variables) properties provide a powerful way to manage styles, making your CSS more flexible and manageable. They are especially useful for themes, [functional](https://www.mindstick.com/articles/13005/the-functional-aspects-of-laser-technology) concepts, and strategic complexity.

**Also, Read:** [Explain CSS animation with an example](https://www.mindstick.com/blog/304341/explain-css-animation-with-an-example)

---

Original Source: https://www.mindstick.com/blog/304343/explain-the-css-custom-properties

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
