如何Justify-End仅选择Bootstrap 4中的Nav-Items?

时间:2017-07-11 23:49:53

标签: javascript jquery html css twitter-bootstrap

我试图只使用 .justify-content-end

向右侧选择导航项

如果我将它添加到顶级div类,这可以正常工作但是如果我创建一个特殊的div来隔离一个nav-items并使用该类它不起作用:

<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#">Bewander</a>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href='@Url.Action("Create", "Reviews")'>Write a Review </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href='@Url.Action("Index", "Reviews")'>Search the World</a>
            </li>
            <div class="justify-content-end">
                <li class="nav-item">
                    <a class="nav-link" href='@Url.Action("Login", "Account")'>Login</a>
                </li>
            </div>
        </ul>
    </div>

</nav>

当.justify-content-end以这种方式工作时,它会移动我不想要的所有项目,因此我尝试隔离特定的导航项:

<nav class="navbar navbar-toggleable-md navbar-inverse bg-inverse">
    <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <a class="navbar-brand" href="#">Bewander</a>
    <div class="collapse navbar-collapse justify-content-end" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href='@Url.Action("Create", "Reviews")'>Write a Review </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href='@Url.Action("Index", "Reviews")'>Search the World</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href='@Url.Action("Login", "Account")'>Login</a>
            </li>
        </ul>
    </div>
</nav>

Here is a codepen to test on

1 个答案:

答案 0 :(得分:0)

.justify-content-end.navbar-nav推到最后,因为它是唯一的孩子。

相反,您可以尝试通过将.navbar-nav应用于flex-grow: 1来强制margin-left: auto;增长到全宽。然后,将.ml-auto应用于要向右推的项目。幸运的是,bootstrap有一个后者的实用程序类 - value

Updated Codepen