Himanshu SharmaCOBOX Technologies
Let’s talk

CSS · Lesson 5 of 29

Inheritance

Learn which CSS properties are passed down from parent to child elements, which are not, and how inherit, initial and unset give you control.

10 minGetting startedUpdated 16 October 2026

By the end of this lessonPredict which styles an element inherits from its parents and set site-wide text styles efficiently on the body.

Styles that flow downhill

Remember the selectors exercise, where nested list items turned bold even though the selector only matched the outer ones? That was inheritance: some CSS properties are passed from an element to everything inside it.

html
<style>
  article {
    color: darkslategray;
    font-family: Georgia, serif;
    border: 2px solid tomato;
  }
</style>
<article>
  <h2>A heading inside the article</h2>
  <p>A paragraph with a <a href="#">link</a> and some <strong>strong text</strong>.</p>
</article>

The heading and paragraph take the article's text colour and font, but they do not each get a red border. The border stays on the article alone.

Which properties inherit

The rule of thumb: properties about text inherit; properties about the box do not.

Usually inherited Not inherited
color margin, padding
font-family, font-size, font-weight, font-style border
line-height, letter-spacing, text-align width, height
list-style background
visibility, cursor display, position

It makes sense when you picture the alternative. If border were inherited, putting a border on a page section would draw a box around every paragraph, link and word inside it.

In the example, the link stayed blue. Browsers have their own built-in rule for links, something like a { color: blue; }. That rule targets the link directly, and any rule that targets an element directly beats a value the element would inherit.

This is an important point: inheritance is the weakest source of a value. It is only used when nothing targets the element itself. Specificity does not help a parent's rule here.

html
<style>
  #main { color: navy; }   /* very specific, but only for #main itself */
  p { color: grey; }       /* weak selector, but targets the p directly */
</style>
<div id="main">
  Text directly in the div is navy.
  <p>This paragraph is grey.</p>
</div>

To make links follow the surrounding text colour, tell them to inherit:

css
a { color: inherit; }

Use inheritance on purpose

Inheritance lets you set your main text styles once, on body, and have the whole page follow:

css
body {
  font-family: system-ui, sans-serif;
  font-size: 1.0625rem;
  line-height: 1.6;
  color: #222;
}

Every paragraph, list and table now has the same base font and colour without a rule of its own. You then only write rules for the exceptions: headings in another font, a muted colour for captions, and so on. Stylesheets built this way are shorter and much easier to change.

Form controls are a famous exception: browsers give input, button, select and textarea their own fonts. Many stylesheets start with this line so forms match the rest of the page:

css
button, input, select, textarea { font: inherit; }

inherit, initial and unset

Every property accepts three special keywords:

  • inherit: take the parent's value, even for properties that normally do not inherit (border: inherit copies the parent's border).
  • initial: use the property's original default from the CSS specification (for color that is usually black, not your body colour).
  • unset: behave as inherit for inherited properties and initial for the others; a handy "forget any rule I set here".

Common mistakes

  • Expecting a parent's rule to beat a direct rule because the parent's selector is more specific.
  • Repeating font-family on every element instead of setting it once on body.
  • Surprise inheritance: making a card's text white for a dark background, then wondering why a light-coloured child box has invisible text.
  • Forms with a different font, fixed with font: inherit.

Exercise

Write one body rule that gives the whole page a font family, a line height of 1.6 and a dark grey colour. Then add exactly two more rules: links inherit the text colour but stay underlined, and headings use a different font family. Test it on a page with a heading, two paragraphs, a link and a button.

Show one possible answer
css
body {
  font-family: system-ui, sans-serif;
  line-height: 1.6;
  color: #333;
}

a { color: inherit; text-decoration: underline; }

h1, h2, h3 { font-family: Georgia, serif; }

button { font: inherit; } /* optional: buttons match the page font */

Key points

  • Text-related properties such as color, font-family, font-size and line-height are inherited by child elements.
  • Box properties such as margin, padding, border, width and background are not inherited.
  • Any rule that targets an element directly beats a value it would inherit, however specific the parent's rule is.
  • Set your main text styles once on body and let inheritance carry them through the page.
  • The keywords inherit, initial and unset force inheritance or reset a property.

Check yourself

1. You set color: navy on a div. What colour is a paragraph inside it with no colour rule of its own?
2. You set border: 1px solid red on a section. Do the paragraphs inside get borders too?
3. #main { color: navy; } and p { color: grey; }. What colour is a p inside #main?

Free tools for this lesson