CSS Variables, also known as custom properties, allow you to store values that you can reuse throughout your CSS. They are defined using the --
prefix and can be used to make your CSS more maintainable and easier to update.
Here’s an example of defining and using a CSS variable:
CSS Code:
/* Define CSS Variables */
:root {
--main-bg-color: lightblue;
--text-color: darkblue;
}
/* Use CSS Variables */
.box {
background-color: var(--main-bg-color);
color: var(--text-color);
}
In this example, we'll use CSS variables to control the size of elements dynamically:
CSS Code:
/* Define CSS Variables */
:root {
--main-bg-color: lightgreen;
--text-color: darkgreen;
}
/* Use CSS Variables */
.box {
background-color: var(--main-bg-color);
color: var(--text-color);
}
.box-large {
width: 200px;
height: 200px;
}