【问题标题】:How can I have my navigation menu fill the browser window without gaps on ends?如何让我的导航菜单填满浏览器窗口而两端没有间隙?
【发布时间】:2014-10-13 05:07:55
【问题描述】:

我正在使用浮动并尝试将导航菜单中的三个按钮中的每一个保持在 ~33.3% 左右,以便它填满屏幕并且在窗口大小缩小时不会中断,也不应该在末端留下间隙当浏览器窗口变宽时。

这是一个小提琴http://jsfiddle.net/xxd1vdcj/1/

<div id ="nav">
    <ul>
        <li id="dawn" >Tradition</li>
        <li id="dusk" >Styles</li>
        <li id="night">Contact</li>

    </ul>
</div>

#nav ul li{
    display:block;
    //width:19.3%;
    width: 33%;
    line-height: 3em;
    margin:0 auto;
    padding:0;
    text-align:center;
    float:left;
    background: -webkit-gradient(linear, left top, left bottom, from(#333), to(#111));
    color: #b0c4ff;
    font-size: 18px;
    margin-top: 140px;
    opacity: 1;
    border: 1px solid black;
}

【问题讨论】:

  • @Squirrl 试试这个 - jsfiddle.net/yLzu13x1
  • @MaryMelody 你是最棒的。这是你第二次救了我。
  • @Squirrl 这只是一个评论,不是解决方案:)
  • @Squirrl 是的!您应该接受我给 +1 的 Roko C. Buljan 的回答以及您的问题。 :)
  • @MaryMelody 感谢您的提醒!

标签: css css-float navigationbar


【解决方案1】:

正如我在评论中所说,&lt;a&gt; 不是有效的 &lt;ul&gt; 孩子。

100/3 = 33.333.. 但是您使用了33%,在较大的屏幕尺寸上,您的三个 LI 宽度的 1 像素(最多 6 像素)比一旦您调整页面大小,剩余的可用宽度 % 不是足以包含固定的 (1px) 边框宽度,从而导致 LI 突破到最近的可用空间。

使用box-sizing

一些box-sizing 将解决您的边框增加可用空间的问题。

*{margin:0; padding:0;} /* Global reset (also to remove 8px margin from Body) */

#nav ul{
    display:block;
    margin:10px;
    margin-top: 140px;
}
#nav ul li{
    box-sizing:border-box;
    border: 1px solid black;

    display:block;
    float:left;
    width: 33%;
    line-height: 3em;
    text-align:center;
    color: #b0c4ff;
    font-size: 18px;
}

http://jsfiddle.net/xxd1vdcj/5/
现在您甚至可以使用 33.333% 作为您的 LI 宽度。


使用display:tabletable-layout

由于旧版浏览器不支持box-sizing,您可以使用这个简单的解决方案:

*{margin:0;padding:0;}
#nav{
    margin:10px;
    margin-top: 140px;
}
#nav ul{
    display:table;       /* Table!! yey */
    width:100%;   
    table-layout: fixed; /* To fix LI widths */
}
#nav ul li{
    border: 1px solid black;
    display: table-cell; /* Note */
    line-height: 3em;
    text-align:center;
    color: #b0c4ff;
    font-size: 18px;
}

它擅长于 tables 的诞生!

http://jsfiddle.net/xxd1vdcj/7/

【讨论】:

    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多