tags:

views:

133

answers:

2

Can somebody please explain, what is the inherit keyword meant to do in CSS?

+3  A: 

I'm not going to plagiarize the fine explanation on sitepoint.com so I'll just give the url here:

http://reference.sitepoint.com/css/inheritvalue#

Hannson
The SitePoint article is super clear with great explanation! Great link!
Doug Neiner
+13  A: 

It will use the same same value as the same property his parent has.

<body>
<h1></h1>
</body>

css:

body{
 margin: 234px;
}
h1{
 margin: inherit; #=234px
}

Note to this if there are multiple instances of <h1> in the file, it will take the margin of it's parent. So 234px is not always the value it will have. For example

<body>
  <h2></h2>
  <div>
    <h2></h2>
  </div>
</body>

css

body{
margin: 20px;
}
div{
margin: 30px;
}
h2{
margin: inherit; #20px if parent is body; 30px is parent is div
}
Timo Willemsen
Its important to note this does not work in IE6+IE7, but great answer. +1
Doug Neiner
Even in IE8- it works with "direction" and "visibility" properties
Sergey Ilinsky
I think the person who asked this question did know how "inherit" works, what he [likely] wanted to know however: "What are the use cases for using CSS inherit mechanism"
Sergey Ilinsky
@Timo, yes, it works as you have explained. Open this link in Safari or Firefox to see it working: http://jsbin.com/umopi
Doug Neiner
@Doug Neiner, thanks removed my comment in my post.
Timo Willemsen