如何使用 css 将 float: left ul/li 导航菜单居中对齐?

发布于 2024-07-25 20:25:31 字数 321 浏览 1 评论 0原文

因此,我使用以下 CSS 来显示水平导航栏:

.navigation ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.navigation li {
  float: left;
  margin: 0 1.15em;
  /*    margin: 0 auto;*/
}

.navigation {
  /*    width: auto;*/
  /*    margin: 0 auto;*/
  text-align: center;
}

我的问题是:如何将导航栏居中对齐在标题上方?

So I have the following CSS in place to display a horizontal navigation bar using:

.navigation ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.navigation li {
  float: left;
  margin: 0 1.15em;
  /*    margin: 0 auto;*/
}

.navigation {
  /*    width: auto;*/
  /*    margin: 0 auto;*/
  text-align: center;
}

My question is: how do I align the navigation bar centrally above the title?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

许你一世情深 2024-08-01 20:25:31

给你的 .navigation ul 一个宽度并使用 margin:0 auto;

.navigation ul 
{
  list-style: none;
  padding: 0;
  width: 400px;
  margin: 0 auto;
}

Give your .navigation ul a width and use margin:0 auto;

.navigation ul 
{
  list-style: none;
  padding: 0;
  width: 400px;
  margin: 0 auto;
}
故事↓在人 2024-08-01 20:25:31

好吧,要在某些东西上使用 margin:0 auto,它必须有一个定义的宽度。 也许最好的解决方法是:

ul li {
  display: inline;
  list-style-type: none;
}
ul {
  text-align:center;
}

Well, to use margin:0 auto on something, it must have a defined width. Probably the best workaround is:

ul li {
  display: inline;
  list-style-type: none;
}
ul {
  text-align:center;
}
心奴独伤 2024-08-01 20:25:31

有一些设置(例如浮动、边距)可能会影响此代码正常工作。 它也适用于 IE7。 我从 CSS Wizardry 的一篇文章 中获取了此代码。

.navigation ul 
{
    list-style: none;
    padding: 0;
    margin: 0;
    text-align: center;/*make this change*/
 }
.navigation li
 {
    float: none;/*make this change*/
    display:inline;/*make this change*/
    margin: 0 1.15em;
 /* margin: 0 auto; */
 }
 .navigation li a {
    display:inline-block;/*make this change*/
 }
 .navigation 
 {
  /*width: auto;*/
  /*margin: 0 auto;*/
    text-align: center;
 }

There are few settings like float, margin which may affect this code to work properly. It works in IE7 too. I got this code from an article over at CSS Wizardry.

.navigation ul 
{
    list-style: none;
    padding: 0;
    margin: 0;
    text-align: center;/*make this change*/
 }
.navigation li
 {
    float: none;/*make this change*/
    display:inline;/*make this change*/
    margin: 0 1.15em;
 /* margin: 0 auto; */
 }
 .navigation li a {
    display:inline-block;/*make this change*/
 }
 .navigation 
 {
  /*width: auto;*/
  /*margin: 0 auto;*/
    text-align: center;
 }
旧故 2024-08-01 20:25:31

您可以将

  • 设置为 display: inline,然后在 上设置 text-align: center ;ul>。 这样做,您可以从列表项中删除 float: left ,并且不需要像使用 margin: 0 auto< 那样为导航栏设置固定宽度。 /代码>。
  • <html>
      <head>
        <style>
          ul { 
            list-style: none;
            text-align: center;
          }
    
          li {
            display: inline;
            margin: 0 1.15em;
          }
        </style>
      </head>
    
      <body>
        <ul>
          <li>Option 1</li>
          <li>Option 2</li>
          <li>Option 3</li>
        </ul>
      </body>
    </html>
    

    You could set the <li>'s to be display: inline, then set text-align: center on the <ul>. Doing that, you can remove the float: left from the list items and you don't need to have a fixed width for the navigation bar as you would if you used margin: 0 auto.

    <html>
      <head>
        <style>
          ul { 
            list-style: none;
            text-align: center;
          }
    
          li {
            display: inline;
            margin: 0 1.15em;
          }
        </style>
      </head>
    
      <body>
        <ul>
          <li>Option 1</li>
          <li>Option 2</li>
          <li>Option 3</li>
        </ul>
      </body>
    </html>
    
    那伤。 2024-08-01 20:25:31

    这个对我来说效果很好!
    (如果我是正确的:IE7+)

    小提琴:http://jsfiddle.net/fourroses666/zj8sav9q/3 /

    .nav{list-style:none; text-align:center;}
    .nav ul{list-style:none;}
    .nav li{display:inline;}
    .nav a{text-decoration:none; font-size:20px; line-height:20px; display:inline-block;}
    
    <nav class="nav" role="navigation" aria-label="main navigation">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu</a></li>
        <li><a href="#">Onze producten</a></li>
        <li><a href="#">Impressie</a></li>
        <li><a href="#">Media</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
    

    This one works great with me!
    (if I'm correct: IE7+)

    Fiddle: http://jsfiddle.net/fourroses666/zj8sav9q/3/

    .nav{list-style:none; text-align:center;}
    .nav ul{list-style:none;}
    .nav li{display:inline;}
    .nav a{text-decoration:none; font-size:20px; line-height:20px; display:inline-block;}
    
    <nav class="nav" role="navigation" aria-label="main navigation">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu</a></li>
        <li><a href="#">Onze producten</a></li>
        <li><a href="#">Impressie</a></li>
        <li><a href="#">Media</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
    
    染柒℉ 2024-08-01 20:25:31
    ul {
     display: inline-block;
     text-align: center;
    }
    
    ul {
     display: inline-block;
     text-align: center;
    }
    
    迷路的信 2024-08-01 20:25:31
    .navigation ul 
    {
      list-style-type: none;
      padding: 0px;
      width: 200px;
      margin: 0 auto;
    }
    
    .navigation ul 
    {
      list-style-type: none;
      padding: 0px;
      width: 200px;
      margin: 0 auto;
    }
    
    幽蝶幻影 2024-08-01 20:25:31

    如果您想在代码中继续使用浮动LI,您可以使用:

    JSFiddle: https://jsfiddle.net/Lvujttw3/

    <style>
    .wrapper {
        width: 100%;
        background-color:#80B5EB;
        text-align: center;
        }
    .intWrapper {
        display: inline-block;
        }
    .mainMenu {
        padding: 0;
        min-height: 40px;
        margin:auto;
        }
    ul {
        list-style-type: none;
        }
    ul li {
        float: left;
        font-size: 15px;
        line-height: 40px;
        }
    ul li A {
        display: block;
        color: white;
        font-weight: bold;
        font-family: Arial;
        text-decoration: none;
        min-height: 40px;
        padding: 0 36px;
        }
    </style>
    
    <div class="wrapper">
        <div class="intWrapper"> 
            <ul class="mainMenu">   
                <li><a href="one.htm" style='background-color:red'>ITEM ONE</a>
                </li><li><a href="two.htm">ITEM TWO</a>
                </li><li><a href="three.htm" style='background-color:red'>ITEM THREE</a>        
                </li>   
            </ul></div>
        </div>
    </div>
    

    If you want to keep using your floated LI in your code, you can use this:

    JSFiddle: https://jsfiddle.net/Lvujttw3/

    <style>
    .wrapper {
        width: 100%;
        background-color:#80B5EB;
        text-align: center;
        }
    .intWrapper {
        display: inline-block;
        }
    .mainMenu {
        padding: 0;
        min-height: 40px;
        margin:auto;
        }
    ul {
        list-style-type: none;
        }
    ul li {
        float: left;
        font-size: 15px;
        line-height: 40px;
        }
    ul li A {
        display: block;
        color: white;
        font-weight: bold;
        font-family: Arial;
        text-decoration: none;
        min-height: 40px;
        padding: 0 36px;
        }
    </style>
    
    <div class="wrapper">
        <div class="intWrapper"> 
            <ul class="mainMenu">   
                <li><a href="one.htm" style='background-color:red'>ITEM ONE</a>
                </li><li><a href="two.htm">ITEM TWO</a>
                </li><li><a href="three.htm" style='background-color:red'>ITEM THREE</a>        
                </li>   
            </ul></div>
        </div>
    </div>
    
    幸福%小乖 2024-08-01 20:25:31
    style="position: absolute; z-index: 1;"
    
    style="position: absolute; z-index: 1;"
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文