bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/CSS/Advanced Styling
CSS•Advanced Styling

CSS Overriding Variables

Override Global Variable With Local Variable

from the earlier example we have learned that global variables can be accessed through the entire document, while local variables can be used only inside the selector where it is declared.

Sometimes we want the variables to change only in a specific section of the page.

Assume we want a different border color for <p class="note"> elements. Then, we can re-declare the --primary-bg-color variable inside the .note selector. When we use var(--primary-bg-color) inside this selector, it will use the local --primary-bg-color variable value declared here.

We see that the local --primary-bg-color variable will override the global --primary-bg-color variable for the .note selector:

Example

Formatted code
:root {
  --primary-bg-color: #1e90ff;
  --primary-color: #ffffff;
}
body {
  background-color: var(--primary-bg-color);
} .container {
color: var(--primary-bg-color);
background-color: var(--primary-color);
padding: 15px;
} .container h2 {
border-bottom: 2px solid
var(--primary-bg-color);
} .container .note {
--primary-bg-color: red; /* local variable will override global */
border: 1px solid var(--primary-bg-color);
padding: 10px;
}

Live preview

Add a New Local Variable

If a variable is going to be used only one single place, we could also have declared a new local variable, like this:

Example

Formatted code
:root {
  --primary-bg-color: #1e90ff;
  --primary-color: #ffffff;
}
body {
  background-color: var(--primary-bg-color);
} .container {
color: var(--primary-bg-color);
background-color: var(--primary-color);
padding: 15px;
} .container h2 {
border-bottom: 2px solid
var(--primary-bg-color);
} .container .note {
--note-border-color: red; /* new local variable */
border: 1px solid
var(--note-border-color);
padding: 10px;
}

Live preview

CSS var() Function

FunctionDescription
var()Inserts the value of a CSS variable

Previous

CSS Variables - var() Function

Next

CSS Variables and JavaScript

This chapter

Overview
53

Lessons

154m

Read time

1. CSS Rounded Corners2. Sass Tutorial3. CSS Border Images4. CSS Multiple Backgrounds5. CSS Background Size6. CSS Background Origin7. CSS Background Clip8. CSS Colors9. CSS Color Keywords10. CSS Gradients11. CSS Radial Gradients12. CSS Conic Gradients13. CSS Shadow Effects14. CSS Box Shadow15. CSS Text Effects16. CSS Custom Fonts17. CSS 2D Transforms18. CSS 2D Transform Scale19. CSS 2D Transform Skew / Matrix20. CSS 3D Transforms21. CSS Transitions22. CSS Transition Timing23. CSS Animations24. CSS Animation Timing25. CSS Animation Properties26. CSS Tooltip27. CSS Tooltip Arrows28. CSS Styling Images29. CSS Image Effects30. CSS Image Hover Overlays31. CSS Responsive Modal Images32. CSS Centering Images33. CSS Image Filter Effects34. CSS Image Shapes35. CSS object-fit Property36. CSS object-position Property37. CSS Masking38. CSS Gradient Mask Layers39. CSS SVG Mask Layers40. CSS Styling Buttons41. CSS Button Hover Effects42. CSS Button Groups43. CSS Pagination Styles44. CSS Multiple Columns45. CSS Column Rules46. CSS User Interface47. CSS Variables - var() Function48. CSS Overriding Variables49. CSS Variables and JavaScript50. CSS Variables in Media Queries51. CSS @property Rule52. CSS Box Sizing53. CSS Media Queries

On this page

Override Global Variable With Local VariableAdd a New Local VariableCSS var() Function