The user avatar seamlessly glides along the menu within the Bootstrap 5 navigation bar

Question:

I am working on a project using Bootstrap 5 for creating a sticky navigation bar in Django. The issue I am facing is that when the navigation menu expands on smaller screens, the user icon (fa-user) moves along with it. I want the user icon to remain fixed on the right side while only the navigation menu expands and collapses.

Below is the code for the navigation bar:

<nav class="navbar navbar-transparent fixed-top navbar-expand-lg">
    <div class="container-fluid d-flex justify-content-arround">
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup"
                aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <a class="navbar-brand" href="#">
            <img class="imagotipe" src="{% static 'img/imagotipe-white-02.svg' %}"
                 alt="imagotipe">
        </a>
        <div class="collapse navbar-collapse text-center" id="navbarNavAltMarkup">
            <div class="navbar-nav">
                <a class="nav-link active" aria-current="page" href="#">Home</a>
                <a class="nav-link" href="#">Me</a>
                <a class="nav-link" href="#">About</a>
                <a class="nav-link" href="#">Contact</a>
            </div>
        </div>
        <div>
            <a href="#">
                <i class="fa-solid fa-user"></i>
            </a>
        </div>
    </div>
</nav>

Problem:

The user icon (fa-user) moves with the menu when it opens or closes on smaller screens. I want the icon to stay fixed on the right side of the navigation bar while only the navigation menu should expand and collapse. How can I achieve this and make sure the user icon remains fixed on the right without moving when the menu expands or collapses?

https://i.sstatic.net/YjrBIbzx.png https://i.sstatic.net/tJbzgHyf.png

Answer №1

In the latest version of Bootstrap, Bootstrap 5, you can easily customize the order of flex items on large screens using the css class order-lg-[order]. More information can be found in the documentation here

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d5b7babaa1a6a1a7b4a595e0fbe6fbe6">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.1/css/all.min.css" integrity="sha512-5Hs3dF2AEPkpNAR7UiOHba+lRSJNeM2ECkwxUIxC1Q/FLycGTbNapWXB4tP889k5T5Ju8fs4b1P5z/iB4nMfSQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  </head>
  <body>
    <nav class="navbar navbar-transparent fixed-top navbar-expand-lg">
        <div class="container-fluid d-flex">
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavAltMarkup"
                    aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <a class="navbar-brand" href="#">
                <img class="imagotipe" height="50px" src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/640px-Google_2015_logo.svg.png"
                    alt="imagotipe">
            </a>
            <div class="d-flex order-lg-3">
                <a href="#">
                    <i class="fa-solid fa-user"></i>
                </a>
            </div>
            <div class="collapse navbar-collapse order-lg-2" id="navbarNavAltMarkup">
                <div class="navbar-nav">
                    <a class="nav-link active" aria-current="page" href="#">Home</a>
                    <a class="nav-link" href="#">Me</a>
                    <a class="nav-link" href="#">About</a>
                    <a class="nav-link" href="#">Contact</a>
                </div>
            </div>
        </div>
    </nav>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b6964647f787f796a7b4b3e25382538">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
  </body>
</html>

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

Creating an Email Template using Razor View Page

After creating an Email Template using Razor Syntax, I encountered an issue when sending it via C# code and SMTP protocol. The email body displayed the raw Razor and HTML markups instead of rendering properly. Could it be that Razor Pages are not suitable ...

"Adjust the opacity of two div elements in CSS while also applying a negative

https://jsfiddle.net/1zj71an3/1/ html: <div id="one"> aaaaaaaa </div> <div id="two"> bbbbbbbb </div> css: #one{ background-color:yellow; width:300px; height:300px; opacity: 0.5; } #two{ background-color:blue; width ...

Maintaining data integrity across various form submissions

I'm currently working on developing a web service that allows users to log in to the backend, fill out a form, and have their submitted information displayed on the front-end. While the data is successfully being submitted to the PostgreSQL database, ...

Is there a way to use HTML to prevent the user from navigating away from the current page when submitting a form? Could jQuery, PHP, or a combination of

Is it achievable to send a message using PHP and keep the user on the same page after clicking "Send Message" button? Perhaps changing the submit button to a success message on the following page: . Can this task be accomplished with PHP and jQuery? ...

Animating DIVS with collapsible content using jQuery

I'm currently facing some CSS issues while developing a small app. You can check it out here: When the "click me" button is pressed, I can't seem to move the content down properly. Any suggestions on how to fix this? I suspect it's related ...

The border-radius property in CSS selectively rounds only one corner of the modal div window

There seems to be an issue with the border-radius property on modal divs in my project. When I apply the property, only one or two corners of the div round while the other corners remain unchanged. For a visual reference, please check out this image linke ...

Creating a PHP code that sends an email with an attachment of an empty

I have encountered an issue with the script below while trying to send an attachment with a mail. Despite successfully sending the mail, the attached file appears empty. I have carefully reviewed the script but am unable to identify the source of the error ...

Ways to adjust the size of v-btn

Objective: My current task involves enhancing the customization options for Vue components, specifically focusing on adjusting the size of a button component based on user input. Our project utilizes Vuetify. Previous Code: <v-btn @click.prevent=&quo ...

Preventing navbar resizing on websites

I'm trying to have a section on the navbar dedicated for text or alerts that appear based on certain events. However, even when the alert is hidden, it still affects the height of the navbar. How can I resolve this issue? It may be difficult to see i ...

The battle of CSS3 Media Types: targeting all devices versus the undefined

What sets apart these two media queries: @media all and (min-width: 480px) { ... } and @media (min-width: 480px) { ... } ...

What is the best way to create a collage of images with a random and unique arrangement?

Recently, I stumbled upon a website and was intrigued by the unique effect of images randomly appearing on the screen. I'm curious about how this effect can be achieved. Is it possible to use CSS Grid to divide the screen and designate some grid ite ...

What is the best way to link the roll button to a specific video URL?

I am currently working on a project that involves assigning specific videos to 6 roll buttons for continuous play. For instance, I want the first roll button to display a yellow dice and the second button to show a blue dice, and so on. As of now, I have ...

What could be causing the jQuery slidedown to malfunction within my table?

My slider is not working when I include a table on my website. The slider works fine without a table, but I need to display members in a table format. Can anyone help me identify the issue with my code? The information needs to be displayed below the nam ...

Guidelines on triggering audio playback with an HTML page hyperlink click

I've made some progress with this code: <audio id="player" src="../media/Click.mp3"></audio> <div> <a href="index.html" onclick="document.getElementById('player').play()">Home</a> </div> However, I& ...

Detecting the preferential usage of -webkit-calc instead of calc through JavaScript feature detection

While it's commonly advised to use feature detection over browser detection in JavaScript, sometimes specific scenarios call for the latter. An example of this can be seen with jQuery 1.9's removal of $.browser. Despite the general recommendatio ...

Arrangement containing 4 separate div elements - 2 divs are fixed in size, 1 div adjusts dynamically (without scroll), and the remaining div fills the

I am trying to implement a specific layout using HTML and CSS on a webpage. So far, I have successfully created the black, red, and blue areas, but I am facing challenges with the scrollable green content. It currently has a static height, but I need it to ...

Troublesome Misalignment of HTML Bootstrap Grid Columns

Is there a way to align filter boxes into a grid that spans 3 blocks per row without losing the bootstrap look? I haven't attempted to remove any of the navbar/input group classes, but I'm wondering if there is a clean solution to fix this. How s ...

Removing a data entry from a PHP-generated MySQL table

In the process of developing a system, I have encountered a need to display database records in an HTML table. The functionality for creating the HTML table and retrieving data is working as expected. However, I am facing an issue with adding a column cont ...

Using Javascript to extract a custom attribute from a button element

In my HTML, there is a button that has a custom property called data-filter-params: <button class="button" type="submit" data-filter-params="{'Type', 'Filter.Type'},{'Code','Filter.Code'}" >Filter</button&g ...

Calculate the width of the outer division based on the chosen inner division

Looking for a solution related to an HTML segment? <div class="outer" style="min-width: 150px;"> <div class="inner1"> <span>Lorem ipsum dolor sit amet</span> // Possibly long, truncate if needed <div class="inner2"/& ...