当屏幕尺寸不可移动时,Bootstrap导航栏显示2行

时间:2014-09-10 19:18:20

标签: html css twitter-bootstrap-3

我正在使用Bootstrap3,尝试构建响应式导航栏。在正常分辨率下,导航栏占用2行。在小分辨率下,它(正确地)只占用1行。

如何让link4和link5与1-3链接显示在同一行? (在代码下方小提琴)

<div class="navbar navbar-inverse navbar-static-top" data-role="navigation"> 
<div class="navbar-header navbar-left">
    <a class="navbar-brand" href="#">Title</a>
    <button type="button" class="navbar-toggle navbar-left left pull-left collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <button type="button" class="navbar-toggle navbar-right right pull-right collapsed navbar-link glyphicon glyphicon-user" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
    </button>
</div>

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav navbar-left">
        <li><a>link1</a></li>
        <li><a>link2</a></li>
        <li><a>link3</a></li>
    </ul>
</div>

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
    <ul class="navbar-nav nav navbar-right">
        <li><a>link4</a></li>
        <li><a>link5</a></li>
    </ul>
</div

http://jsfiddle.net/katgucbw/1/

2 个答案:

答案 0 :(得分:1)

您只需将navbar-right移至相同的navbar-collapse元素即可。像那样:

<div class="navbar navbar-inverse navbar-static-top" data-role="navigation"> 
    <div class="navbar-header navbar-left">
        <a class="navbar-brand" href="#">Title</a>
        <button type="button" class="navbar-toggle navbar-left left pull-left collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <button type="button" class="navbar-toggle navbar-right right pull-right collapsed navbar-link glyphicon glyphicon-user" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
        </button>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-left">
            <li><a>link1</a></li>
            <li><a>link2</a></li>
            <li><a>link3</a></li>
        </ul>
        <ul class="navbar-nav nav navbar-right">
            <li><a>link4</a>
            </li>
            <li><a>link5</a>
            </li>
        </ul>
    </div>
</div>

答案 1 :(得分:0)

将此ul代码添加到第一个#bs-example-navbar-collapse-1下方的ul内。这样,您就可以在大于768px的屏幕尺寸上将这些链接放在同一行上。

<ul class="navbar-nav nav navbar-right hidden-xs" style="margin-right: 10px">
    <li><a>link4</a></li>
    <li><a>link5</a></li>
</ul>

此外,在#bs-example-navbar-collapse-2内,在最后hidden-sm添加hidden-mdhidden-lgul类,以便它只显示在手机上( &LT; 768px)

Official Documentation on Responsive Utilities


完整代码:

<div class="navbar navbar-inverse navbar-static-top" data-role="navigation"> 
    <div class="navbar-header navbar-left">
        <a class="navbar-brand" href="#">Title</a>
        <button type="button" class="navbar-toggle navbar-left left pull-left collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <button type="button" class="navbar-toggle navbar-right right pull-right collapsed navbar-link glyphicon glyphicon-user" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
        </button>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-left">
            <li><a>link1</a></li>
            <li><a>link2</a></li>
            <li><a>link3</a></li>
        </ul> 
        <ul class="navbar-nav nav navbar-right hidden-xs" style="margin-right: 10px">
            <li><a>link4</a></li>
            <li><a>link5</a></li>
        </ul> 
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
        <ul class="navbar-nav nav navbar-right hidden-sm hidden-md hidden-lg">
            <li><a>link4</a></li>
            <li><a>link5</a></li>
        </ul>
    </div> 
</div>

Working Bootply