防止项目在引导程序中折叠4导航栏切换

时间:2018-04-01 17:10:03

标签: html css twitter-bootstrap bootstrap-4 navbar

我有一个bootstrap 4导航栏。在这个导航栏中,我有一个字体真棒图标购物车,当页面传递最大宽度值时,我不想切换(进入3行菜单) (来自bootstrap的默认响应式导航栏)。我想留在菜单栏附近。我该怎么做? PS:我会放三个!字体真棒图标是帮助您看到它。

HTML:

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="index.php">English Park Cuisine</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
            <a class="nav-link" href="index.php">Acasa<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Meniu
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                <a class="dropdown-item" href="meniu.php">Pizza</a>
                <a class="dropdown-item" href="#">Paste</a>
                <a class="dropdown-item" href="#">Supe</a>
                <a class="dropdown-item" href="#">Ciorbe</a>
                <a class="dropdown-item" href="#">Desert</a>
            </div>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Contact</a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                <p class="dropdown-item">0746117702</p>
                <p class="dropdown-item">0742112452</p>
                <p class="dropdown-item">0735212352</p>
            </div>
        </li>
    </ul>
    !!!<a href="#"><i class="fas fa-shopping-cart fa-lg" style="color: white;"></i></a>
    <ul class="navbar-nav">
        <li class="nav-item">
            <a class="nav-link" href="#">Login</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#">Register</a>
        </li>
    </ul>
</div>

2 个答案:

答案 0 :(得分:7)

您需要将不可折叠的项目分组到一个div中,然后使用间距和顺序实用程序根据需要对齐它。

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

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">English Park Cuisine</a>
    <div class="d-flex order-lg-1 ml-auto pr-2">
        <a href="#" class="navbar-text"><i class="fa fa-shopping-cart fa-lg" style="color: white;"></i></a>
        <ul class="navbar-nav flex-row">
            <li class="nav-item mx-2 mx-lg-0">
                <a class="nav-link" href="#">Login</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Register</a>
            </li>
        </ul>
    </div>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="index.php">Acasa<span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Meniu
            </a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                    <a class="dropdown-item" href="meniu.php">Pizza</a>
                    <a class="dropdown-item" href="#">Paste</a>
                    <a class="dropdown-item" href="#">Supe</a>
                    <a class="dropdown-item" href="#">Ciorbe</a>
                    <a class="dropdown-item" href="#">Desert</a>
                </div>
            </li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Contact</a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                    <p class="dropdown-item">0746117702</p>
                    <p class="dropdown-item">0742112452</p>
                    <p class="dropdown-item">0735212352</p>
                </div>
            </li>
        </ul>
    </div>
</nav>

相关:Bootstrap 4 - Navbar items outside the collapse

答案 1 :(得分:0)

将您的购物车移到nav collapse div之外并将其嵌套,并将导航折叠到新的div中。然后相应地设置购物车的样式,但它不会因导航崩溃而消失。

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="index.php">English Park Cuisine</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
</button>
<div> <!-- New Div !-->
<div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
            <a class="nav-link" href="index.php">Acasa<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Meniu
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                <a class="dropdown-item" href="meniu.php">Pizza</a>
                <a class="dropdown-item" href="#">Paste</a>
                <a class="dropdown-item" href="#">Supe</a>
                <a class="dropdown-item" href="#">Ciorbe</a>
                <a class="dropdown-item" href="#">Desert</a>
            </div>
        </li>
        <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Contact</a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                <p class="dropdown-item">0746117702</p>
                <p class="dropdown-item">0742112452</p>
                <p class="dropdown-item">0735212352</p>
            </div>
        </li>
    </ul>
    <ul class="navbar-nav">
        <li class="nav-item">
            <a class="nav-link" href="#">Login</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" href="#">Register</a>
        </li>
    </ul>
</div>
    <!-- move the anchor outside of the original div but nested inside the new div !-->
    !!!<a href="#"><i class="fas fa-shopping-cart fa-lg" style="color: white;"></i></a>
</div>