Navbar brand in Bootstrap 4 not centered when navbar is collapsed

I am currently working on aligning the brand-item and nav-items on mobile view.

The alignment is incorrect in the mobile view. It is not centered. The "Brand" should be vertically aligned with "link1-3".
In the desktop view, the alignment should be like this.

I have attempted multiple solutions without success.

Here is one of the solutions I tried on Stack Overflow but it did not work.

My current code can be found on Codeply.com.

<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
      <a href="#" class="navbar-brand mx-auto">Brand</a>    
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>   
    <div class="collapse navbar-collapse justify-content-end nav-justified" id="navbarNav">
      <div class="navbar-nav">
        <a class="nav-item nav-link active" href="#">Link 1<span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link" href="#">Link 2</a>
        <a class="nav-item nav-link" href="#">Link 3</a>
      </div>
    </div>
</nav>

What steps should I take to correctly align the brand-item and nav-items?


Edit1: Revised the image and provided a clearer description of my alignment goals.

Edit2: Found a solution! All the answers I received address my issue. However, dmbaughman's answer is the simplest and does not disrupt other content on the website.

Answer №1

An issue that arises is the discrepancy in width between the .navbar-brand link and the links located below it, largely due to the shared space with the menu icon on the right. To address this, one solution is to apply absolute positioning to the menu icon, removing it from the document flow and allowing the brand and links to align correctly.

Existing Situation

Apply the following styles to .navbar-toggler

.navbar-toggler {
  position: absolute;
  top: 8px;
  right: 16px;
}

Outcome

Answer №2

Since you have applied the mx-auto class and it is functioning correctly to center the elements between the start of the screen and the button on the right, there is a slight offset. You can refer to the attached jpeg to see that the elements are now centered due to the button being removed, allowing mx-auto to find the center between the remaining elements.

Initially, you can adjust the margin style in percentage to slightly move the brand to the right.

Does this solution resolve your issue?

To change the brand, you can use the following code snippet:

<a href="#" class="navbar-brand mx-auto"><span style="margin-left:50%">Brand</span></a>

Here is the result of the above code:

https://i.sstatic.net/RR8eB.jpg

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

The issue arises when trying to use jQuery post with dynamically generated variables

Hello there! I have a working jQuery post function, but unfortunately it's not behaving exactly as I hoped. In my code snippet below, the variable "url" should match a specific regex pattern and then trigger a request using jQuery to my PHP file with ...

Dynamic search bar in Javascript showing empty spaces for cards that are not currently visible

Currently, I am in the process of developing an online learning platform which features a catalog of courses. Users can search for courses based on the titles or descriptions using the search bar. To display the catalog, I have utilized bootstrap cards. Th ...

Tips on redirecting a user to a specific page after they have made a selection without doing so immediately

I am a beginner in the world of coding. Currently, I am working on creating templates for an online software application. Users will have the option to select from different choices using radio buttons. However, I want to ensure that users are not redirect ...

Unable to resize static and draggable elements simultaneously

Observe this fiddle to see the demonstration: DEMO If you are interested in the resizing of white divs, check out this link: Resizing of White divs The draggable divs are represented by red while the static divs are shown in white and located in droppabl ...

The Transform feature doesn't seem to be functioning as expected within the Bootstrap

I was experimenting with creating a simple transform transition animation using css and js, so I wrote the following code: (test page) <!DOCTYPE html> <html> <head> <style type="text/css"> *{ ...

How to center align an HTML page on an iPhone device

I am currently testing a HTML5 webpage on my iPhone, utilizing CSS3 as well. The page appears centered in all browsers except for the iPhone, where it is left aligned. I have attempted to use the following viewport meta tag: <meta name="viewport" conte ...

When working with basic shapes such as "rect" or "circle," the inline SVG may not be displayed

This is the code we are using to style our checkboxes: .checkbox-default { display: inline-block; *display: inline; vertical-align: middle; margin: 0; padding: 0; width: 22px; height: 22px; background: url('data:image/ ...

The overflow-anchor property is not effective for scrolling horizontally

My goal is to create a never-ending horizontal scroll that allows users to scroll both left and right. When users scroll to the left, new content should be added to the beginning of the scrollable element (similar to scrolling through a schedule history). ...

What is the best way to horizontally center an image with a transparent background using CSS?

A search button triggers a processing gif image to appear in the center with a transparent background. The content of this function is described below. Thank you for all responses. .img-loader{ text-align: center; width: 50px; display: block; ma ...

Can you guide me on implementing an onclick event using HTML, CSS, and JavaScript?

I am looking for a way to change the CSS codes of the blog1popup class when the image is clicked. I already know how to do this using hover, but I need help making it happen on click instead. The element I want to use as the button; <div class=&quo ...

Prevent global CSS from affecting VueJS by enabling only scoped CSS within components

Is there a way to eliminate all global CSS within a VueJS component and only allow scoped CSS? If so, how can this be achieved? Below is the issue I am trying to address: * { /* Global styles */ } /* Component-specific styles */ .items ul { } .i ...

Is there a way to arrange the outcomes in a single line?

I need help displaying my results in three rows side by side in React/JavaScript using flexbox. Since I only have one CardItem, I can't just copy and paste it three times as it would show the same result in each row. Is there a way to achieve this wit ...

Ways to display the main image on a blog post

This piece of code is designed for displaying the relevant post associated with wp_ai1ec_events function display_event($atts ) { global $wpdb; $event = $wpdb->get_results("SELECT * FROM wp_ai1ec_events ORDER BY start"); ...

Unable to modify the text color within a moving button

Working on a school project and implemented an animated button style from w3 schools. However, I'm struggling to change the text color within the button. Being new to HTML, I would greatly appreciate any insights or suggestions on what might be going ...

The method to disable the dropdown for a select tag in a Polymer Dom-module is not functioning properly

I need help modifying the country dropdown based on a value retrieved from backend code If the disabled_value is 0, I want to hide the dropdown and make it unselectable If the disabled_value is 1, I want to enable the dropdown for selecting countries &l ...

Extract information from an external HTML table and store it in an array

I am looking for a way to extract data from an HTML table on an external site and save it as a JSON file for further manipulation. Is there a method to retrieve table data from an external HTML site that differs from the code example provided? Additional ...

Utilize the Tab key in Textarea fields to insert a tab character ( ) instead of navigating to the

My current issue involves utilizing an HTML textarea named userInput. Whenever I press the tab key, it simply shifts focus to the next element instead of inserting a tab character or some spaces within the textarea. Is there a way for me to configure the ...

Idea: Develop a bookmarklet that generates a header form and deletes it upon submission

After much contemplation and experimentation, I have been grappling with the idea of creating a bookmarklet that displays a header when clicked on any webpage. This header will contain a small form that submits its contents to a server before disappearing. ...

The drop-down tabs are being covered by my <div> element

I've been struggling with the layout of my website due to issues with the positioning of my div tags. Whenever I hover over the arcade tab or other drop-down menus, they end up behind the div for my movies. I would like to reverse this behavior so tha ...

Code for dynamic drop down menu and pagination using PHP and HTML

I'm struggling to understand the logic behind adjusting the number of images displayed on my page. I can't seem to select the first option (5) from the dropdown menu. No matter what I choose, it always defaults to showing 10 images. This issue ...