为什么我不能在ap标签内使用标题标签并使用CSS设置样式?

Bis*_*isu 4 html css3

这是我的代码:

<html>
    <head>
        <style>
              div p h1 {
                  background-color: red;
              }
       </style>
    </head>
    <body>
        <div>
            <p><h1>hello2</h1></p> 
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

mtc*_*ch9 7

我想这就是为什么:

<p>标签只能包含内联元素.标题标记是块级元素,<p>即使您将它们设置为内联显示,也无法进入标记内部.

  • 事实上你不能。根据:https://www.w3.org/TR/html-markup/h1.html#h1 您只能使用[短语元素](https://www.w3.org/TR/html-markup/ common-models.html#common.elem.phrasing) 在 h 标签内 (2认同)