Inherit Values From the Parent

In Settle the Events Page Conflicts, every color you fixed came from a rule that matched the element. Most elements on your pages have no such rule for most properties, though. Nothing in events.css mentions li, yet every item on the seed swap page’s lists is dark green and uses the system font. Those values have to come from somewhere.

In this lesson, you’ll find out how an element gets a value when no rule sets it. You’ll be able to predict whether it takes its parent’s value or a starting value of its own, and check your prediction in DevTools. Then you’ll use inheritance on purpose to style text once and to give a button the page’s font. Start with one of Rosa’s lists.

Set Text Styles Once on body

A page has dozens of elements and only a few rules, so something has to fill in the gaps. Open events/seed-swap.html in the live preview. Inspect the first item under What to Bring, Seeds you saved or have spare, and switch to the Computed pane. Type color into its filter. The item’s color is oklch(0.247 0.05 152.7), the dark green from your body rule.

Switch back to the Styles pane, and you’ll find that body rule under Inherited from body, as you saw in Link Your First Stylesheet. No rule sets a color on the <li>, the <ul> around it, or <main> around that.

When no rule sets a property on an element, and the property is one that passes down, the element takes its parent’s computed value: the final value the Computed pane shows for the parent. That’s inheritance. Here the value passes from <body> to <main>, to the <ul>, and on to the <li>.

If you know nested scopes, this will feel familiar. A function that uses a variable it doesn’t define gets it from the scope around it, the nearest one first. An element without its own value gets it from the element around it, its parent. The comparison stops there, though. A scope passes down every variable, but an element passes down only some properties, as the next section shows.

An element inherits from its nearest parent. Inspect the date, Sunday, March 21, at 2 p.m., in the first paragraph. That’s a <time> element, and its color is rgb(115, 115, 115), the gray #737373 of your p rule, not the body’s green. The <time> element’s parent is the paragraph, and the paragraph has its own color from the p rule, so the gray is what passes down.

Watch out: inheritance works one property at a time. An element inherits a property only when no declaration sets that property on it, so an inherited value never competes with a declaration for the same property. Even a bare p rule that sets a color beats a color the paragraph would inherit from an ID rule on <main>. The p rule sets only the color, though, so the paragraphs still inherit the body’s font.

You can confirm which elements take the body’s color. In events.css, change the body rule’s color to purple:

Language: CSS Filename: events/events.css
body {
  background-color: honeydew;
  color: purple;
  font-family: system-ui, sans-serif;
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.