Defining variables (--variable-name: value;)
CSS custom properties, more commonly known as CSS variables, allow you to store specific values for reuse throughout your stylesheets. By defining a variable in a central place, you can easily manage and update values like colors, fonts, or sizes, which promotes cleaner and more maintainable code. To declare a global variable, you define it within the :root
pseudo-class, making it accessible across the entire document.
Example 1: Basic Color Variable
/* Define a global variable for the primary color */
:root {
--primary-color: #3498db;
}
/* Use the variable for a button's background color */
.button {
background-color: var(--primary-color);
color: white;
padding: 10px 20px;
}
Explanation: This code defines a global variable named --primary-color
with a blue value. The .button
class then uses this variable for its background-color
, ensuring brand consistency.
Example 2: Font Size Variable
/* Define a global variable for the base font size */
:root {
--base-font-size: 16px;
}
/* Apply the font size to the body */
body {
font-size: var(--base-font-size);
}
/* Use a calculated value for a larger heading */
h1 {
font-size: calc(var(--base-font-size) * 2);
}
Explanation: Here, --base-font-size
is set to 16px
. This variable is then applied directly to the body
and used in a calc()
function to make the h1
element twice the base size.
Example 3: Spacing Variable
/* Define a global variable for standard spacing */
:root {
--standard-spacing: 15px;
}
/* Use the variable for margin on a container */
.container {
margin: var(--standard-spacing);
}
/* Use the variable for padding on a card */
.card {
padding: var(--standard-spacing);
}
Explanation: This example establishes a consistent spacing unit with the --standard-spacing
variable. It's then applied to both the margin
of a container and the padding
of a card for a uniform layout.
Example 4: Border Style Variable
/* Define a global variable for a common border style */
:root {
--default-border: 1px solid #ccc;
}
/* Apply the border to an input field */
input {
border: var(--default-border);
}
/* Apply the border to an image */
img {
border: var(--default-border);
}
Explanation: The --default-border
variable stores a complete border definition. This makes it easy to apply a consistent border style to multiple elements like input
fields and img
tags.
Example 5: Background Color Variable
/* Define a global variable for the page's background color */
:root {
--background-color: #f4f4f4;
}
/* Apply the background color to the body */
body {
background-color: var(--background-color);
}
Explanation: A variable named --background-color
is created to hold the background color for the page. This allows for quick and easy theme changes by updating a single line of code.
Example 6: Box Shadow Variable
/* Define a global variable for a subtle box shadow */
:root {
--box-shadow-light: 0 2px 4px rgba(0,0,0,0.1);
}
/* Apply the box shadow to a card component */
.card {
box-shadow: var(--box-shadow-light);
}
Explanation: This code defines --box-shadow-light
to store a reusable box-shadow
value. Applying this variable to elements like .card
ensures a consistent shadow effect across the site.
Example 7: Overriding a Variable
/* Define a global theme color */
:root {
--theme-color: #3498db; /* Blue */
}
/* Define a special section with a different theme color */
.special-section {
--theme-color: #e74c3c; /* Red */
}
/* Style a button within the special section */
.special-section .button {
background-color: var(--theme-color);
}
Explanation: This example shows the power of variable scope. A global --theme-color
is set, but it is overridden within the .special-section
. Any element inside this section that uses --theme-color
will now use the new value.