Position the navigation content slightly to the left and right using Bootstrap 5

Struggling to align links in a Navbar to different sides? I attempted using the classes "ms-auto" and "me-auto" on two lists, but had no success (everything stayed to the left, as shown in the picture). Essentially, I want the "Dropdown" item on the right of the Navbar and the rest on the left.

https://i.sstatic.net/8zFt5.png

 <nav class="navbar fixed-top">
        <div class="navbar-nav position-fixed" id="navbarSupportedContent">
            <a class="navbar-brand" href="/">Company</a>

            <ul class="navbar nav ms-auto">
                <li class="nav-item"><a href="/" class="nav-link">Home</a></li>
                <li class="nav-item"><a href="/Home/About" class="nav-link">About</a></li>
                <li class="nav-item"><a href="/Home/Contact" class="nav-link">Contact</a></li>
                
            </ul>
            <ul class="navbar nav me-auto">
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        Dropdown
                    </a>    
                </li>
            </ul>
        </div>
    </nav>

Answer №1

I made some adjustments by incorporating additional div containers to better organize the logo and left items. Then I utilized flex and margin classes to achieve the desired layout.

<nav class="navbar fixed-top">
    <div class="navbar-nav position-fixed d-flex flex-row w-100" id="navbarSupportedContent">
       <div class="d-flex ms-0"> 
            <a class="navbar-brand" href="/">Company</a>

            <ul class="navbar nav">
                <li class="nav-item pe-2"><a href="/" class="nav-link">Home</a></li>
                <li class="nav-item pe-2"><a href="/Home/About" class="nav-link">About</a></li>
                <li class="nav-item"><a href="/Home/Contact" class="nav-link">Contact</a></li>
                
            </ul>
       </div>
        <div class="ms-auto me-0">
            <ul class="navbar nav">
                <li class="nav-item dropdown">
                    <a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                        Dropdown
                    </a>    
                </li>
            </ul>
        </div>
    </div>
</nav>

Answer №2

I successfully achieved the desired result by utilizing only margin properties (me-auto & me-0):

<ul class="navbar nav me-auto">...</ul>
<ul class="navbar nav me-0">...</ul>

To center the middle element, simply do not specify margins for the middle ul:

<ul class="navbar nav">...</ul>
<ul class="navbar nav me-0">...</ul>

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

When a container is styled with 'overflow-y: auto', it will display additional horizontal and vertical scrolling bars

I have two files, one HTML and one CSS. The HTML file contains the following code: <div class="app"> <div class="header"></div> <div class="main"> <div class="container_1"> ...

How can I dynamically retrieve the width of an image in React as the screen size changes?

I have successfully implemented an image hover effect on my website. When I hover over a certain text, an image appears. The image is responsive, but I am facing an issue where I want the width of the text to be equal to the width of the image. When I resi ...

Angular can be used to compare two arrays and display the matching values in a table

Having two arrays of objects, I attempted to compare them and display the matching values in a table. While looping through both arrays and comparing them by Id, I was able to find three matches. However, when trying to display these values in a table, onl ...

PHP data insertion using filters and validation

I have created a database and implemented a validation system to ensure that fields are not left empty. If the fields are filled and pass through the sanitization function, I then proceed to save the data in the database. <?php // Define variables as ...

Choose checkboxes based on the checkboxes that were previously selected

My goal is to have a checkbox automatically selected based on the previously selected checkbox. For example, if I select the first checkbox (which has a specific class), then only the checkbox with the same class as the first one should be selected using j ...

Dynamic CSS overlay based on database field content without requiring user interaction

Is there a way to automatically display overlays for specific options in a database field using Bootstrap code? The options are SOLD, CONTRACT, and NO. I need different overlays to appear for SOLD and CONTRACT, while no overlay is needed for the NO option ...

Troubleshooting: Issues with AngularJS Data Binding in HTML Tables

I have been working on code from an online tutorial but unfortunately, it is not functioning properly. I have checked the script, app, and controller but still cannot identify the issue. Any assistance would be greatly appreciated! ...

Can integer values be stored in localStorage similar to JavaScript objects and retrieved without requiring typecasting?

After setting an integer value to a localStorage item: localStorage.setItem('a', 1) and checking its data type: typeof(localStorage.a) "string" it shows as a string. I then typecast it to an int for my purposes: parseInt(localStorage.a) My ...

Selenium - pausing for scroll completion

I am currently working on automating downloads from a webpage using the selenium tool. My approach involves creating a firefox driver that opens the page and clicks the download button. However, I have encountered an issue where the button needs to be vis ...

Responsive CSS design that adjusts to fit the content

Struggling to make the wrapper expand with its content... This is the structure: * { padding: 0; margin: 0; } body { background-color: #ccc; background-repeat:repeat; font: Tahoma, Geneva, sans-serif; color: #FFF; } .wrapper { width: 95%; margin: 0 au ...

Center an absolutely positioned div using CSS

What is the best way to position an absolute div at the center? <div class="photoFrame">minimum width of 600px, positioned absolutely</div> jQuery var screenWidth = $(window).width(); $('.photoFrame').css({'margin-left': ...

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...

How can I prevent the arrow keys and space bar from causing my webpage to shift downwards?

I created a game where the arrow keys control movement and the space bar shoots, but every time I press the down key or space bar, it scrolls to the bottom of the web page. Is there a way to adjust the CSS so that it focuses on the game div tag without ...

What is the best way to incorporate this PHP script into an HTML document?

Recently, I made the decision to create a booking calendar and integrate it into my website. However, I encountered an issue when trying to include it in the html file. I attempted using the <?php ?> tag, but unfortunately, it did not display as expe ...

How to exclude elements nested inside another element using CSS or XPath techniques

Presented here is a snippet of XML code: <xml> <section> <element>good</element> <section class="ignored"> <element>bad</element> </section> </section> </xml> Identifying a ...

Bootstrap fixed top navbar with a double color scheme

I would like to replicate the following image: using a navbar-fixed-top with Bootstrap. Currently, I have added the following custom CSS: navbar-fixed-top.css .navbar { background-color: #8CB5A0; background-image: none; } Here is the Bootstrap ...

How can I verify an element's attribute while employing Event Delegation?

Is there a method to determine if a particular element has been activated using Event Delegation, based on its attribute, class, or ID? <ul> <li><button>Make the first paragraph appear</button></li> <li><butto ...

show fixed div when in view

After seeking inspiration from this specific thread on SO, I still need some help. The challenge I'm facing involves displaying a sticky footer within a parent div that is positioned halfway down the page. I've tried multiple tutorials, but none ...

Is it possible for the JavaScript DOM API to retrieve the exact casing of attribute and tag names?

Is it possible to retrieve the original casing of an attribute name or tag name from the source? The attribute name is in lowercase The tag name is in uppercase The local element name is in lowercase I am looking for a solution that doesn't require ...

Stopping halfway through a jQuery toggle width animation to close again

Perhaps the question may be a bit unclear, but allow me to provide an example. When repeatedly clicking the button that toggles the width, the div continues to animate long after the button press, which is not visually appealing. My desired outcome is for ...