Inheritance and defaulting
Definition and purpose
Inheritance passes the computed value of an inherited property from the parent to the child when no overriding declaration supplies a value. Color and font size normally inherit; margins and borders normally do not.
Color inherits, border does not
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>#a { color: navy; border: 4px solid green; }</style> </head> <body> <div id="a"> <span id="b">Text</span> </div> </body></html>The child receives the color but not the border.
Syntax and mental model
Each element resolves its own cascade first. For an inherited property without a cascaded value, the parent computed value supplies the default; non-inherited properties use their initial value instead. At the root, inheritance uses the initial value. A direct child declaration outranks an inherited value without comparing its selector to the parent selector.
A direct child declaration
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { color: navy; } p { color: green; } #c { color: inherit; } </style> </head> <body> <div id="a"> <p id="b">Text</p> <p id="c">Other</p> </div> </body></html>The direct p rule overrides inheritance; c explicitly requests the parent color.
Common mistakes
A transparent child background may reveal the parent background, but that is not inheritance. Use direct declarations for borders or explicit inherit when appropriate.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Specificity and source order. Next lesson: CSS-wide keywords.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Inheritance and defaulting — knowledge check
Five questions on inheritance and defaulting.
Which normally inherits?
Which value does a child inherit?
Why can a transparent child show a parent background?
A child directly declares color: green while its parent is navy. What happens?
What does a non-inherited property normally use when no declaration applies?