【问题标题】:Align navigation bar对齐导航栏
【发布时间】:2021-11-29 17:55:23
【问题描述】:

我的导航栏有问题...

  1. 我想将它定位到中心,但它不会这样做。
  2. 我想从左到右填充。

我试图: 文本对齐:在 .navbar 中居中 text-align:在导航栏中居中。

.navbar {
  background-color: gray;
  position: fixed;
  top: 0%;
}

.navbar a {
  color: white;
  text-align: center;
  font-family: monospace;
  font-size: 20px;
  text-decoration: none;
  padding: 15px;
  display: block;
  float: left;
}

.navbar a:hover {
  background: lightgray;
  color: orange;
}
<div class="navbar">
  <a href="../index.html">Home</a>
  <a href="Guides.html">Guides</a>
  <a href="#">Clips</a>
  <a href="#">Leaderborard</a>
  <a href="Support the server.html">Support the server</a>
  <a href="Help.html">Help</a>
</div>

【问题讨论】:

  • 您可能想研究 CSS 网格布局和/或 Flexbox。

标签: html css navigation


【解决方案1】:

我在 CSS 源代码中记录了我所做的事情。

.navbar {
  background-color: gray;
  position: fixed;
  top: 0%;
  text-align: center; /* Added */
}

.navbar a {
  color: white;
  /* text-align: center; Removed */
  font-family: monospace;
  font-size: 20px;
  text-decoration: none;
  padding: 15px;
  display: inline-block; /* Changed from block */
  /* float: left; Removed */
}

.navbar a:hover {
  background: lightgray;
  color: orange;
}
<div class="navbar">
  <a href="../index.html">Home</a>
  <a href="Guides.html">Guides</a>
  <a href="#">Clips</a>
  <a href="#">Leaderborard</a>
  <a href="Support the server.html">Support the server</a>
  <a href="Help.html">Help</a>
</div>

【讨论】:

    【解决方案2】:

    这似乎可以使用

    justify-content: center;
    

    在容器上

    .container {
      width: 100%;
      padding: 20px;
      display: flex;
      justify-content: center;
    }
    .navbar {
      background-color: gray;
      position: fixed;
      top: 0%;
      text-align: center; /* Added */
    }
    
    .navbar a {
      color: white;
      /* text-align: center; Removed */
      font-family: monospace;
      font-size: 20px;
      text-decoration: none;
      padding: 15px;
      display: inline-block; /* Changed from block */
      /* float: left; Removed */
    }
    
    .navbar a:hover {
      background: lightgray;
      color: orange;
    }
    .navbar1 {
          background-color: gray;
          display: flex;
          justify-content: center;
          top: 50%;
          text-align: center; /* Added */
        }
        
        .navbar1 a {
          color: white;
          /* text-align: center; Removed */
          font-family: monospace;
          font-size: 20px;
          text-decoration: none;
          padding: 15px;
          display: inline-block; /* Changed from block */
          /* float: left; Removed */
        }
        
        .navbar1 a:hover {
          background: lightgray;
          color: orange;
        }
       
    
     <div class="container">
          <div class="navbar">
            <a href="../index.html">Home</a>
            <a href="Guides.html">Guides</a>
            <a href="#">Clips</a>
            <a href="#">Leaderborard</a>
            <a href="Support the server.html">Support the server</a>
            <a href="Help.html">Help</a>
          </div>
        </div>
    <div class="navbar1">
                <a href="../index.html">Home</a>
                <a href="Guides.html">Guides</a>
                <a href="#">Clips</a>
                <a href="#">Leaderborard</a>
                <a href="Support the server.html">Support the server</a>
                <a href="Help.html">Help</a>
              </div>

    【讨论】:

    • 嗨,感谢您的帮助,您的代码有效,但我需要最后一件事。侧面是空的,我想用导航栏填充它...
    • 嗨,然后摆脱容器并应用显示:flex;证明内容:中心;直接在导航栏类中,我向同一个 sn-p 添加了第二个版本,因此您可以比较结果
    • 哇!这是我想要的工作谢谢你的帮助!我已经看到了那些标签,我需要了解更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 2014-02-09
    • 2015-10-19
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多