What is "inheritance" in CSS? Which properties are inherited by default?
Inheritance in CSS is a mechanism by which some properties are automatically passed down to descendants from parent elements. If a parent sets, for example, a text color, child elements inherit that color unless something else is specified.
How it works
Example:
<div style="color: blue;">
<p>Text inside a paragraph</p>
</div>The <p> tag itself has no color, but it will inherit it from <div>, and the text will be blue.
Properties inherited by default
Properties related mainly to text and fonts are inherited, for example:
color, text colorfont-family, fontfont-size, font sizefont-style,font-weight, style and weightline-height, line spacingtext-align, text alignmentvisibility, visibilitycursor, cursor stylelist-style, list stylingletter-spacing,word-spacing, spacing between letters and words
Non-inherited properties
Most properties related to sizes, spacing, borders, positioning, and background are not inherited:
margin,padding,border,width,height,background, and so on.
How to control inheritance
CSS lets you control this explicitly using values:
inherit, force inheritanceinitial, reset to the default valueunset, inherit if the property is normally inherited; otherwise reset
Example:
p {
background-color: inherit;
}In short: inheritance makes styling predictable and saves code, child elements automatically pick up some of their parents' visual properties, especially text-related ones.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.