无法在Bootstrap导航栏中浮动元素

时间:2017-10-03 14:49:25

标签: html css html5 twitter-bootstrap bootstrap-4

我有问题要将元素浮动到引导程序导航栏内的右侧,我需要应用float:右边的选项1和选项2元素,以及中心搜索输入,我尝试了!important规则,但仍然无法正常工作,当内容折叠并单击导航栏切换按钮时,它会从左侧移动一些px。

  

编辑:我还需要按以下顺序折叠时显示导航栏:品牌 - 搜索输入 - Toggler按钮。以及搜索输入下的切换按钮的内容。

以下是相关代码:

.navbar{
    background-image: cornflowerblue;
}
#mainNav{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
body{
    background-color: grey;
}
#navContent{
    background-color: white;
    margin: 3%;
}
#final{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
#searchForm{
    margin: auto;
}
#test{
    text-align: center;
    border: 5px solid black;
    float: right;
}
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
    <div class="container-fluid">
        <section class="row" id="mainNav">
            <nav class="navbar navbar-expand-xs rounded">
                <a class="navbar-brand" href="#">Brand</a>

                <form class="form-inline" id="searchForm" role="search">
                    <div class="input-group">
                        <input class="form-control" type="text" placeholder="Search">
                        <div class="input-group-btn">
                            <button class="btn btn-secondary" type="button"><i class="fa fa-search"></i></button>
                        </div>
                    </div>
                </form>

                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
                    <i class="fa fa-bars"></i>
                </button>
                <div id="test">
                    <div class="collapse navbar-collapse" id="navToggler">
                        <ul class="navbar-nav">
                            <li class="nav-item"><a class="nav-link" href="#">Option 1</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Option 2</a></li>
                        </ul>
                    </div>
                </div>
            </nav>
        </section>
        </div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
    <script src="js/index.js"></script>
        </body>

3 个答案:

答案 0 :(得分:2)

只需在下面添加<div class="col-md-12"></div>部分就可以了。

&#13;
&#13;
.navbar{
    background-image: cornflowerblue;
}
#mainNav{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
body{
    background-color: grey;
}
#navContent{
    background-color: white;
    margin: 3%;
}
#final{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
#searchForm{
    margin: auto;
}
#test{
    text-align: center;
    border: 5px solid black;
    float: right;
}
&#13;
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
    <div class="container-fluid">
        <section class="row" id="mainNav">
        <div class="col-md-12">
            <nav class="navbar rounded">
                <a class="navbar-brand" href="#">Brand</a>

                <form class="form-inline" id="searchForm" role="search">
                    <div class="input-group">
                        <input class="form-control" type="text" placeholder="Search">
                        <div class="input-group-btn">
                            <button class="btn btn-secondary" type="button"><i class="fa fa-search"></i></button>
                        </div>
                    </div>
                </form>

                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navToggler" aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation">
                    <i class="fa fa-bars"></i>
                </button>
                <div id="test">
                    <div class="collapse navbar-collapse" id="navToggler">
                        <ul class="nav navbar-nav">
                            <li class="nav-item"><a class="nav-link" href="#">Option 1</a></li>
                            <li class="nav-item"><a class="nav-link" href="#">Option 2</a></li>
                        </ul>
                    </div>
                </div>
            </nav>
            </div>
        </section>
        </div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
    <script src="js/index.js"></script>
        </body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您想在Bootstrap 4中使用大型菜单(例如,针对Boostrap 3的yamm),我使用flexbox成功了:

<!-- adapted from bs-repo basic navbar example code -->
<nav id="navbar" class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" href="#">Navbar</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse"
          data-target="#navbarSupportedContent"
          aria-controls="navbarSupportedContent" aria-expanded="false"
          aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com"
           id="navbarDropdownMenuLink" data-toggle="dropdown"
           aria-haspopup="true" aria-expanded="false">Dropdown link</a>
        <div class="dropdown-menu"
             aria-labelledby="navbarDropdownMenuLink">
          <ul class="d-md-inline-flex flex-md-row">
            <li class="dropdown-item">
              <ul class="d-md-inline-flex flex-md-column">
                <li class="dropdown-item"><a href="#">Action</a></li>
                <li class="dropdown-item"><a href="#">Another action</a></li>
                <li class="dropdown-item"><a href="#">Something else here</a></li>
              </ul>
            </li>
            <li class="dropdown-item">
              ...
            </li>
            <li class="dropdown-item">
              ...
            </li>
          </ul>
        </div>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="text"
             placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0"
              type="submit">Search</button>
    </form>
  </div>
</nav>

答案 2 :(得分:0)

使用Bootstrap 4集成Flex系统。我稍微清理了一下代码并添加了魔法 ml-auto (margin-left:auto)。 试一试。

.navbar{
    background-image: cornflowerblue;
}
#mainNav{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
body{
    background-color: grey;
}
#navContent{
    background-color: white;
    margin: 3%;
}
#final{
    border: 5px solid black;
    margin: 3%;
    background-color: white;
}
#searchForm{
    margin: auto;
}

<html>
<head>
    <title></title>
</head>
<body>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <div class="container-fluid">
        <nav class="navbar navbar-expand-lg rounded" id="mainNav">
            <a class="navbar-brand" href="#">Brand</a>
            <form class="form-inline" id="searchForm" name="searchForm" role="search">
                <div class="input-group">
                    <input class="form-control" placeholder="Search" type="text">
                    <div class="input-group-btn">
                        <button class="btn btn-secondary" type="button"><i class="fa fa-search"></i></button>
                    </div>
                </div>
            </form>
            <button aria-controls="navToggler" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-target="#navToggler" data-toggle="collapse" type="button"><i class="fa fa-bars"></i></button>
            <div class="collapse navbar-collapse ml-auto" id="navToggler">
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="#">Option 1</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="#">Option 2</a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js">
    </script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js">
    </script>
</body>
</html>