March 12, 2023 (1y ago)
In modern web development, creating dynamic, maintainable, and scalable stylesheets is essential. CSS variables, also known as custom properties, have revolutionized the way we write CSS by enabling us to store values in variables. This makes it easier to manage and update styles across a project.
CSS variables allow you to define a value once and reuse it throughout your stylesheet. They are declared within a CSS rule that contains a --
prefix, and they can be accessed using the var()
function.
/* Define a CSS variable */
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--font-size-large: 2rem;
}
/* Use the CSS variable */
body {
background-color: var(--primary-color);
font-size: var(--font-size-large);
}
button {
background-color: var(--secondary-color);
}
:root
SelectorThe :root
selector represents the document's root element and is higher in the hierarchy than the html
element. By defining variables in :root
, you ensure they are globally accessible throughout your stylesheet.
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--font-size-base: 1rem;
--spacing-unit: 8px;
}
CSS variables are perfect for storing commonly used values like colors, font sizes, and spacing units. This makes it easy to maintain a consistent design language.
body {
color: var(--primary-color);
font-size: var(--font-size-base);
margin: var(--spacing-unit);
}
h1 {
color: var(--secondary-color);
margin-bottom: calc(var(--spacing-unit) * 2);
}
CSS variables inherit like other CSS properties. This means you can define a variable in a parent element and reuse or override it in child elements.
.container {
--container-padding: 16px;
padding: var(--container-padding);
}
.container .child {
padding: calc(var(--container-padding) / 2);
}
When using CSS variables, you can provide a fallback value in case the variable is not defined. This ensures your styles remain robust.
button {
background-color: var(--button-bg, #333); /* #333 is the fallback */
}
CSS variables can be dynamically updated within media queries, making responsive design more manageable.
:root {
--font-size-responsive: 16px;
}
@media (min-width: 600px) {
:root {
--font-size-responsive: 18px;
}
}
body {
font-size: var(--font-size-responsive);
}
CSS variables can be used in animations, providing more control and flexibility.
:root {
--rotation-angle: 360deg;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(var(--rotation-angle));
}
}
.element {
animation: rotate 2s infinite;
}
One of the most powerful uses of CSS variables is theming. You can easily switch themes by updating the variable values.
/* Light theme */
:root {
--background-color: #fff;
--text-color: #000;
}
/* Dark theme */
[data-theme='dark'] {
--background-color: #000;
--text-color: #fff;
}
body {
background-color: var(--background-color);
color: var(--text-color);
}
CSS variables are a powerful tool that can significantly enhance your stylesheets by making them more dynamic, maintainable, and scalable. By leveraging custom properties effectively, you can create a consistent and adaptable design system that improves both development efficiency and user experience. Start incorporating CSS variables into your projects today to see the benefits firsthand!
Pure CSS text effect codepen.io/thebabydino/de… No text duplication, no JS, no images save for CSS gradients, no SVG at all. Under 20 CSS declarations, including layout, prettifying, `@media` queries, `@supports` blocks, CSS variable declarations... No Firefox (bug 🪲 1481498).