将导航栏搜索表单和中心的按钮与 Bootstrap 4 对齐

Posted

技术标签:

【中文标题】将导航栏搜索表单和中心的按钮与 Bootstrap 4 对齐【英文标题】:Align Navbar search form and button in the center with Bootstrap 4 【发布时间】:2019-02-04 05:44:36 【问题描述】:

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

<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>

【问题讨论】:

【参考方案1】:

您应该将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】:

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>

【讨论】:

【参考方案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

【讨论】:

以上是关于将导航栏搜索表单和中心的按钮与 Bootstrap 4 对齐的主要内容,如果未能解决你的问题,请参考以下文章

导航栏中没有输入组的引导 3.1.1 宽搜索面板

Bootstrap 导航栏中心项目

Bootstrap 导航栏中的中心菜单项

导航栏中的中心表单

Bootstrap 4 - 使用切换按钮将链接移动到导航栏的右侧[重复]

Bootstrap Navbar 与品牌折叠 [重复]