将Navbar搜索表单和按钮与Bootstrap 4居中对齐

时间:2018-08-29 12:22:44

标签: html css bootstrap-4 navbar

这是标题的代码,我想将其搜索栏和按钮居中对齐

<header>
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <a class="navbar-brand" href="#">Aditya Shrivastava</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
    <nav class="navbar navbar-light bg-light">
      <form class="form-inline">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </nav>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav ml-auto">
        <a class="nav-item nav-link" href="#">Twitter</a>
        <a class="nav-item nav-link" href="#">Resume</a>
        <a class="nav-item nav-link" href="#">Blog</a>
      </div>
    </div>
  </nav>
</header>

3 个答案:

答案 0 :(得分:3)

要对齐Bootstrap 4导航栏内容,请使用utility classes

  • flex-grow-1上的品牌和形式,以便它们填充宽度
  • mx-lg-auto将表单放在lg及更高版本上(navbar-expand-lg断点)
  • ml-auto(位于右侧导航栏折叠上)以使其保持正确对齐
  • m-* and p-*根据需要调整边距或填充

<header>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand flex-grow-1" href="#">Aditya Shrivastava</a>
        <div class="flex-grow-1 d-flex">
            <form class="form-inline flex-nowrap bg-light mx-0 mx-lg-auto rounded p-1">
                <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
                <button class="btn btn-outline-success" type="submit">Search</button>
            </form>
        </div>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
            <div class="navbar-nav ml-auto">
                <a class="nav-item nav-link" href="#">Twitter</a>
                <a class="nav-item nav-link" href="#">Resume</a>
                <a class="nav-item nav-link" href="#">Blog</a>
            </div>
        </div>
    </nav>
</header>

https://www.codeply.com/go/NXIwAj610b


相关:Bootstrap NavBar with left, center or right aligned items

答案 1 :(得分:0)

您应将text-align: center;归入.form-inline类。或者也使用引导程序默认类align="center"

希望获得帮助。

让我知道进一步的澄清。

<header>
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <a class="navbar-brand" href="#">Aditya Shrivastava</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
    <nav class="navbar navbar-light bg-light">
      <form class="form-inline" style="text-align: center;">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </nav>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav ml-auto">
        <a class="nav-item nav-link" href="#">Twitter</a>
        <a class="nav-item nav-link" href="#">Resume</a>
        <a class="nav-item nav-link" href="#">Blog</a>
      </div>
    </div>
  </nav>
</header>

答案 2 :(得分:-1)

Bootstrap具有内置的中心对齐方式,您只需在表单类旁边添加align =“ center”

<header>
  <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <a class="navbar-brand" href="#">Aditya Shrivastava</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
    <nav class="navbar navbar-light bg-light">
      <form class="form-inline" align="center">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
      </form>
    </nav>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav ml-auto">
        <a class="nav-item nav-link" href="#">Twitter</a>
        <a class="nav-item nav-link" href="#">Resume</a>
        <a class="nav-item nav-link" href="#">Blog</a>
      </div>
    </div>
  </nav>
</header>