Bootstrap 4.0 fixed navbar - Resolved alignment problem

I am working on dividing my navbar into two equal parts. I want the right side to contain my menu and the left side to have my icon and search bar. When the search bar is clicked, it should extend to fill the rest of the half in the left part. I have managed to use a fixed navbar successfully, but I am struggling with aligning my menu to the right side.

This is how it currently looks: https://i.sstatic.net/6X9AO.png

As shown in the image, the menu is not completely aligned to the right. After trying the suggestions from Stack Overflow, using float: right did not work for me. I am unsure of how to fix this issue. Here is the code I am currently using:

<nav class="navbar fixed-top navbar-expand navbar-light bg-light">
        <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
            <img src="images/some_logo.png" class="img-rounded" alt="some_logo" style="height: 40px; width: 80px">
            <div class="px-2">
                <form>
                    <input type="text" name="search" placeholder="Search ...">              
                </form>
            </div>
        </div>
        <div class="navbar-collapse collapse w-100 order-4 order-md-0 dual-collapse2">
            <ul class="navbar-nav">
                <li class="nav-item-active">
                    <a class="nav-link" href="#/">Home<span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/product">Products</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/services">Services</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/career">Career</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/contact">Contact Us</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#" onclick="openNav()" style="width: 60px; font-size: 25px; margin-top: -7px">&#9776;</a>
                </li>
            </ul>           
        </div>
    </nav>

In the browser's Element tab, I tried adjusting the margin-right, but the values were negative, which doesn't seem like the correct approach. I am relatively new to frontend development and finding the Bootstrap framework quite challenging. Any assistance or guidance would be greatly appreciated.

Answer №1

ul.navbar-nav{
position:absolute;
right:0px;
}
<!DOCTYPE html>
<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.bundle.min.js" integrity="sha384-feJI7QwhOS+hwpX2zkaeJQjeiwlhOP+SdQDqhgvvo1DsjtiSQByFdThsxO669S2D" crossorigin="anonymous"></script>


</head>

<body>
<nav class="navbar fixed-top navbar-expand navbar-light bg-light">
        <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
            <img src="images/some_logo.png" class="img-rounded" alt="some_logo" style="height: 40px; width: 80px">
            <div class="px-2">
                <form>
                    <input type="text" name="search" placeholder="Search ...">              
                </form>
            </div>
        </div>
        <div class="navbar-collapse collapse w-100 order-4 order-md-0 dual-collapse2">
            <ul class="navbar-nav">
                <li class="nav-item-active">
                    <a class="nav-link" href="#/">Home<span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/product">Products</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/services">Services</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/career">Career</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/contact">Contact Us</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#" onclick="openNav()" style="width: 60px; font-size: 25px; margin-top: -7px">&#9776;</a>
                </li>
            </ul>           
        </div>
    </nav>

</body>
</html>

Answer №2

When I encountered a similar issue not too long ago, I found that using the built-in bootstrap classes mr-auto and ml-auto on your uls can help resolve it. It is recommended to have a separate ul for each section of the navbar, such as one for the search bar and another for the links on the right side.

<nav class="navbar fixed-top navbar-expand navbar-light bg-light">
    <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2 mr-auto">
        <img src="images/some_logo.png" class="img-rounded" alt="some_logo" style="height: 40px; width: 80px">
        <div class="px-2">
            <form>
                <input type="text" name="search" placeholder="Search ...">
            </form>
        </div>
    </div>
    <div class="navbar-collapse collapse w-100 order-4 order-md-0 dual-collapse2">
        <ul class="navbar-nav ml-auto">
            <li class="nav-item-active">
                <a class="nav-link" href="#/">Home
                    <span class="sr-only">(current)</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#/product">Products</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#/services">Services</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#/career">Career</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#/contact">Contact Us</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" onclick="openNav()" style="width: 60px; font-size: 25px; margin-top: -7px">&#9776;</a>
            </li>
        </ul>
    </div>
</nav>

Answer №3

Thanks to @ZimSystem's solution here, using ml-auto in bootstrap 4 beta helps in aligning items to the right. Finally, I managed to make this code work.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<nav class="navbar fixed-top navbar-expand navbar-light bg-light">
        <div class="navbar-collapse collapse w-100 order-1 order-md-0 dual-collapse2">
            <img src="images/some_logo.png" class="img-rounded" alt="some_logo" style="height: 40px; width: 80px">
            <div class="px-2">
                <form>
                    <input type="text" name="search" placeholder="Search ...">              
                </form>
            </div>
        </div>
        <div class="navbar-collapse collapse w-100 order-4 order-md-0 dual-collapse2">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item-active">
                    <a class="nav-link" href="#/">Home<span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/product">Products</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/services">Services</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/career">Career</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#/contact">Contact Us</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#" onclick="openNav()" style="width: 60px; font-size: 25px; margin-top: -7px">&#9776;</a>
                </li>
            </ul>           
        </div>
    </nav>

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

Transitioning from feelings to sewing: incorporate a duo of properties within variations and breakpoints

I was thinking about how to convert this styled-emotion code into stitches. Essentially, I need to handle the onlyShowOnDesktop and breakpoint props. The definition of breakpoint is as follows: const breakpoint = isTopNavigationBreakPoint ? theme.defaultB ...

The primary content division is trapped behind the side navigation bar

Why is my sidebar fixed on the left of the screen overlapping with the main content? I want the main content to be displayed next to the navbar and scroll up and down while keeping the nav bar in place, but no matter what styling I apply, it doesn't w ...

Simplified user interface for detecting radio button clicks

Currently working on a form that includes radio buttons, where an update function is triggered whenever there is a user input change. The challenge I am facing is how to incorporate user-friendly radio buttons with a larger button area encompassing both t ...

Adjust the order of list items based on the resolution change

Is there a way to rearrange the order of the list items in an ul when the screen resolution changes, specifically having the last li appear in the first place? Edit: I am attempting to achieve this by using flexbox. .postpreview ul { list-style: none; d ...

Customizing element borders based on device size using bootstrap

I'm currently working with Bootstrap to design a mobile-first responsive layout. I have set up multiple rows and columns that I adjust based on different screen size categories. Is there a way to style borders using just Bootstrap classes for the var ...

Customizing the appearance of checkboxes in React Material-Table filtering feature

Is there a way to modify the style of checkboxes in filtering? For instance, if I wish to adjust the dimensions of the checkbox that appears for the Birth Place field, what steps should I take? https://i.stack.imgur.com/pZLqQ.png ...

The onClick event in HostListener is intermittent in its functionality

While attempting to create a dropdown box in Angular by following these examples, I am encountering inconsistent results. Below is the code I have used: HTML <button (click)="eqLocationDrop()" id="eqLocButton"><i class="fas fa-caret-down">< ...

Items in a CSS masonry container with a 'column count' feature showcasing drop shadows that elegantly span across multiple lines

I've been experimenting with CSS column count to create a masonry effect for my items. Everything was going smoothly until I added a subtle drop shadow to the items. Upon closer inspection in the jsfiddle link provided, you'll notice that the dr ...

Is it possible to adjust the height of input fields in MUI Reactjs?

Having trouble adjusting the height of input textfields using in-line css. <FormControl sx={{ "& .MuiOutlinedInput-notchedOutline": { borderColor: "blue", }, "&.Mui-focused .MuiOutlinedInpu ...

Updating div visibility based on form content in PHP

I am struggling to find a way to filter a page filled with divs based on form input to determine whether they should be displayed or not. The layout is akin to a collection of articles, where each div showcases an image and some text that links to the comp ...

Creating an AI adversary for a simple Tic Tac Toe game board: a step-by-step guide

Being new to programming, I recently completed a basic Tic Tac Toe gameboard successfully. However, as I progress to the next phase of my assignment which involves implementing an AI opponent, I encountered several challenges. As a novice in programming, ...

Explore our Enhanced Django Search Page featuring Efficient Query Pagination!

Hello, I recently set up a basic search form and search view to display search results. I now want to incorporate pagination, but I've run into an issue with the URL structure. Currently, my search URL looks like ../search?q=Bla When attempting to ad ...

CSS not working with fixed positioning

As part of a school project, I am required to utilize the position fixed property to correctly position the cookie statement at the bottom right of the screen. However, when I attempt to move the element, it does not seem to display properly. ...

What could be causing the "Cannot POST /api/" error to occur when attempting to submit a form?

Having issues with my basic website and struggling to find a solution. As a complete beginner in this field, I am stuck and need some guidance. Accessing http://localhost:3000/class/create works perfectly fine when running the server. However, trying to a ...

What is the best method for incorporating CSS into an Angular application's embedded PowerBI report, which is displayed

Is there a way to incorporate external CSS or another method to apply styles to a PowerBI report embedded in an Angular application as an iframe? Specifically, I want to remove the scrollbar to ensure that the report seamlessly fits within the angular page ...

Incorporating the FLEX property into my code has been a challenge as it is not functioning properly on iOS 7 and 8 devices. Does anyone have any tips or suggestions on how to

While utilizing FLEX in my code, I noticed that the output is not what I expected on iOS 7/8 devices. <div className="modal-row"> <div className="col1"> <span>{dict.YourServiceNumber}</span> </div> <div ...

What are some ways to implement drop-down functionality using CSS?

I've been working on tweaking the CSS of this website for what feels like an eternity. I just can't seem to figure out how to make the "medical" menu item drop down to display the rest of the hidden menu items. I've managed to unhide and exp ...

What is the process for generating a GET request for selected checkboxes and subsequently showcasing the data on an HTML webpage?

Currently working on an app project and need assistance with implementing a feature. I have successfully set up a POST method for the checkboxes that are checked, but I am unsure how to retrieve this data and display it on my HTML page using a GET method ...

How can you prevent a draggable element from surpassing the bottom of the screen?

I'm dealing with an element that I want to make draggable only along the Y-axis. It needs to be able to go past the top of the screen, but I need to restrict it from going past the bottom of the screen. I recently came across the containment feature i ...

Is it possible to create a table using a loop in an SQLite query?

Welcome In the company where I work, Excel is currently being used to manage a large database of items for cost estimation purposes. To simplify this process, I am developing an Electron App that will replace Excel with a more user-friendly interface and ...