Home > Web Front-end > CSS Tutorial > How to Position Bootstrap 4 Navbar Items Outside the Collapse Section?

How to Position Bootstrap 4 Navbar Items Outside the Collapse Section?

Barbara Streisand
Release: 2024-10-30 22:54:03
Original
989 people have browsed it

 How to Position Bootstrap 4 Navbar Items Outside the Collapse Section?

Bootstrap 4: Positioning Navbar Items Outside the Collapse Section

Navigating Bootstrap 4's navbar can be tricky, especially when it comes to maintaining certain elements outside the collapsible section. Many developers struggle with ensuring that specific items remain persistent regardless of the collapse state.

To address this, the simplest approach is to utilize flexbox utility classes. This eliminates the need for additional CSS and cleanly keeps the desired items outside the navbar-collapse div.

Below is an improved version of the provided code snippet:

<code class="html"><nav class="navbar fixed-top navbar-light navbar-expand-lg navbar-template">
    <a class="navbar-brand" href="#">Navbar</a>
    <div class="d-flex flex-row order-2 order-lg-3">
        <ul class="navbar-nav flex-row">
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-facebook"></span></a></li>
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-twitter"></span></a></li>
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-youtube"></span></a></li>
            <li class="nav-item"><a class="nav-link px-2" href="#"><span class="fa fa-linkedin"></span></a></li>
        </ul>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown">
            <span class="navbar-toggler-icon"></span>
        </button>
    </div>
    <div class="collapse navbar-collapse order-3 order-lg-2" id="navbarNavDropdown">
        <ul class="navbar-nav ml-auto">
            <li class="nav-item"><a class="nav-link" href="#">Home</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Features</a></li>
            <li class="nav-item"><a class="nav-link" href="#">Pricing</a></li>
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown">Dropdown link</a>
                <div class="dropdown-menu dropdown-menu-right">
                    <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></code>
Copy after login

In this improved version, you can clearly observe the following changes:

  1. The items you wish to keep outside the collapse are situated outside the navbar-collapse div.
  2. We have applied the d-flex flex-row classes to the container where the persistent items reside. This ensures that these items remain in a horizontal row.
  3. The order-* classes are used. These are responsive ordering utility classes that determine an element's position within the flex container when in different screen breakpoints.
  4. The order-2 order-lg-3 class has been applied to the container containing the persistent items. This ensures that those items remain the second in order during mobile viewports and the third in order on larger screens.
  5. The order-3 order-lg-2 class has been applied to the navbar-collapse div, ensuring that the collapsible items take precedence on mobile and become second in order on larger screens.

Utilizing these classes effectively ensures that the items outside the collapse remain visible while the collapsible section takes precedence when the breakpoint is reached.

The above is the detailed content of How to Position Bootstrap 4 Navbar Items Outside the Collapse Section?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template