【问题标题】:HTML center nav barHTML 中心导航栏
【发布时间】:2017-02-25 14:42:20
【问题描述】:

如何使导航栏居中,但仍以一条线显示在网站顶部?我知道我必须更改构建 li 的 css 文件中的某些内容,但我无法弄清楚。代码是:

<ul>
  <li><a class="active" href="index.html">Home</a></li>
  <li><a href="index.html">Tab 1</a></li>
  <li><a href="index.html">Tab 2</a></li>
  <li><a href="index.html">Tab 3</a></li>
  <li><a href="index.html">Tab 4</a></li>
</ul>

而css文件是这样的:

ul {
  display: inline-block;
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  background-color: #111;
}

.active {
  background-color: #791519;
}

【问题讨论】:

标签: html css center nav


【解决方案1】:

我会将它包装在 NAV 标签中,并在 NAV 标签上设置 text-align to center。

nav {
  text-align:center;
}
ul {
  display: inline-block;
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #333;
  margin:0 auto;

}


<nav>
<ul>
  <li><a class="active" href="index.html">Home</a></li>
  <li><a href="index.html">Tab 1</a></li>
  <li><a href="index.html">Tab 2</a></li>
  <li><a href="index.html">Tab 3</a></li>
  <li><a href="index.html">Tab 4</a></li>
</ul>
</nav>

Example

【讨论】:

  • 谢谢!这比第一个响应要容易得多!谢谢!
  • 你应该接受这个或带有弹性盒子的那个,我更喜欢这种方法,因为弹性盒子仍然不受支持。
  • 是的,我也更喜欢你的方式,因为 flex 太过分了。有了你的,使用填充我可以让它变得像我想要的那样又高又长。非常感谢!
  • 另一件事。如何添加带有图标的搜索选项卡?
  • 检查代码笔。我做了一个改变..它可能适合你的需要。
【解决方案2】:

我推荐使用Flex Box:

ul {
  display: flex;
  flex-direction: row;
  align-items: stretch;
  list-style-type: none;
  width:100%;
  padding: 0;
  overflow: hidden;
  background-color: #333;
}

li{
  flex-grow: 1;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover:not(.active) {
  background-color: #111;
}

.active {
  background-color: #791519;
}
<ul>
  <li><a class="active" href="index.html">Home</a></li>
  <li><a href="index.html">Tab 1</a></li>
  <li><a href="index.html">Tab 2</a></li>
  <li><a href="index.html">Tab 3</a></li>
  <li><a href="index.html">Tab 4</a></li>
</ul>

【讨论】:

    【解决方案3】:

    也许这样(添加换行和文本对齐:居中):

    .center {
      margin: 0 auto;
      text-align: center;
    }
    
    ul {
      display: inline-block;
      list-style-type: none;
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #333;
    }
    
    li {
      float: left;
    }
    
    li a {
      display: block;
      color: white;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
    }
    
    li a:hover:not(.active) {
      background-color: #111;
    }
    
    .active {
      background-color: #791519;
    }
    <div class="center">
      <ul>
        <li><a class="active" href="index.html">Home</a></li>
        <li><a href="index.html">Tab 1</a></li>
        <li><a href="index.html">Tab 2</a></li>
        <li><a href="index.html">Tab 3</a></li>
        <li><a href="index.html">Tab 4</a></li>
      </ul>
    </div>

    【讨论】:

      【解决方案4】:

      你可以通过改变 ul 的位置来做到这一点 检查 ul 的以下 css 更改

      ul {
        display: inline-block;
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #333;
        position:relative;
        left:100px;
      }
      
      li {
        float: left;
      }
      
      li a {
        display: block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
      }
      
      li a:hover:not(.active) {
        background-color: #111;
      }
      
      .active {
        background-color: #791519;
      }
      <ul>
        <li><a class="active" href="index.html">Home</a></li>
        <li><a href="index.html">Tab 1</a></li>
        <li><a href="index.html">Tab 2</a></li>
        <li><a href="index.html">Tab 3</a></li>
        <li><a href="index.html">Tab 4</a></li>
      </ul>

      希望对你有帮助

      【讨论】:

      • 谢谢!现在我可以用像素改变位置!谢谢!
      • 这不会使导航栏居中。它只在左侧增加了 100 像素间距。
      猜你喜欢
      • 1970-01-01
      • 2014-04-11
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 2021-07-08
      • 2017-05-08
      • 2017-09-07
      相关资源
      最近更新 更多