CSS Interview Questions
HTML & CSS
Web DevelopmentFrontendQuestion 12
How does CSS inheritance work? What properties are inherited by default?
Answer:
CSS Inheritance
Inheritance in CSS refers to the mechanism by which certain properties are passed down from a parent element to its child elements in the DOM (Document Object Model). When a property is inherited, the child elements automatically take on the value of that property from their parent, unless a different value is explicitly specified for the child.
How Inheritance Works
-
Inherited Properties:
- Certain CSS properties are naturally inherited from parent elements. These properties typically relate to text and font styling, such as
color,font-family,font-size,line-height, etc. - If a child element does not have a specific rule defined for an inheritable property, it will inherit the value from its parent.
- Certain CSS properties are naturally inherited from parent elements. These properties typically relate to text and font styling, such as
-
Non-Inherited Properties:
- Most properties related to the box model, layout, positioning, background, and borders are not inherited. For example,
margin,padding,border,width, andheightare not inherited by default. - For non-inherited properties, child elements will use either their initial value (the default value defined by the CSS specification) or a value explicitly set by CSS.
- Most properties related to the box model, layout, positioning, background, and borders are not inherited. For example,
-
Explicit Inheritance:
- You can explicitly set a property to be inherited using the
inheritkeyword. This forces a child element to inherit a property value from its parent, even if the property is non-inherited by default. - Example:
p { background-color: inherit; /* Inherits the background-color from its parent */ }
- You can explicitly set a property to be inherited using the
Example of Inherited Properties
Consider the following HTML and CSS:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: Arial, sans-serif; /* Inherited by child elements */
color: blue; /* Inherited by child elements */
margin: 20px; /* Not inherited */
}
p {
font-size: 16px; /* Inherited by child elements */
}
span {
font-style: italic; /* Inherited by child elements */
}
</style>
<title>CSS Inheritance Example</title>
</head>
<body>
<p>This is a paragraph with <span>some italic text</span> inside it.</p>
</body>
</html>
-
Inherited Properties:
- The
font-familyandcolorproperties set on thebodyelement will be inherited by thepelement and thespanelement inside it. So, the text color will be blue, and the font family will be Arial. - The
font-sizeset on thepelement will be inherited by thespanelement inside it, making the text size 16px.
- The
-
Non-Inherited Property:
- The
marginproperty set on thebodyis not inherited, so thepelement does not receive amarginvalue from thebody.
- The
Properties That Are Inherited by Default
Some common properties that are inherited by default include:
-
Text and Font Properties:
colorfont-familyfont-sizefont-stylefont-weightline-heighttext-aligntext-indenttext-transformletter-spacingword-spacingwhite-space
-
Other Inherited Properties:
visibilitycursorlist-stylequotes
Non-Inherited Properties (Require Explicit Assignment)
Some common non-inherited properties include:
-
Box Model Properties:
marginpaddingborderwidthheightdisplay
-
Layout Properties:
positiontop,right,bottom,leftfloatclearz-index
-
Background Properties:
background-colorbackground-imagebackground-positionbackground-repeatbackground-size
-
Other Non-Inherited Properties:
opacityoverflowbox-shadowtransformtext-shadow
Overriding Inherited Properties
Inherited properties can be overridden by specifying a different value in the child element’s CSS.
Example:
p {
color: red; /* Overrides inherited color */
}
In the above example, even if the parent element (e.g., body) has a color of blue, the p element will display its text in red because the inherited property is overridden.
Summary
- CSS Inheritance allows child elements to inherit certain properties from their parent elements, simplifying style management.
- Inherited Properties include most text-related properties, while layout and box-model properties are generally not inherited.
- Explicit Inheritance can be enforced using the
inheritkeyword, and inherited properties can be overridden by specifying a different value in the child element.