Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I use CSS variables to create a responsive color scheme?
Asked on May 04, 2026
Answer
CSS variables, also known as custom properties, allow you to define reusable values that can be adjusted for responsive design. You can use them to create a flexible color scheme that adapts to different screen sizes.
<!-- BEGIN COPY / PASTE -->
<style>
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
}
@media (max-width: 600px) {
:root {
--primary-color: #9b59b6;
--secondary-color: #e74c3c;
}
}
body {
background-color: var(--primary-color);
color: var(--secondary-color);
}
</style>
<body>
<h1>Responsive Color Scheme</h1>
<p>This text will change color based on screen size.</p>
</body>
<!-- END COPY / PASTE -->Additional Comment:
- CSS variables are defined using the "--" prefix and can be accessed using the "var()" function.
- The ":root" selector is commonly used to define global CSS variables.
- Media queries adjust the variable values based on screen size, allowing for a responsive design.
- This example changes the color scheme when the viewport width is 600px or less.
Recommended Links:
