0

我是新手,想问一下您如何在 Bootstrap 4 中将导航栏项目放置在右侧?我在这里尝试了Bootstrap 4 - Right Align Navbar Items的解决方案,但没有奏效。

我试图理解 Bootstrap 4 中的示例代码,但不是什么使搜索表单向右对齐?

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
        <a class="navbar-brand" href="#">Hidden brand</a>
        <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
            <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">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>

        <form class="form-inline my-2 my-lg-0">
            <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>
    </div>
</nav>

mr-auto 不会使用下面的方法将导航项目向右对齐,

<nav class="navbar navbar-expand-lg navbar-light bg-light"> 
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggler40" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation"> 
            <span class="navbar-toggler-icon"></span> 
        </button>             

        <div class="collapse navbar-collapse" id="navbarToggler40"> 
            <a class="navbar-brand" href="#">Hidden brand</a> 
            <ul class="navbar-nav mt-2 mt-lg-0 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"> 
                    <a class="nav-link disabled" href="#">Disabled</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"> 
                        <a class="dropdown-item" href="#">Action</a> 
                        <a class="dropdown-item" href="#">Another action</a> 
                        <a class="dropdown-item" href="#">Something else here</a> 
                    </div>                         
                </li>                     
            </ul>                                  
        </div>             
    </nav>
4

1 回答 1

0

.mr-auto导航栏菜单(标签)上设置的类<ul>将表单推到右侧。

#navbarTogglerDemo01标签有 3 个直接子代:<a><ul>标签<form>。通常它们都会左对齐,但上面提到的类将<ul>标签的右边距设置为自动。这是实际的 css 样式:

.mr-auto {
    margin-right: auto !important;
}

您可以在 Bootstrap 4 文档的Spacing 实用程序部分阅读更多信息。

更新回应评论:
考虑下面的例子。除了配色方案,两个导航栏之间的唯一区别是.mr-auto类的用法。上面的导航栏使用它,并将表单推到右端,而下面的导航栏不使用它,因此表单位于菜单项旁边。

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarTogglerDemo01">
        <a class="navbar-brand" href="#">Hidden brand</a>
        <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
            <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">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>

        <form class="form-inline my-2 my-lg-0">
            <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>
    </div>
</nav>


<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
        <a class="navbar-brand" href="#">Hidden brand</a>
        <ul class="navbar-nav mt-2 mt-lg-0">
            <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">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>

        <form class="form-inline my-2 my-lg-0">
            <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>
    </div>
</nav>


<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>

因此,如果您想将整个菜单向右对齐,那么您有两个简单的选择。下面的示例使用.mr-auto品牌<a>标签上的类。您的另一个选择可能是将.mr-auto类更改.ml-auto<ul>元素上。

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
        <a class="navbar-brand mr-auto" href="#">Hidden brand</a>
        <ul class="navbar-nav mt-2 mt-lg-0">
            <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">
                <a class="nav-link disabled" href="#">Disabled</a>
            </li>
        </ul>

        <form class="form-inline my-2 my-lg-0">
            <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>
    </div>
</nav>


<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>

于 2017-12-10T20:26:47.803 回答