-1

I used descendant selector and I declared the style process in the head section

In the HTML head:

<style>
  p h1 {
    color: blue;
    background-color: red;
  }
</style>

In the HTML body:

<p>
  <h1>
    I am Anik Islam
  </h1>
</p>

It didn't show blue colored letters and red colored background, it showed the default instead. What am I missing here?

finnmglas
  • 1,626
  • 4
  • 22
  • 37

1 Answers1

1

Your HTML is malformed.

You can not place an h element inside a p element. Header tags should be separate block elements not nested within paragraph elements.

In addition, many web browsers will auto-close open <p> tags if another block element is encountered such as <h1>.

Chances are very high the browser is rendering your markup as...

<p>

</p>

<h1>
 I am Anik Islam
</h1>

</p>

Therefore there is no nesting taking place. So, consequently the CSS isn't seeing any nesting.

If you wish to nest elements, use div tags. or if you wish to style the h1 specifically, target it by adding a class or ID to it.

Scott
  • 21,475
  • 8
  • 43
  • 55
  • "*many web browsers...*", they should all do this, it's part of [the specs](https://www.w3.org/TR/2011/WD-html5-20110113/tokenization.html#parsing-main-inbody) "***A start tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"** If the stack of open elements has a p element in button scope, then act as if an end tag with the tag name "p" had been seen*" – Kaiido May 22 '20 at 07:11
  • Agreed, but I can't be absolutely definitive unless I check *every possible* web browser `:)` – Scott May 22 '20 at 07:20