How To Apply Concept of Inheritance in CSS?
Last Updated :
04 Jan, 2025
Inheritance, a fundamental concept in object-oriented programming (OOP), mirrors real-life inheritance where children inherit traits from parents.
CSS Inheritance: In CSS inheritance, the child element will naturally inherit properties from its parent element.
Syntax:
<style>
#parentclass {
color: red;
}
</style>
<div id="parentclass">
Parent Div
<div id="div1Child">Child Div 1</div>
<div id="div2Child">Child Div 2</div>
</div>
Here parentclass passes a CSS styling done as color to be red. Whereas the child classes div1Child and div2Child have no ruleset of color: red set to them but they got displayed in red.
It is because the child div’s 1 and 2 inherited the properties from the parent i.e. parentclass.
HTML
<!--Driver Code Starts-->
<html>
<head>
<!--Driver Code Ends-->
<style>
#parentclass {
color: black;
}
#child1 {
color: green;
}
#childchild1 {
color: red;
}
</style>
<!--Driver Code Starts-->
</head>
<body>
<div id="div1">
Parent
<div id="child1">
Child 1
<div id="childchild1">
Child Child 1
<div id="childchildchild1">
Child Child Child
</div>
</div>
<div id="childchild2">
Child Child 2
</div>
</div>
<div id="child2">
Child 2
</div>
</div>
</body>
</html>
<!--Driver Code Ends-->
Output
Apply Concept of Inheritance in CSSHere #parentclass has color:black, #child1 has color:green and #childchild1 has color:red. In the above code #child1 and #child2 are in #parentclass so both should get the color black inherited but only child 2 gets the color because we gave #child1 to color: green this is known as specificity.
CSS properties that can be inherited.
We cannot inherit all the properties /rules of CSS. All font-* properties are naturally inherited like
- font-size
- font-family
- font-weight
- font-style, etc.
The color property is also inherited.
CSS properties such as height, border, padding, margin, width, etc. are not inherited naturally. We can do inheritance on noninheritable CSS properties. We use inherit for doing so.
CSS Inherit:
We use inherit on a CSS property for taking up its parent's element property. Let’s say we have a code:
HTML
<!--Driver Code Starts-->
<html>
<head>
<!--Driver Code Ends-->
<style>
#parentclass {
padding: 30px;
color: red;
}
#Child {
padding: inherit;
}
</style>
<!--Driver Code Starts-->
</head>
<body>
<div id="parentclass">
Parent
<div id="Child">Child</div>
</div>
</body>
</html>
<!--Driver Code Ends-->
Output:
Apply Concept of Inheritance in CSSIn this way, we inherit noninheritable CSS properties using inherit. Only the direct child element of a parent element can inherit it but the grandchild cannot. Instead, it will resort to its original browser computed height.
Similar Reads
How to apply !important in CSS? The !important rule in CSS is used to add more importance to a property/value than normal. It forces a style to override any other declarations, ensuring the specified property value is applied, regardless of specificity. It helps resolve conflicts but should be used sparingly to avoid complicating
2 min read
How to apply styles to multiple classes at once ? Applying styles to multiple classes at once means using a single CSS rule to style multiple elements that share different class names. This can be achieved by separating class selectors with commas, allowing for efficient styling of various elements without redundant code.Here we have some common ap
2 min read
How to use CSS to separate content & design ? In this article, we will learn about how to separate the content from the design using CSS, along with understanding the different ways to implement it through examples. The attractive web pages may contain some text content, images, video, audio, slides, etc, in a structure with a properly well-org
4 min read
How to Include One CSS File in Another? Here are the methods to include one CSS file in another:Method 1. Using @import RuleThe @import rule allows you to import one CSS file into another, enabling modularity and reusability of styles.html<html> <head> <link rel="stylesheet" href="styles.css"> </head> <body>
3 min read
How to apply class conditionally in CSS ? In web development, the styling of elements on a page is a crucial aspect of creating a visually appealing and interactive website. The traditional method of styling elements involves applying the same styles to all elements of the same type. However, this approach can become limiting when creating
4 min read