twitter-bootstrap Bootstrap 3:将导航栏项目保留在折叠之外而不换行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18761633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:22:05  来源:igfitidea点击:

Bootstrap 3: keep navbar items outside the collapse without wrapping to a new line?

htmlcsstwitter-bootstraptwitter-bootstrap-3

提问by CeeGee

I could use some help getting my Bootstrap 3 navbar to look right when collapsed.

我可以使用一些帮助让我的 Bootstrap 3 导航栏在折叠时看起来正确。

Apologies if this is a duplicate question but a good long search hasn't yielded the answers I need.

如果这是一个重复的问题,但长时间的搜索没有得到我需要的答案,我深表歉意。

I've created a responsive navbar based on the Bootstrap demo code. In it, there's a 40x40 image and a globe glyph that link to dropdown menus, which I've right-aligned and omitted from the nav Collapse.

我已经基于 Bootstrap 演示代码创建了一个响应式导航栏。在其中,有一个 40x40 图像和一个链接到下拉菜单的地球字形,我已将其右对齐并从导航折叠中省略。

Unfortunately, the 40x40 and globe glyph appear on a new line when the nav collapses, which I don't want.

不幸的是,当导航折叠时,40x40 和地球字形出现在新行上,这是我不想要的。

My perfect layout when the nav collapses would be for the 40x40 and globe to appear on the same line as my brand image, the collapse button etc; right aligned, and not contained in the collapse.

当导航折叠时,我的完美布局是 40x40 和地球仪与我的品牌形象、折叠按钮等出现在同一行;右对齐,并且不包含在折叠中。

Thank you for any advice you can give!

感谢您提供的任何建议!

Code is like so:

代码是这样的:

    <nav class="navbar navbar-default" role="navigation">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-hd-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a href="index.php"><img class="navbar-brand img-responsive" src="http://placehold.it/138x38" alt="Logo"></a>
    </div>
     <div class="navbar-right">
        <ul class="nav"  style="float: right; display: inline-block !important">
            <li class="dropdown pull-right">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="http://placehold.it/40x40" alt="Profile Image" style="display: inline-block !important"/><b class="caret grey"></b></a>
                <ul class="dropdown-menu">
                    <li class="dropdown-header">Hello</li>
                    <li class="divider"></li>
                    <li><a href="">Thing 1</a></li>
                    <li class="divider"></li>
                    <li><a href="">Thing 2</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav"   style="float: right; display: inline-block !important">
            <li class="dropdown pull-right">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-globe grey"></span><b class="caret grey"></b></a>
                <ul class="dropdown-menu">
                    <li style="padding: 10px"><p>News</p></li>
                </ul>
            </li>
        </ul>

         <div class="collapse navbar-collapse navbar-hd-collapse">
             <ul class="nav navbar-nav">
                 <li class="active"><a href="">Product</a></li>
                 <li><a href="">Other Product</a></li>
                 <li><a href="">Third Product</a></li>
             </ul>
         </div>
    </nav>

JS Fiddle is here: http://jsfiddle.net/CaraGee/q8br8/

JS 小提琴在这里:http: //jsfiddle.net/CaraGee/q8br8/

回答by Bala

Add Style=" float:right"; in the 40*40 and globe div... so that the 40x40 and globe glyph appear on a same line

添加样式=“浮动:右”;在 40*40 和地球 div 中...以便 40x40 和地球字形出现在同一行

 <div class="navbar-right" style="float:right;">
    <ul class="nav">
        <li class="dropdown pull-right">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown"><img src="http://placehold.it/40x40" alt="Profile Image" style="display: inline-block !important"/><b class="caret grey"></b></a>
            <ul class="dropdown-menu">
                <li class="dropdown-header">Hello</li>
                <li class="divider"></li>
                <li><a href="">Thing 1</a></li>
                <li class="divider"></li>
                <li><a href="">Thing 2</a></li>
            </ul>
        </li>
    </ul>
    </div>
     <div class="navbar-right" style="float:right">
    <ul class="nav">
        <li class="dropdown pull-right">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown"><span class="glyphicon glyphicon-globe grey"></span><b class="caret grey"></b></a>
            <ul class="dropdown-menu">
                <li style="padding: 10px"><p>News</p></li>
            </ul>
        </li>
    </ul>
 </div>

回答by Alejandro Silva

make a the collapse div with the align to the left: and the non collapsable elements inside a div with the align to the right like this:

制作一个折叠 div,左对齐:和 div 内的不可折叠元素,右对齐,如下所示:

<div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">Project name</a>
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <!-- THIS WILL COLLAPSE-->
            <div class="navbar-collapse collapse navbar-left">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#about">About</a></li>
                    <li><a href="#contact">Contact</a></li>
                </ul>
            </div>
            <!-- THIS WILL NOT COLLAPSE -->
            <div class="nav navbar-nav" >
                <form class="navbar-form navbar-right">
                    <div class="form-group">
                        <input type="text" placeholder="Usuario" class="form-control">
                    </div>
                    <div class="form-group">
                        <input type="password" placeholder="Contrase?a" class="form-control">
                    </div>
                    <button type="submit" class="btn btn-success">Conectarse</button>
                </form>
            </div>
        </div>
    </div>