为什么 css 没有应用于具有相同类的元素?

2 html css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

<style>
    .city
    {
        background-color: black;
        color:blanchedalmond;
    }
</style>

</head>
<body>
    <div class="city">
<h1>Paris</h1>
    </div>
    <p class="city"><h2>Germany</h2></p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我正在学习类,现在我制作了两个具有相同类的 html 元素,但 css 未应用于 p 标签。

con*_*exo 5

因为p无法容纳h2

检查浏览器的元素检查器。自动h2关闭p元素,因此这是实际呈现的 HTML:

<p class="city"></p>
<h2>Germany</h2>
<p></p>
Run Code Online (Sandbox Code Playgroud)
<p class="city"></p><!-- the closing tag here is automatically added by the browser because the `<h2>` closes a parent `<p>` element -->
<h2>Germany</h2>
<!--the missing opening tag <p> is added by the browser's fault tolerance here --></p>
Run Code Online (Sandbox Code Playgroud)