Sub topic 1
CSS custom properties refresher
Styling hooks are built on CSS custom properties. This chapter covers the parts of that feature you need before the later chapters: declaring a property, reading it with var(), setting fallbacks, and how values inherit down the DOM tree.
Prerequisites
This chapter assumes you already know:
- Basic Lightning web component structure: an HTML template, a JavaScript class, and a CSS file.
- CSS selectors, the cascade, and inheritance.
- SLDS and base components such as
lightning-buttonandlightning-card, at the level of using them in a template.
Declaring and reading a custom property
A custom property is a CSS property whose name you choose. The name must start with two hyphens. You declare it like any other property, and you read it with the var() function.
.card {
--card-accent: #0b5cab;
--card-gap: 1rem;
border-left: 4px solid var(--card-accent);
padding: var(--card-gap);
}Three rules matter here:
- Names are case-sensitive.
--Card-Accentand--card-accentare different properties. - The browser stores the value as a token stream and does not check it when you declare it.
--card-gap: banana;is accepted. The value is only checked when a real property uses it. - You can use
var()inside most property values, including insidecalc(). You cannot use it in a property name or in a media query condition.
Fallback values
var() takes an optional second argument. The browser uses it when the property is not defined at that element.
.badge {
background-color: var(--badge-bg, #f3f3f3);
}Everything after the first comma is the fallback. A fallback such as var(--font, Arial, sans-serif) therefore supplies the whole list Arial, sans-serif. You can also nest another var() as the fallback to chain defaults.
Inheritance
Custom properties inherit by default. An element that does not set a property receives its parent's computed value. An element can override the value for itself and its descendants.
The next chapter shows why this matters for Lightning web components. A base component renders inside a shadow tree, which blocks ordinary selectors. Inherited custom properties still pass through, so a value set outside a component can be read inside it.
Scope and the cascade
A custom property follows the normal cascade. Specificity, source order, and !important decide which declaration wins on a given element. Inheritance then passes the winning value down.
<div class="region">
<p class="note">Default accent</p>
<p class="note highlight">Highlighted accent</p>
</div>.region {
--accent: #0b5cab;
}
.note {
color: var(--accent, #333333);
}
.region .highlight {
--accent: #c23934;
}The first paragraph is blue because it inherits --accent from .region. The second paragraph matches the more specific .region .highlight rule, so it gets red.
In a Lightning web component's CSS file, :host targets the component's own host element. Setting a property there makes it available to everything inside the component.
Common errors
- A typo in the name.
var(--card-acent)does not raise an error. If there is no fallback, the property using it becomes invalid at computed-value time. It then behaves as if unset, so you see an inherited or initial value. - A value that is invalid where it is used.
--card-gap: bananais accepted, butpadding: var(--card-gap)is then discarded. - Expecting the property to apply at the wrong element. A value only reaches the elements below the one that sets it, never above it.
- Missing units.
--gap: 16works incalc(var(--gap) * 1px)but not aspadding: var(--gap).
Exercise
A .region rule sets --accent: green. Inside it, <p class="note"> uses color: var(--accent, black). No other rule sets --accent. What color is the text? What color is it if .region is removed from the page's CSS?
Show answer
The text is green, because the paragraph inherits --accent from .region. Without that rule, --accent is undefined at the paragraph, so var() uses the fallback and the text is black.