twitter-bootstrap 如何将 Bootstrap 导航栏中的表单元素(搜索框)居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31094802/
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 22:52:29  来源:igfitidea点击:

How to center a form element (search box) inside of Bootstrap navbar?

htmlcsstwitter-bootstrap

提问by Daniel Kobe

I'm trying to get the search box to be in the center of my Bootstrap navbar. Removing navbar-left seems to destroy the alignment on the navbar. Im going for something like the linkedin.compage. Whats the easiest way to do this?
Heres a link to a JSFiddle of my navbar https://jsfiddle.net/b6pmy9w4/. Thanks in advance for any help.
Html Code:

我试图让搜索框位于我的 Bootstrap 导航栏的中心。删除导航栏左侧似乎会破坏导航栏上的对齐方式。我要找一些类似linkedin.com 的页面。什么是最简单的方法来做到这一点?
这是我的导航栏https://jsfiddle.net/b6pmy9w4/的 JSFiddle 的链接。在此先感谢您的帮助。
html代码:

<nav class="navbar navbar-default" style="border-radius: 0;">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed burger-button" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand jura-font" href="/#!/">COLIGN</a>
                </div>
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <form class="navbar-form navbar-left" role="search">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Search">
                        </div>
                        <!-- <button type="submit" class="btn btn-default">Submit</button> -->
                    </form>
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#"><span class="glyphicon glyphicon-comment"></span></a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-user"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">View Profile</a></li>
                                <li><a href="#">Manage Account</a></li>
                                <li><a href="#">Invite a friend</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="/auth/signout">Log out</a></li>
                            </ul>
                        </li>
                    </ul>
                </div>
                <!-- /.navbar-collapse -->
            </div>
            <!-- /.container-fluid -->
        </nav>

回答by Bobby Tables

Don't know if it's the best way to do it but

不知道这是否是最好的方法,但是

  • Remove the navbar-left
  • Move it after the ul
  • Set text-align:center
  • 除掉 navbar-left
  • 之后移动它 ul
  • text-align:center

Updated fiddle

更新的小提琴

回答by user2709115

I know this post is old now, but on Bootstrap4 I just added the ml-auto class to the form tag and it worked perfectly:

我知道这篇文章已经过时了,但是在 Bootstrap4 上,我只是将 ml-auto 类添加到了表单标签中,它运行良好:

<form class="form-inline my-2 my-lg-0 ml-auto">

You can also add the col classes to change the size of the form input and the button.

您还可以添加 col 类来更改表单输入和按钮的大小。