CSS Value Inherit

The value inherit keyword is used to inherit a property to an element from its parent element property value. The inherit keyword can be used for inheriting any CSS property, and on any HTML element.

Inheritance is always from the parent element in the document tree, even when the parent element is not the containing block.

Syntax:

property_name: inherit;

Example 1: Inheriting the font size from the parent element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<meta charset="utf-8">
  
<head>
    <style>
        span {
            font-size: 10px;
        }
  
        .gfg {
            font-size: inherit;
        }
    </style>
</head>
  
<body>
    <h1 style="text-align: center;
               color: green;">
        GeeksforGeeks
    </h1>
    <div style="text-align: center; font-size: 20px;">
        <span class="gfg">Inherited size</span>
    </div>
</body>
  
</html>

chevron_right


Output:



Example 2: Inheriting the color from the parent element.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<meta charset="utf-8">
  
<head>
    <style>
        span {
            color: blue;
        }
  
        .gfg {
            color: inherit;
        }
    </style>
</head>
  
<body>
    <h1 style="text-align:center; color:green;">
        GeeksforGeeks
    </h1>
  
    <div style="text-align: center; color: green">
        <span class="gfg">
            Inherited color is green but 
            span color is set to blue
        </span>
    </div>
</body>
  
</html>

chevron_right


Output:

Supported Browsers:

  • Chrome
  • Safari
  • Edge
  • Firefox
  • Opera



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.