CSS Variables, also known as Custom Properties, allow you to define reusable values that can be applied throughout your stylesheets. They enhance maintainability and flexibility by making it easier to manage theme changes and modify large-scale designs.
:root
or any other) using the --
prefix.Variables are defined using the --variable-name
syntax. Typically, they are placed inside the :root
selector for global access.
:root {
--primary-color: #3498db;
--font-size-large: 24px;
}
--primary-color
variable stores the color #3498db
.--font-size-large
variable stores the font size 24px
.:root
means that these variables can be accessed globally.Once defined, a variable can be referenced using the var()
function.
h1 {
color: var(--primary-color);
font-size: var(--font-size-large);
}
In this example, the h1
element will have a color of #3498db
and a font size of 24px
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
:root {
--main-bg-color: lightblue;
--text-color: darkblue;
--padding: 20px;
}
body {
background-color: var(--main-bg-color);
color: var(--text-color);
padding: var(--padding);
}
</style>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph using CSS variables.</p>
</body>
</html>
--main-bg-color
.--text-color
.--padding
.--primary-color
).Reusability: Define common values like colors, padding, and font sizes once and reuse them across multiple selectors.
CSS variables follow the standard inheritance rules. If a variable is declared within a specific element, it will only be available within that element and its children. Variables defined within the :root
selector (or globally) are available throughout the document.
:root {
--text-color: black;
}
.section {
--text-color: green;
}
p {
color: var(--text-color);
}
--text-color
is black..section
, the --text-color
is redefined as green, so any paragraph inside .section
will have green text. Elsewhere, paragraphs will have black text.CSS variables can have fallback values to ensure compatibility or handle situations where a variable might be undefined.
p {
color: var(--secondary-color, red); /* If --secondary-color is undefined, red will be used */
}
--secondary-color
is not defined, the text color will default to red.One of the major advantages of CSS variables is that they can be manipulated using JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
:root {
--bg-color: lightgray;
}
body {
background-color: var(--bg-color);
}
</style>
</head>
<body>
<button onclick="changeColor()">Change Background Color</button>
<script>
function changeColor() {
document.documentElement.style.setProperty('--bg-color', 'lightcoral');
}
</script>
</body>
</html>
lightgray
.--bg-color
variable to lightcoral
, changing the background color dynamically.CSS variables can be used inside media queries, making them highly flexible for responsive design.
:root {
--font-size: 16px;
}
@media (min-width: 768px) {
:root {
--font-size: 18px;
}
}
body {
font-size: var(--font-size);
}
16px
.768px
, the variable --font-size
is updated to 18px
, automatically adjusting the font size based on the screen width.Calculated Values: CSS variables do not directly support arithmetic operations like calc()
inside the variable definition itself. Calculations are done when the variable is used, not when it’s defined.
Example:
:root {
--box-size: 20px;
}
div {
width: calc(var(--box-size) * 2); /* Calculation happens when used */
}
CSS variables offer great flexibility and reusability for modern web development. They improve code maintainability and allow for dynamic, theme-based designs. Understanding how to define and apply custom properties gives you the ability to manage your CSS more effectively, especially in large-scale projects or when implementing themes and responsive designs.
next -> CSS Media Queries