html - 如何使这个导航菜单居中

标签 html css

我已经查看了有关堆栈溢出的类似案例,但他们的解决方案对我不起作用。我如何让这个导航栏居中,请帮助。我只想将它居中,仅此而已。我正在努力不在尝试这样做时引入错误。有一段时间我很难解决这个问题。这是我的 HTML:

.toggle,
[id^=drop] {
  display: none;
}


/* Giving a background-color to the nav container. */

nav {
  margin: 0;
  padding: 0;
  background-color: #254441;
}


/* Since we'll have the "ul li" "float:left"
     * we need to add a clear after the container. */

nav:after {
  content: "";
  display: table;
  clear: both;
}


/* Removing padding, margin and "list-style" from the "ul",
     * and adding "position:reltive" */

nav ul {
  float: right;
  padding: 0;
  margin: 0;
  list-style: none;
  position: relative;
}


/* Positioning the navigation items inline */

nav ul li {
  margin: 0px;
  display: inline-block;
  float: left;
  background-color: #254441;
}


/* Styling the links */

nav a {
  display: block;
  padding: 14px 20px;
  color: #FFF;
  font-size: 17px;
  text-decoration: none;
}

nav ul li ul li:hover {
  background: #000000;
}


/* Background color change on Hover */

nav a:hover {
  background-color: #000000;
}


/* Hide Dropdowns by Default
     * and giving it a position of absolute */

nav ul ul {
  display: none;
  position: absolute;
  /* has to be the same number as the "line-height" of "nav a" */
  top: 60px;
}


/* Display Dropdowns on Hover */

nav ul li:hover>ul {
  display: inherit;
}


/* Fisrt Tier Dropdown */

nav ul ul li {
  width: 170px;
  float: none;
  display: list-item;
  position: relative;
}


/* Second, Third and more Tiers 
     * We move the 2nd and 3rd etc tier dropdowns to the left
     * by the amount of the width of the first tier.
    */

nav ul ul ul li {
  position: relative;
  top: -60px;
  /* has to be the same number as the "width" of "nav ul ul li" */
  left: 170px;
}


/* Change ' +' in order to change the Dropdown symbol */

li>a:after {
  content: ' +';
}

li>a:only-child:after {
  content: '';
}
<nav>
  <label for="drop" class="toggle">Menu</label>
  <input type="checkbox" id="drop" />
  <ul class="menu">
    <li><a href="#">Home</a></li>
    <li>
      <!-- First Tier Drop Down -->
      <label for="drop-1" class="toggle">WordPress +</label>
      <a href="#">WordPress</a>
      <input type="checkbox" id="drop-1" />
      <ul>
        <li><a href="#">Themes and stuff</a></li>
        <li><a href="#">Plugins</a></li>
        <li><a href="#">Tutorials</a></li>
      </ul>

    </li>
    <li>

      <!-- First Tier Drop Down -->
      <label for="drop-2" class="toggle">Web Design +</label>
      <a href="#">Web Design</a>
      <input type="checkbox" id="drop-2" />
      <ul>
        <li><a href="#">Resources</a></li>
        <li><a href="#">Links</a></li>
        <li>

          <!-- Second Tier Drop Down -->
          <label for="drop-3" class="toggle">Tutorials +</label>
          <a href="#">Tutorials</a>
          <input type="checkbox" id="drop-3" />

          <ul>
            <li><a href="#">HTML/CSS</a></li>
            <li><a href="#">jQuery</a></li>
            <li><a href="#">Other</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#">Graphic Design</a></li>
    <li><a href="#">Inspiration</a></li>
    <li><a href="#">Contact</a></li>
    <li><a href="#">About</a></li>
  </ul>
</nav>

任何帮助将不胜感激。

最佳答案

您已将 float: right 用于 nav ul 。我已经删除它并添加了

display: flex; justify-content: center;

.toggle,
[id^=drop] {
  display: none;
}


/* Giving a background-color to the nav container. */

nav {
  margin: 0;
  padding: 0;
  background-color: #254441;
}


/* Since we'll have the "ul li" "float:left"
 * we need to add a clear after the container. */

nav:after {
  content: "";
  display: table;
  clear: both;
}


/* Removing padding, margin and "list-style" from the "ul",
 * and adding "position:reltive" */

nav ul {
  display: flex;
  justify-content: center;
  padding: 0;
  margin: 0;
  list-style: none;
  position: relative;
}


/* Positioning the navigation items inline */

nav ul li {
  margin: 0px;
  display: inline-block;
  float: left;
  background-color: #254441;
}


/* Styling the links */

nav a {
  display: block;
  padding: 14px 20px;
  color: #FFF;
  font-size: 17px;
  text-decoration: none;
}

nav ul li ul li:hover {
  background: #000000;
}


/* Background color change on Hover */

nav a:hover {
  background-color: #000000;
}


/* Hide Dropdowns by Default
 * and giving it a position of absolute */

nav ul ul {
  display: none;
  position: absolute;
  /* has to be the same number as the "line-height" of "nav a" */
  top: 60px;
}


/* Display Dropdowns on Hover */

nav ul li:hover>ul {
  display: inherit;
}


/* Fisrt Tier Dropdown */

nav ul ul li {
  width: 170px;
  float: none;
  display: list-item;
  position: relative;
}


/* Second, Third and more Tiers 
 * We move the 2nd and 3rd etc tier dropdowns to the left
 * by the amount of the width of the first tier.
*/

nav ul ul ul li {
  position: relative;
  top: -60px;
  /* has to be the same number as the "width" of "nav ul ul li" */
  left: 170px;
}


/* Change ' +' in order to change the Dropdown symbol */

li>a:after {
  content: ' +';
}

li>a:only-child:after {
  content: '';
}
<nav>
  <label for="drop" class="toggle">Menu</label>
  <input type="checkbox" id="drop" />
  <ul class="menu">
    <li><a href="#">Home</a></li>
    <li>
      <!-- First Tier Drop Down -->
      <label for="drop-1" class="toggle">WordPress +</label>
      <a href="#">WordPress</a>
      <input type="checkbox" id="drop-1" />
      <ul>
        <li><a href="#">Themes and stuff</a></li>
        <li><a href="#">Plugins</a></li>
        <li><a href="#">Tutorials</a></li>
      </ul>

    </li>
    <li>

      <!-- First Tier Drop Down -->
      <label for="drop-2" class="toggle">Web Design +</label>
      <a href="#">Web Design</a>
      <input type="checkbox" id="drop-2" />
      <ul>
        <li><a href="#">Resources</a></li>
        <li><a href="#">Links</a></li>
        <li>

          <!-- Second Tier Drop Down -->
          <label for="drop-3" class="toggle">Tutorials +</label>
          <a href="#">Tutorials</a>
          <input type="checkbox" id="drop-3" />

          <ul>
            <li><a href="#">HTML/CSS</a></li>
            <li><a href="#">jQuery</a></li>
            <li><a href="#">Other</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#">Graphic Design</a></li>
    <li><a href="#">Inspiration</a></li>
    <li><a href="#">Contact</a></li>
    <li><a href="#">About</a></li>
  </ul>
</nav>

关于html - 如何使这个导航菜单居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46976473/

相关文章:

html - 响应式表格使布局超出 100% 宽度

html - 导航栏,列表项之间的可见空间

javascript - 如何从 DOM 错误事件中获取详细消息

javascript - 将 onClick 分配给链接标签 <a> 时出现问题

javascript - 焦点轮廓在右侧断开 - 未完全包裹元素

css - Twitter Bootstrap 输入追加在 row-fluid 上中断

html - Asp GridView 分页自定义 CSS 在设计 View 中有效,但在浏览器中无效

javascript - 页面加载时不计算字符数

html - super 专家的 iPhone 设备上的 Css 问题?

html - Laravel 网站托管后无法正常工作 HTTP ERROR 500 无法处理此请求