Why doesn’t the <p> tag inherit your father’s color?

Asked

Viewed 108 times

4

I have the following very simple code

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
    
    p#father {
        color: red;
    }

    p#child {
        color: inherit;
    }
    
    </style>
</head>
<body>
    
<p id="father">
    pai
    <p id="child">
        filho
        <p id="child2">
            filho 2
        </p>
    </p>
</p>

</body>
</html>

What happens is it was for the tag with id#child inherit the color: red of the paragraph id#father. And it doesn’t, because ?

2 answers

6

Your syntax is incorrect, you cannot have a paragraph <p>within another paragraph.

If you change the children to an allowed element (within the tag <p>) such as a <span> You will see that the children will automatically inherit their father’s color.

And if you want to change the color of a child just apply the css only to the desired child. See:

#father {
  color: red;
}

#child2 {
  color: green;
}
<p id="father" style="color:red">
    pai
    <span id="child">
        filho
        <span id="child2">
            filho 2
        </span>
    </span>
</p>

  • I came to post the answer and you already had put exactly what I would put +1

  • 1

    Whoa, thanks a colleague

6

The problem is that the Browser fixes your code. Just as Vinicius said, it is not appropriate to put a <p> within another. When vc does this the browser rendering engine does this with your code

inserir a descrição da imagem aqui

So, with the browser automatically shutting down your <p> when you put inherit in fact he inherits the color of body and not from his father, for he has no father!

OBS: This behavior can change from browser to browser.

The Firefox also closes the <p> automatically and does not render a <p> within another...

inserir a descrição da imagem aqui

OBS2: In HTML5 the tag <p> does not need a closing tag </p>, although I recommend using...

inserir a descrição da imagem aqui

Source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/p

  • 2

    Thanks, hugocsl did not know this of the browser. And dear tu ta in every corner of stackoverflow ;)

  • @Leandronascimento hahaha according to my fiancée "I’m addicted to this little game" rss. But sharing the knowledge is very good, it was worth the force!

  • 2

    That’s right, knowledge is the key to the future. The world moves and we have to move along with it hehehe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.