Centered item in Bootstrap navbar with others aligned to the right

Experimenting with ReactJS and Bootstrap 4 to create a customized NavBar.

Seeking guidance on aligning elements in the NavBar, such as centering some and placing others on the right side.

Particularly interested in positioning the logout icon on the right-hand side of the NavBar.

Here's the current state:

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

Review the existing code snippet:

render () {
        return (
            <nav className="navbar navbar-dark bg-primary fixed-top">
                <Link className="navbar-brand" to="/">
                    App
                </Link>

                {
                    !localStorage.getItem('token') &&
                    <button className="btn btn-dark" onClick={this.loginClicked}>Login</button>
                }
                {
                    localStorage.getItem('token') &&
                    <div className="mx-auto order-0">
                        <button className="btn btn-primary btn-lg navbar-btn">
                            <i class="fas fa-file-invoice-dollar fa-lg"></i>
                            <sup className="notification-badge"><span class="badge badge-success">1</span></sup>
                        </button>
                        <button className="btn btn-primary btn-lg navbar-btn">
                            <i class="fas fa-envelope fa-lg"></i>
                        </button>
                        <button className="btn btn-primary btn-lg navbar-btn">
                            <i class="fas fa-cogs fa-lg"></i>
                        </button>
                        <button className="btn btn-outline-danger btn-lg" onClick={this.logoutClicked}>
                            <i class="fas fa-sign-out-alt fa-lg"></i>
                        </button>
                    </div>

                }
            </nav>
        );
    }

Desired outcome for the NavBar layout:

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

Answer №1

Follow these steps to make the necessary adjustments:

  1. No need to modify the flex properties, as the bootstrap nav already has display flex & space-around by default.
  2. Organize your HTML content accordingly - A. include navbar-brand B. a parent div containing the center elements C. incorporate your logout button
  3. Eliminate the margin auto classes (mx-auto and order-0) from your code, as they are causing issues.

Update your React code with the following changes:

render() {
  return (
    <nav className="navbar navbar-dark bg-primary fixed-top">
      <Link className="navbar-brand" to="/">
        App
      </Link>

      {!localStorage.getItem("token") && (
        <button className="btn btn-dark" onClick={this.loginClicked}>
          Login
        </button>
      )}
      {localStorage.getItem("token") && (
        <React.Fragment>
          <div className="first-part">
            <button className="btn btn-primary btn-lg navbar-btn">
              <i class="fas fa-file-invoice-dollar fa-lg" />
              <sup className="notification-badge">
                <span class="badge badge-success">1</span>
              </sup>
            </button>
            <button className="btn btn-primary btn-lg navbar-btn">
              <i class="fas fa-envelope fa-lg" />
            </button>
            <button className="btn btn-primary btn-lg navbar-btn">
              <i class="fas fa-cogs fa-lg" />
            </button>
          </div>
          <div className="second-part">
            <button
              className="btn btn-outline-danger btn-lg"
              onClick={this.logoutClicked}
            >
              <i class="fas fa-sign-out-alt fa-lg" />
            </button>
          </div>
        </React.Fragment>
      )}
    </nav>
  );
}

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css" />
<nav class="flex-container navbar navbar-dark bg-primary fixed-top">
    <a class="navbar-brand" to="/"> App
    </a>
    <div class="first-part">
        <button class="btn btn-primary btn-lg navbar-btn">
        <i class="fas fa-file-invoice-dollar fa-lg"></i>
        <sup class="notification-badge"><span class="badge badge-success">1</span></sup>
        </button>
        <button class="btn btn-primary btn-lg navbar-btn">
        <i class="fas fa-envelope fa-lg"></i>
        </button>
        <button class="btn btn-primary btn-lg navbar-btn">
        <i class="fas fa-cogs fa-lg"></i>
        </button>
    </div>
    <div class="second-part">
        <button class="btn btn-outline-danger btn-lg">
        <i class="fas fa-sign-out-alt fa-lg"></i>
        </button>
    </div>
</nav>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>

Answer №2

To achieve the desired layout, you can utilize the CSS properties display: flex and align-content: space-between in the following manner:

.container{
 width: 100%;
}
middle_to_right{
 padding-left: 50%;
 background-color: red;
 display: flex;
 flex-flow: row nowrap;
 flex-basis: 100%;
 justify-content: space-between;
}

.left-section{
 display: flex;
flex-flow: row nowrap;
}

.icon{
 width: 50px;
 height: 50px;
 background-color: yellow;
}

.left-section .icon{
 background-color: green;
}
<div class="container">
  <div class="middle_to_right">
    <div class="left-section">
      <div class="icon">
      </div>
      <div class="icon">
      </div>
      <div class="icon">
      </div>
    </div>
    <div class="right-section">
      <div class="icon">
      </div>
    </div>
  </div>
</div>

Answer №3

Explained in detail here and also here, it is important to take into account the width of neighboring flexbox items to ensure that the center content is truly centered in the viewport.

The easiest approach using Bootstrap 4 would involve utilizing the w-100 utility classes to guarantee that the three distinct flexbox children consistently occupy the width equally, aligning as needed...

<nav class="navbar navbar-dark bg-dark fixed-top navbar-expand">
    <a class="navbar-brand w-100" to="/">
        App
    </a>
    <div class="d-flex">
        <div class="text-nowrap">
            <button class="btn btn-primary btn-lg navbar-btn">
                <i class="fas fa-star fa-lg"></i>
            </button>
            <button class="btn btn-primary btn-lg navbar-btn">
                <i class="fas fa-envelope fa-lg"></i>
                <sup><span class="badge badge-success">1</span></sup>
            </button>
            <button class="btn btn-primary btn-lg navbar-btn">
                <i class="fas fa-cogs fa-lg"></i>
            </button>
        </div>
    </div>
    <div class="w-100 text-right">
        <button class="btn btn-outline-danger btn-lg" onclick="{this.logoutClicked}">
            <i class="fas fa-sign-out-alt fa-lg"></i>
        </button>
    </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

Attaching dynamic data to a specific element within an array

I have successfully created a demo where elements can be dropped into a specific area and their top and left values are displayed. I have also added functionality to remove dropped items and move them between different blocks. However, I am encountering so ...

`A mistake occurred while building in the package.json file`

While attempting to run all build processes by using the command npm run build:css, I encountered an error indicated below. Even after running npm cache clean --force, the issue remains unresolved. https://i.sstatic.net/4edDo.png npm ERR! code ELIFECYCLE ...

CSS selector for detecting elements with empty or whitespace-only content

A special selector called :empty allows us to target elements that are entirely devoid of content: <p></p> However, in some cases, the element may appear empty due to line breaks or white spaces: <p> </p> In Firefox, a workar ...

Is there a way to include text beneath the navigation bar and other elements on the page?

I am stuck trying to add some text below my header and navigation bar. Here is the code I've been working with: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta htt ...

Delete the top row from the table

After extensive searching on various websites, I am still unable to resolve my html or CSS issue. An unexpected line is appearing above the table here. Here's the code snippet that is causing trouble: <table border="0" width="840" cellspacing="0" ...

Convert all SASS code into CSS within a Nuxt project

I am currently tackling a project that involves using SASS for part of the code. However, I have made the decision to transition from SASS to vanilla CSS. My goal is to find a way to convert all SASS segments into CSS either programmatically across the ent ...

What causes custom CSS properties to be overridden by Material UI CSS class properties?

I encountered an issue while attempting to utilize an npm package containing a Typography element from Material UI. The code is authored by me. Upon integrating it into a project, I noticed that the typography's CSS class properties were overpowering ...

Updating text color with Ajax response value

I need assistance with updating the text color of a sensor value displayed using html/ajax. Currently, the sensor value is being displayed successfully, but I want the text color to change based on the value. Here's an example: if value < 50 then f ...

The bootstrap code I wrote runs smoothly on Codeply's online compiler, but unfortunately it's not functioning properly in VS code

<nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="">OurCoolBrand</a> <button class="navbar-toggler" type="button&quo ...

Inaccurate audio timestamps in Chrome

I am currently working on a web application that features an audio component sourced from a local .mp3 file lasting approximately 1 hour. I have encountered an issue where, upon clicking the seekbar to jump to a specific point in the audio (e.g., 00:01:00) ...

Google Authentication integration in React Native

What is the process for enabling logging in my application created with react-native init using a Google account? ...

Arranging date and time in jQuery based on AM/PM notation

I have written some JavaScript code to sort dates in ascending order (from newest to oldest). I have successfully sorted the dates, but I am having trouble sorting the time with AM or PM using a 12-hour format. I can do it in a 24-hour format, but not in ...

Tips for avoiding repetitive querying when using server-side rendering and dynamic metadata

I have implemented Next.js 13 with the App routing system, as shown in the code. I have a SSR function called FetchingProduct which calls an API to generate dynamic metadata and send data props to a client page component. There is a concern that the fetc ...

Creating a Comprehensive Page Design with Bootstrap and the Latest Version of Vue

My goal is to develop a Single Page Application using Vue3 and the Bootstrap 5 framework with a simple layout of Header -> Content -> Footer. I want the Content section to fill the space between the header and footer, ensuring there is no overlap. Ho ...

Conceal div elements containing identical information by utilizing jQuery

I'm dealing with a webpage that pulls in a long section of divs from another application. Unfortunately, I can't filter the divs before they appear, but I urgently need to hide any duplicate data within certain values using jQuery. The duplicate ...

Creating a table with adjustable row heights is a great way to enhance the user

Can the height of an HTML table row be adjusted dynamically by dragging and dropping, similar to how it's done in this demo (https://reactdatagrid.io/docs/row-resize), but for free? ...

Enhancing text with custom gradient using CSS styling

I am facing an issue where uploading an image with text is not helpful for Google search visibility. I am looking to add a specific gradient style to my text. Could anyone assist me in achieving this particular text look that I have in mind? I came acros ...

The Material UI Menu does not close completely when subitems are selected

I am working on implementing a Material UI menu component with custom MenuItems. My goal is to enable the closure of the entire menu when clicking outside of it, even if a submenu is open. Currently, I find that I need to click twice – once to close the ...

What is the best way to increase the size of a specific text style?

Recently, I created a post in WordPress that included some code samples. To ensure the code was displayed neatly, I utilized the "preformatted" style. However, upon previewing it, I noticed that the text within the code samples was incredibly small, almost ...

Tips for adjusting the size of your Navigation bar

I've noticed that most of the questions I've come across are related to WordPress or Bootstrap nav bars, which I'm not familiar with. I've created mine using CSS. I want to enhance my navigation bar for mobile users by making the butto ...