使用Bootstrap 4将搜索栏移到第二行以显示中等屏幕

时间:2018-12-10 23:21:27

标签: css twitter-bootstrap bootstrap-4 navbar

此导航栏使用Bootstrap4。对于中型屏幕(768像素至991像素),我希望将搜索栏和按钮移至第二行,因为它在大屏幕及以上。当前,在中等屏幕上,搜索栏紧挨着其他导航链接:

enter image description here

在大屏幕/超大屏幕上的外观:

enter image description here

每个第一个答案建议的更新代码:

<nav class="navbar navbar-expand-md navbar-fixed-top navbar-dark bg-navbar">
    <div class="container">
        <a class="navbar-brand" href="/" style="align-children: middle">
        <span class="" aria-hidden="true"></span>
            Example
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#site-nav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="sr-only">Toggle navigation</span>
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse ml-auto" id="site-nav">
            <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="/agencies/">Agencies</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/meetings/">Meetings</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/agenda/">Agenda</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/documents/">Documents</a>
                </li>
            </ul>
            <form class="form-inline mt-3 mb-3" role="search" action="/search/" method="post">
                <div class="btn-group">
                    <input type="text" name="request" class="form-control" placeholder="Search Documents">
                    <button type="submit" class="btn search-button">Search</button>
                </div>
            </form>
        </div>
    </div>
</nav>

2 个答案:

答案 0 :(得分:1)

将搜索表单的内容放在btn-group中,这应该使它们保持在一起:

        <form class="form-inline mt-3 mb-3" role="search" action="/search/" method="post">
            <div class="btn-group" role="group" aria-label="Basic example">
                <input type="text" name="request" class="form-control" placeholder="Search Documents">
                <button type="submit" class="btn search-button">Search</button>
            </div>
        </form>

答案 1 :(得分:1)

您可以使用自适应Flexbox方向工具。例如,flex-column flex-xl-row上的navbar-collapse

<nav class="navbar navbar-expand-md fixed-top navbar-dark bg-dark">
    <div class="container">
        <a class="navbar-brand" href="/" style="align-children: middle">
        <span class="" aria-hidden="true"></span>
            Example
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#site-nav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="sr-only">Toggle navigation</span>
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse ml-auto align-items-start align-items-xl-center flex-column flex-xl-row" id="site-nav">
            <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="/agencies/">Agencies</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/meetings/">Meetings</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/agenda/">Agenda</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/documents/">Documents</a>
                </li>
            </ul>
            <form class="form-inline ml-2" role="search" action="/search/" method="post">
                <div class="form-group">
                    <input type="text" name="request" class="form-control" placeholder="Search Documents">
                </div>
                <button type="submit" class="btn search-button">Search</button>
            </form>
        </div>
    </div>
</nav>

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

注意:您使用的是navbar-expand-md,因此导航栏会折叠到sm上的切换器中。如果您想在md上显示堆叠的菜单/窗体,则应将导航栏扩展到sm

另请参阅:Bootstrap 4 navbar with 2 rows