如何在 HTML 和 CSS 的行上制作导航栏?

How to make a navbar on a row with HTML and CSS?

我的 ul 元素不在 header 之上。我尝试使用绝对位置和相对位置,但后来就搞砸了。 ul 应该放在后台。是因为logo吗?我只想要一个基本的导航栏。请问有人可以帮忙吗?


    @import url("https://fonts.googleapis.com/css2?family=Nunito:wght@800&display=swap");
    @import url("https://fonts.googleapis.com/css2?family=Fruktur&family=Nunito:wght@800&display=swap");
    
    * {
      margin: 0;
      padding: 0;
      color: white;
      list-style: none;
      text-decoration: none;
      box-sizing: border-box;
      font-family: "Nunito", sans-serif;
    }
    
    header {
      width: 100%;
      height: 60px;
      background-color: #141517;
    
      .Logo {
        width: 120px;
        margin-top: 15px;
        margin-left: 8px;
      }
    
      ul {
        text-align: center;
      }
    
      li {
        font-size: 15px;
        margin: 0 10px;
        line-height: 60px;
        display: inline-block;
      }
    }

示例CSS

@import url("https://fonts.googleapis.com/css2?family=Nunito:wght@800&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Fruktur&family=Nunito:wght@800&display=swap");
* {
  margin: 0;
  padding: 0;
  color: white;
  list-style: none;
  text-decoration: none;
  box-sizing: border-box;
  font-family: "Nunito", sans-serif;
}

header {
  width: 100%;
  height: 60px;
  background-color: #141517;
}

header .Logo {
  width: 120px;
  margin-top: 15px;
  margin-left: 8px;
}

header ul {
  text-align: center;
}

header li {
  font-size: 15px;
  margin: 0 10px;
  line-height: 60px;
  display: inline-block;
}
<script src="https://kit.fontawesome.com/7ddb0129cc.js" crossorigin="anonymous"></script>

<body style="background-color: #202020">
  <header>
    <img class="Logo" src="Logo.svg" />
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Developing</a></li>
      <li><a href="#">LeaderBoards</a></li>
    </ul>
  </header>

您需要使用flexbox or grid,css中的两种方式来创建布局。我在下面使用了 flexbox 来让它工作。我还使用 css 而不是 scss juste 让它在 Stack Overflow 上工作。

@import url("https://fonts.googleapis.com/css2?family=Nunito:wght@800&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Fruktur&family=Nunito:wght@800&display=swap");

* {
  margin: 0;
  padding: 0;
  color: white;
  list-style: none;
  text-decoration: none;
  box-sizing: border-box;
  font-family: "Nunito", sans-serif;
}

header {
  width: 100%;
  min-height: 60px;
  background-color: #141517;
  display:flex;
  align-items:center;
  justify-content : space-between;
}

 .Logo {
    width: 120px;
    margin-top: 15px;
    margin-left: 8px;
  }

  ul {
    text-align: center;
    display: flex;
  }

  li {
    font-size: 15px;
    margin: 0 10px;
    line-height: 60px;
    display: inline-block;
  }
<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>RoPoop</title>
  <link rel="shortcut icon" type="image/jpg" href="favicon.ico" />
  <link rel="stylesheet" href="CSS/IndexStyle.css" />
  <script src="https://kit.fontawesome.com/7ddb0129cc.js" crossorigin="anonymous"></script>
</head>

<body style="background-color: #202020">
  <header>
    <img class="Logo" src="Logo.svg" />
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Developing</a></li>
      <li><a href="#">LeaderBoards</a></li>
    </ul>
  </header>
</body>

</html>