How to align the navbar toggle button and list items to the right in Bootstrap 5

I'm struggling with a simple html page that has a fixed navbar at the top. Everything looks great when viewed in full-screen mode, with centered links. However, when the page size is reduced, it collapses to a toggle button on the left side. What I really want is for this toggle button and the dropdown links to align neatly on the right side of the page. I'm currently using a local copy of Bootstrap 5. Is there a way to accomplish this while still having the link items appear centered in full screen mode?

So far, I've tried options like navbar-toggler-right, but they haven't been helpful.

<!DOCTYPE html>
<html lang="en">
 <head>
  <title>Bootstrap 5 - Test Navbar Page</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="Testing Navbar">
  <meta name="keywords" content="bootstrap5,html">
  <meta name="author" content="Bootstrap 5">
  <!-- Bootstrap 5 -->
  <link rel="stylesheet" href="css/bootstrap.min.css"></link>
 </head>
 <body>
  <nav class="navbar navbar-expand-sm bg-light navbar-light fixed-top">
   <!-- Toggler/collapsibe Button -->
   <button class="navbar-toggler ms-2" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle Navigation">
    <span class="navbar-toggler-icon"></span>
   </button>
   <div class="navbar-collapse collapse justify-content-center" id="navbarCollapse">
    <ul class="navbar-nav ms-2">
     <li class="nav-item">
      <a class="nav-link" href="#">Resources</a>
     </li>
     <li>
      <a class="nav-link" href="#">Contact</a>
     </li>
     <li>
      <a class="nav-link" href="#">Login</a>
     </li>
     <li>
      <form action="#">
       <input type="submit" class="btn btn-warning" value="Register">
      </form>
     </li>
    </ul>
   </div>
  </nav>
  <!-- Bootstrap 5 -->
  <script src="js/bootstrap.bundle.min.js"></script>
 </body>
</html>

Current Full Page View

https://i.sstatic.net/SMZXe.png

Current Collapsed View

https://i.sstatic.net/XlFBN.png

Answer №1

If you're looking for a simple solution, using align-items-end is the way to go. Just remember to nest your content within a container.

<nav class="navbar navbar-expand-sm bg-light navbar-light fixed-top">
    <!-- Toggler/collapsibe Button -->
    <div class="container-fluid">
        <button class="navbar-toggler ms-auto ms-sm-2" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle Navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="navbar-collapse collapse justify-content-center" id="navbarCollapse">
            <ul class="navbar-nav ms-2 align-items-end">
                <li class="nav-item">
                    <a class="nav-link" href="#">Resources</a>
                </li>
                <li>
                    <a class="nav-link" href="#">Contact</a>
                </li>
                <li>
                    <a class="nav-link" href="#">Login</a>
                </li>
                <li>
                    <form action="#">
                        <input type="submit" class="btn btn-warning" value="Register">
                    </form>
                </li>
            </ul>
        </div>
    </div>
</nav>

Live Demo

For more information, check out: Customizing Bootstrap 5 Navbar Alignment

Answer №2

Don't forget to add the brand to your navigation bar. Below is an example of how you can include it:

<nav class="navbar navbar-expand-sm bg-light navbar-light fixeds-top">
    <div class="container">
<!-- Make sure to insert this brand section -->
         <a class="navbar-brand" href="lorem.php">
            <img src="../images/icon.png" alt="" width="30" height="30" class="d-inline-block align-top">
            Brand
        </a>
       
   <button class="navbar-toggler ms-2" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle Navigation">
    <span class="navbar-toggler-icon"></span>
   </button>
   <div class="navbar-collapse collapse justify-content-center" id="navbarCollapse">
<!-- Your code goes here -->
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoiding Page Reloads with Ajax while Removing Entries from a Table in Laravel

I am encountering an issue where I can click the button, but it requires a refresh to execute the delete action. Is there something in my ajax code causing this problem and why is it refreshing? I want the button to instantly delete without the need for a ...

During DragAndDropToObject in Selenium IDE, the initial element in the list is overlooked

I have a webpage that is split into two datagrids. The left datagrid is structured like this: <ul class="left_dg"> <li> <ul></ul> </li> <li> <ul></ul> </li> <li&g ...

Can you explain the function of the forward slash in the HTML img tag?

Currently, I am in the process of learning html/css by following the tutorial provided by W3Schools.com. The part of the code that is causing me some difficulty can be found at http://www.w3schools.com/css/tryit.asp?filename=trycss_vertical-align Specific ...

Retrieve the raw text from the input field instead of the date object

Below is the HTML code for an input field: <div class="input-group input-group-md"> <input id="date" name="date" [readOnly]="disabled" type="text" placeholder="M0/d0/0000 Hh:m0:s0" [placeholder]="pl ...

Ben Kamens has developed a new feature in jQuery Menu Aim where the sub menu will not reappear once it

While exploring Ben Kemens’ jquery-menu-aim, I came across a demonstration on codepen. The code on codepen allows smooth transition from the main menu to the sub menu, but if you move away completely, the submenu remains visible and doesn't disappe ...

Is there a way to showcase whitespacing in HTML?

I have a database full of content that includes whitespace formatting for display on a webpage. However, when viewed on Stackoverflow using the code tag, the formatting changes. The second way shows how it is stored in the database and how I want it to app ...

What is causing this form to submit?

I need help with sending emails via AJAX. My problem is that the form keeps submitting and refreshing, even though I haven't used GET to send anything in the URL. HTML: <form onsubmit="ajaxEmail(); return false;" > <input type=" ...

Is it possible to target an iframe and display text simultaneously?

Trying to create a unique menu that interacts with an iframe and displays text in a separate div upon clicking. Example: • Menu item 1 clicked. • "https://wikipedia.org" loads in the iframe. • Address of the wikipedia page displayed in a diffe ...

Implementing OAuth2 in a Microservices architecture to establish a user account

In my current setup, I am utilizing a React Application along with a separate Express API. My goal is to allow users to register through my React app. To do this, I believe the oauth2 flows should follow these steps: Prompt the user for information (suc ...

Custom Bootstrap 3 Panel Organization

I'm intrigued by the layout shown in the image attached. My attempts to recreate it using a combination of columns and rows have been unsuccessful. Could it be that I'm going about this the wrong way? ...

Adjust the style of cursor - User Interface Expansion Panel Material Design

Using the MiU component "Expansion panel", I encountered an issue. By default, when the user hovers over the panel, the cursor is set to pointer. I attempted to change it to a default cursor, but my modification did not work. The code for my component is ...

Invoke a node.js function from within an HTML document

Seems like a recurring question. The closest solution I found was on this page: execute a Nodejs script from an html page? However, I'm still having trouble grasping it. Here's the scenario: I have an express server set up with the following ...

What is the proper method for submitting a mail form via AJAX without using jQuery?

Currently, I am working on integrating a PHP mail form with AJAX. The aim is to create a straightforward form that collects user information such as phone numbers and sends it to my email address. While I have managed to set up the form to send emails usi ...

Strategies for avoiding the issue of multiple clicks on a like button while also displaying an accurate likes

My latest project involves creating a Like button component that features a button and a likes counter text field. The challenge I am facing is that each time I click on the button, it toggles between like and dislike states. However, if I rapidly press ...

Utilizing a parent scope variable in a callback function

This question delves more into the concept of JavaScript Closures rather than Firebase. The issue arises in the code snippet below, where the Firebase callback fails to recognize the variable myArr from the outer scope. function show_fb() { var myAr ...

Ways to set a random value to a data attribute from a specified array while using v-for in Vue.js?

In my main Vue instance, I have a data attribute that consists of an array containing 4 items: var app = new Vue({ el: '#app', efeitosAos: ['fade', 'flip-up', 'slide-up', 'zoom-in'] ...

Is the RouterModule exclusively necessary for route declarations?

The Angular Material Documentation center's component-category-list imports the RouterModule, yet it does not define any routes or reexport the RouterModule. Is there a necessity for importing the RouterModule in this scenario? ...

AJAX: Enhancing Web Pages without Replacing JavaScript

After attempting to replace a section of javascript on my webpage using AJAX, I encountered an issue where it would not replace the content. When I checked the element with the id 'treintracking', I could see the javascript code from the script b ...

How to keep a window/tab active without physically staying on that specific tab

Whenever I'm watching a video on a website and switch to another tab, the video stops playing. But when I switch back to the original tab, the video resumes. Is there a trick to prevent the video from stopping? I've already tried using scrollInto ...

Trouble Deciphering JSON Array Using Eval Function

I'm facing an issue with two files in my project - one is a PHP file containing an array that is echoed using json_encode, and the other is a JavaScript file containing various functions for a webpage. One particular function in the JavaScript file is ...