Sub topic 9
Centralize colors and spacing with custom properties
The previous chapters repeated the same values in several rules. The nav background #1f2937 appears in .nav-list and again in .submenu, and #38bdf8 appears in both the focus outline and the current-page border. This chapter replaces those repeated values with CSS custom properties, so each value is defined once.
You should already know basic CSS selectors and properties, the box model, and the .nav-list rules from the earlier chapters.
What a custom property is
A custom property is a CSS variable that you name yourself. Its name starts with two dashes, such as --nav-bg. You declare it like any other property and read it with the var() function.
Custom properties inherit. If you declare one on :root (the html element), every element on the page can read it. If you declare it on a narrower selector, only that element and its descendants can.
:root {
--nav-bg: #1f2937;
--nav-bg-hover: #374151;
--nav-bg-current: #111827;
--nav-text: #ffffff;
--nav-accent: #38bdf8;
--nav-gap: 0.25rem;
--nav-pad-y: 0.75rem;
--nav-pad-x: 1rem;
}The names describe the role of each value (--nav-accent), not its appearance (--sky-blue). If the accent later becomes orange, a role name still makes sense.
Replace the repeated values
Here is the nav CSS from the earlier chapters, rewritten with var(). The layout is the same. Only the source of each value has changed.
.nav-list {
display: flex;
gap: var(--nav-gap);
margin: 0;
padding: 0;
list-style: none;
background: var(--nav-bg);
}
.nav-list a {
display: block;
padding: var(--nav-pad-y) var(--nav-pad-x);
color: var(--nav-text);
text-decoration: none;
}
.nav-list a:hover,
.nav-list a:focus-visible {
background: var(--nav-bg-hover);
}
.nav-list a:focus-visible {
outline: 2px solid var(--nav-accent);
outline-offset: -2px;
}
.nav-list a[aria-current="page"] {
background: var(--nav-bg-current);
border-bottom: 3px solid var(--nav-accent);
}
.submenu {
background: var(--nav-bg);
}Changing --nav-accent in :root now updates the focus outline and the current-page border together. The dropdown's .submenu rule from the later dropdown chapter can reuse --nav-bg the same way, so the two surfaces always match.
Override a property in a smaller scope
Because custom properties inherit, you can redefine one on a selector and everything inside picks up the new value. This works well for a second theme, such as a light bar.
.nav-light {
--nav-bg: #f3f4f6;
--nav-bg-hover: #e5e7eb;
--nav-bg-current: #d1d5db;
--nav-text: #111827;
}Add the class to the nav element, as in <nav class="nav-light" aria-label="Main">. No other rule changes. The .nav-list rules read var(--nav-bg) and get the light value, because the variable resolves on the element where it is used.
Spacing can change the same way. A compact bar on small screens only needs new values:
@media (max-width: 40rem) {
:root {
--nav-pad-y: 0.5rem;
--nav-pad-x: 0.75rem;
}
}The media query itself arrives in a later chapter. The point here is that the rules using var(--nav-pad-y) stay untouched.
Fallback values
var() accepts a second argument that is used when the property is not defined:
.nav-list a {
color: var(--nav-text, #ffffff);
}Use fallbacks sparingly. If you declare every property in :root, the fallbacks only add noise. They help in a component meant to be copied into pages that may not define your variables.
Common errors
- Typos in the name. Names are case-sensitive, so
--Nav-bgand--nav-bgare different properties. A name that was never declared makes the declaration invalid when the page is rendered. The browser then uses the inherited or initial value, not your fallback in the stylesheet. In practice you see a transparent background or wrong text color with no error message. - Using variables in media query conditions.
@media (max-width: var(--bp))does not work. Write the breakpoint value directly. - Declaring on the wrong element. A property declared on
.nav-listis not visible to a.footer-linksrule elsewhere. Declare shared values on:root. - Packing values into one variable. Keep
--nav-pad-yand--nav-pad-xseparate, so a rule can change one without repeating the other.
Exercise
Exercise
The focus outline and the current-page border both use #38bdf8. Using the variables from this chapter, how do you change both to orange (#f97316) with one edit, and what happens if you misspell the variable name in one of the rules?
Show answer
Change the declaration in :root to --nav-accent: #f97316; and both rules update, because each reads var(--nav-accent). If one rule misspells the name, for example var(--nav-acent), that declaration is invalid when the page is rendered. The browser ignores it and uses the inherited or initial value. That rule does not use the orange accent, and the browser reports no error. The outline or border may disappear.