Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
How to use CSS to separate content & design ?
Next article icon

How To Apply Concept of Inheritance in CSS?

Last Updated : 04 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 CSS

Here #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 CSS

In 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.



Next Article
How to use CSS to separate content & design ?
author
vishnubharadwaj
Improve
Article Tags :
  • CSS
  • Web Technologies
  • CSS-Misc

Similar Reads

  • How to apply inline CSS ?
    In this article we will learn how to apply inline CSS, Inline CSS contains the CSS property in the body section attached to the element is known as inline CSS. This kind of style is specified within an HTML tag using the style attribute. It is used to apply a unique style to a single HTML element. S
    1 min read
  • 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 a
    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. [GFGTABS] html <html> <head> <link rel="stylesheet" href="styles.css
    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
  • Inherit a class to another file in Sass
    Sass or syntactically awesome style sheets is a CSS preprocessor that gives CSS such powers that are not available in plain CSS. It gives the power of using expressions, variables, nesting, mixins(Sass form of functions), inheritance, and more. Other well-known CSS preprocessor examples include Less
    4 min read
  • How to Access Two Ids in One CSS Selector?
    In CSS, an ID is a unique identifier that is assigned to an HTML element, it is used to target and style a specific element on the web page in CSS selectors are also used to target HTML elements for styling and ID is one type of selector. These are the following approaches to access two IDs in one C
    3 min read
  • How to Write a:hover in Inline CSS?
    The :hover pseudo-selector in CSS enables you to modify the style of elements when a user hovers their mouse over them. This selector is widely used to improve user experience by adding visual feedback on elements like buttons, links, images, or any interactive items on a webpage. For the :hover eff
    2 min read
  • How content property is used with :after selector in CSS ?
    In this article, we will learn how content property is used with the :after selector in CSS. The :after selector is used to insert something after any selected element. And the content property is used to specify the content that will be inserted. So we use the content property in ::after selector t
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences