Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inheritance is not working correctly in CSS

Here is the code

body {
  color: purple;
}

p {
  color: blue;
}
<body>
    <p> 
        <h1>Hello</h1>
    </p>

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

Now problem here is that h1 does not inherit the color of element p which is its nearest parent.
I read that if color is not given, then its default value is inherit. So, first h1 should be blue. So, what is happening here ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What is happening is caused by a problem with the HTML.

A p element cannot contain an h1 element so the system has closed the p element just before the h1 element. Hence the h1 element takes on the color of its nearest ancestor which has a color set and that is body in this case.

Try instead a legal bit of HTML, where the p is replaced with a div:

body {
  color: purple;
}

div {
  color: blue;
}
<body>
  <div>

    <h1>Hello</h1>
  </div>
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading