我正在创建用于投资组合目的的网页,当我创建带有h2p标签的部分时,p标签会跳到角落。知道为什么会这样吗?

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <link href="https://fonts.googleapis.com/css?family=Open+Sans|Comfortaa" rel="stylesheet">
    <link rel="stylesheet" href="index.css" />
    <title>Scriptura</title>
  </head>
  <body>
    <nav class="header-nav">
      <ul>
        <li><a href="#">About</a></li>
        <li><a href="#">Support Us</a></li>
        <li><a href="#">GitHub</a></li>
      </ul>
    </nav>

    <header>
      <h1>Welcome To <em>Scriptura</em></h1>
      <p>The premier note-taking software on the web!</p>
    </header>

   <section>
     <h2>What Is <em>Scriptura</em></h2>
     <p>It's just my thing</p>
   </section>

    <script src="index.js"></script>
  </body>
</html>


CSS:

body {
    animation: bg-animation 1s ease-in-out 0s infinite alternate both;
    margin: 0;
    font-family: Open Sans, sans-serif;
}

h1, h2, h3, h4, h5, h6 {
    font-family: Comfortaa, sans-serif;
}

a {
    text-decoration: none;
    color: black;
}

header {
    position: relative;
    margin: 0 auto;
    padding: 10px;
    text-align: center;
    background-color: white;
    height: 600px;
}

header h1, p {
    position: absolute;
    top: 0; bottom: 0; right: 0; left: 0;
    margin: 0;
}

header h1 {
    line-height: 540px;
}

header p {
    line-height: 620px;
}

.header-nav {
    text-align: right
}

.header-nav ul {
    list-style-type: none;
    margin: 0;
}

.header-nav li {
    display: inline-block;
    padding: 20px;
    transition: background-color 0.1s ease-in-out 0s;
}

.header-nav li:hover {
    background-color: darkgrey;
}

@keyframes bg-animation {
    from {
        background-color: whitesmoke;
    }
    to {
        background-color: white;
    }
}

最佳答案

这个CSS规则就是问题所在(其中的绝对位置):

header h1, p {
    position: absolute;
    top: 0; bottom: 0; right: 0; left: 0;
    margin: 0;
}


原因:选择器header h1, p对于h1标记内的header标记和任何p标记均有效,即,对于section内的第二个标记也有效。 (不仅在header内部)

08-25 17:50