html Boostrap 4导航栏折叠菜单右对齐

inn6fuwd  于 2022-11-20  发布在  其他
关注(0)|答案(3)|浏览(221)

我正在使用Bootstrap 4,并且我试图通过单击折叠按钮来使弹出菜单在右侧而不是左侧打开。我尝试在“ul”元素上使用“ml-auto”。导航栏项目在未折叠时正确地位于右侧。当折叠时,按钮正确地在右边,但是菜单弹出在左边。我也试过在div中放入“ml-auto”,但是没有成功。下面是我的HTML:

<nav class="navbar navbar-expand-lg navbar-dark fixed-top">
    <a class="navbar-brand" href="#home"><img id="logo" class="no-opacity" src="content/white-logo.png"></a>
    <button class="navbar-toggler ml-auto" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav ml-auto">
            <li class="nav-item">
                <a class="nav-link" href="#home"><i class="fa fa-home fa-fw" aria-hidden="true"></i>&nbsp;Home</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#about"><i class="fa fa-user fa-fw" aria-hidden="true"></i>&nbsp;About</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#projects"><i class="fa fa-folder-open fa-fw" aria-hidden="true"></i>&nbsp;Projects</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#experience"><i class="fa fa-briefcase fa-fw" aria-hidden="true"></i>&nbsp;Experience</a>
            </li>       
            <li class="nav-item">
                <a class="nav-link" href="#contact"><i class="fa fa-envelope fa-fw" aria-hidden="true"></i>&nbsp;Contact</a>
            </li>                     
        </ul>
    </div>
</nav>

我觉得有一个简单的解决办法,但我想不出来。

xcitsw88

xcitsw881#

只需添加简单的规则到您的CSS。

.nav-item{
    text-align: right;
}
42fyovps

42fyovps2#

根据http://getbootstrap.com/docs/4.0/components/navs/#horizontal-alignment,在导航栏的折叠下拉列表中居中、右对齐或左对齐的方法是使用justify-content-endnavclass中的朋友。
示例。此代码生成它后面的图像。

<nav class="navbar navbar-expand-lg fixed-top">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <ul class="list-inline social-buttons">
            {% for link in site.social %}
            <li class="list-inline-item">
                <a href="{{ link.url }}"><i class="{{ link.icon }}"></i></a>
            </li>
            {% endfor %}
        </ul>

        <div class="navbar-toggler" data-toggle="collapse" data-target="#menu">
            &#9776;<!-- three bars symbol -->
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="menu">
            <ul class="nav ml-auto">
                <a class="nav-link page-scroll" href="#about">About</a>
                <a class="nav-link page-scroll" href="#projects">Projects</a>
                <a class="nav-link page-scroll" href="#writing">Writing</a>
                <a class="nav-link page-scroll" href="#contact">Contact</a>
            </ul>
        </div>
    </div>
</nav>

现在将这一行修改为:

<div class="nav ml-auto justify-content-end">

收益率

请注意,这显然与ml-auto无关,当ml-auto丢失时,会导致未折叠的菜单项(在更宽的屏幕上)不右对齐。(toggler按钮本身对我来说总是右对齐的。)似乎只有justify控制未折叠菜单中的内容。

whitzsjs

whitzsjs3#

只需在HTML中使用“文本靠右”
例如:
<div class="navbar-collapse text-right" id="navbarResponsive">

相关问题