How can you align an icon to the right in a Bootstrap4 navbar while keeping it separate from the toggle menu?

Looking to utilize the Twitter Bootstrap framework for structuring my navbar with distinct "left", "middle", and "right" sections, where the middle portion collapses beneath the navbar-toggler (burger menu) when space is limited.

For a self-contained example, check out this Codepen: https://codepen.io/fiver/pen/eKJOMG

My current struggle lies in defining the "icon" elements correctly:

<nav className="navbar navbar-expand-sm navbar-dark bg-dark ">
  <a className="navbar-brand" href="#">ExampleApp</a>
  <button className="navbar-toggler" type="button"
    data-toggle="collapse" 
    data-target="#navbarNav" >
    <span className="navbar-toggler-icon"/>
  </button>

  <a className="navbar-brand navbar-right" href="#">Icon1</a>

  <div id="navbarNav" className="collapse navbar-collapse" >
    <ul className="navbar-nav">
      <li nav items></li>
      ....
    </ul>
  </div>

  <a className="navbar-brand navbar-right" href="#">Icon2</a>
</nav>

When the horizontal space is sufficient and the navbar-toggle is unnecessary, Icon1 seems to be misplaced:

https://i.sstatic.net/5lvCO.png

Conversely, when the navbar-toggler is activated and the menu expands, Icon2 appears to be in the incorrect position:

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

Currently, the plan is to incorporate only one of the Icon menu items in my application. The inclusion of two icons is solely for demonstrating the differing behaviors based on their placement.

Interestingly, both positions appear to function correctly when the navbar-toggler menu is active but collapsed:

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

Any suggestions on how to achieve the desired behavior of positioning the icon like Icon2Icon1

Answer №1

  • For the navbar-container, utilize d-flex and justify-content-between classes.
  • Add the w-100 class to the nav element.
  • Insert
    <a class="navbar-brand navbar-right" href="#">Icon1</a>
    immediately after the ExampleApp anchor tag.
  • Apply the ml-auto class to the navbar-toggler.
  • Include
    <a class="navbar-brand navbar-right pt-2" href="#">Icon2</a>
    after the nav element.

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />

<div style="background-color: rgb(52, 58, 64);">
  <div id="navbar-container" class="container d-flex justify-content-between">
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark  w-100">
      <a class="navbar-brand" href="#">ExampleApp</a><a class="navbar-brand navbar-right" href="#">Icon1</a>
      <button class="navbar-toggler ml-auto" type="button" data-toggle="collapse" data-target="#navbarNav">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div id="navbarNav" class="collapse navbar-collapse">
        <ul class="navbar-nav">
          <li class="nav-item active"><a class="nav-link" href="#">Item 1</a></li>
          <li class="nav-item "><a class="nav-link" href="#">Item 2</a></li>
          <li class="nav-item dropdown"><a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">Options</a>
            <div class="dropdown-menu"><a class="dropdown-item" href="#">Drop Item 1</a><a class="dropdown-item" href="#">Drop Item 2</a>
              <div class="dropdown-divider"></div><a class="dropdown-item" href="#">Drop Item 3</a></div>
          </li>
        </ul>
      </div>
    </nav><a class="navbar-brand navbar-right pt-2" href="#">Icon2</a></div>
</div>

https://codepen.io/anon/pen/NzxqxW

Update

If you wish to align both icons to the right side, follow these steps.

  • Apply d-flex and justify-content-between classes to the navbar-container.
  • Use the w-100 class for the nav element.
  • Assign the ml-auto class to the navbar-toggler.
  • Create a new div with classes d-flex justify-content-between outside of the nav to contain both icons.

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet" />

<div style="background-color: rgb(52, 58, 64);">
  <div id="navbar-container" class="container d-flex justify-content-between">
    <nav class="navbar navbar-expand-sm navbar-dark bg-dark  w-100">
      <a class="navbar-brand" href="#">ExampleApp</a>
      <button class="navbar-toggler ml-auto" type="button" data-toggle="collapse" data-target="#navbarNav">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div id="navbarNav" class="collapse navbar-collapse">
        <ul class="navbar-nav">
          <li class="nav-item active"><a class="nav-link" href="#">Item 1</a></li>
          <li class="nav-item "><a class="nav-link" href="#">Item 2</a></li>
          <li class="nav-item dropdown"><a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown">Options</a>
            <div class="dropdown-menu"><a class="dropdown-item" href="#">Drop Item 1</a><a class="dropdown-item" href="#">Drop Item 2</a>
              <div class="dropdown-divider"></div><a class="dropdown-item" href="#">Drop Item 3</a></div>
          </li>
        </ul>
      </div>
    </nav>
    <div class="d-flex justify-content-between pt-2">
      <a class="navbar-brand navbar-right" href="#">Icon1</a>
      <a class="navbar-brand navbar-right" href="#">Icon2</a></div>
  </div>
</div>

https://codepen.io/anon/pen/RJrxpL

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

Navigating with Anchors, Styling and jQuery

Firstly: Apologies in advance for any language errors as English is not my native tongue. :) The Scenario Here's the deal: I'm attempting to create a single button that, when clicked by the user, automatically scrolls down to the next DIV. Each ...

Stopping the parent onclick event from propagating to a child element within a bootstrap modal

I am currently working with angularjs and bootstrap, incorporating nested onclick events using Angular's ng-click in various HTML elements. One is located in a table header to display different sort icons and execute the sorting logic when the header ...

CSS - Mysterious Gaps Found in Div Block

Below is the code snippet I am currently working with: html { height: 100%; overflow: hidden; } body { height: 100%; color: #bdc3c7; font-family: Montserrat; background-color: #3E4651; } .nav { ...

The CSS for a VueJS compiled app seems to fail to apply properly unless manually copied and pasted into the browser's style editor

After compiling my vuejs app with npm run build, I noticed that the CSS does not display when viewing it in Firefox. Surprisingly, the styles do load in the network tab and appear under the style editor, but with "0 rules". However, everything displays fin ...

Having difficulty maintaining the consistent size of the MathJax math font in relation to the surrounding text

Issue I am currently using MathJax to display mathematical equations on my webpage: https://i.sstatic.net/EfvVq.png The problem I am facing is that I want the math font to appear larger than the surrounding text, as depicted in the image above. However, ...

Flashing Effect of Angular Ui-Bootstrap Collapse During Page Initialization

Below is the code snippet I've implemented to use the ui-bootstrap collapse directive in my Angular application. HTML: <div ng-controller="frequencyCtrl" style="margin-top:10px"> <button class="btn btn-default" ng-click="isCollapsed = ...

Is there a way to adjust the label size for a material ui TextField component?

My TextField element is defined like this: <TextField id="standard-with-placeholder" label="First Name" className={classes.textField} margin="normal" /> It currently appears as shown in the image below: https://i.sstatic.net/aYi1a.png How ...

Utilizing JavaScript to trigger the :hover pseudo-class and implement event transfers

Consider this situation: You have a scenario where two images are stacked on top of each other. The image with the highest z-index is transparent and handles click events, similar to Google's Map API, while the image below is for visual representatio ...

Unable to add padding to <b> tag

Can anyone explain why the padding-top property is not taking effect for the <b> tag? Check out this link <b>hello world</b>​ b { padding-top: 100px; } ​ ...

Using AJAX to Access PHP Variables

I am working on creating a progress bar and have the following CSS code: #result{ background:#8c8f91; margin-left: 15px; margin-right: auto; table-layout: fixed; border-collapse: collapse; z-index: -1; position:relative; width: ...

The TinyMCE extension in Yii fails to load CSS files when accessed through a subdomain

My Yii application located at site.com/module has the tinymce extension installed, but I am facing an issue where the tinymce CSS files are not loading when I access the application through the sub-domain alias module.site.com. Interestingly, all JS file ...

Display movie details in a responsive column layout that condenses into a single column

I'm having trouble displaying movie information on a website due to CSS and HTML issues. My goal is to align the title (Director:, Cast:, etc) with the first item in the list, and have all subsequent items follow below. I initially tried using a defin ...

What steps can I take to resolve the warning about serving images with low resolution in Lighthouse while using Next.js?

I successfully incorporated the svg logo into my webpage using the Next.js Image tag. import Image from "next/Image" export default function Home() { return ( <div> <Image width={32} height={32} src="/logo.svg"> ...

Achieving full wrapping of border around the entire element

Trying to create a link that resembles a button, but facing an issue when the anchor text spans more than one word causing it to wrap onto the next line. This results in the border around the link/button not wrapping around the entire element. It appears a ...

Struggling to ensure buttons on my page respond and resize properly alongside the other elements

Despite making most of this page responsive, I'm having trouble getting the two buttons to scale appropriately. They are utilizing javascript for a mouseover effect, and I suspect that might be causing some issues. Ideally, I would like the images to ...

links contained within a single span inside an anchor tag

I am attempting to place all three spans within an anchor tag, inline. I am not certain what is missing from the code: a.facebook-button { float: none; display: inline-block; margin-bottom: 5px; left: auto; text-shadow: 0 -1px 1px rgba(0, 0, 0 ...

What can I do to prevent my HTML/CSS/jQuery menu text from shifting when the submenu is opened?

After encountering numerous inquiries regarding the creation of disappearing and reappearing menus, I successfully devised a solution that functions seamlessly on desktop and tablet devices. My approach involved utilizing the jQuery .toggleClass function w ...

Transforming HTML into a Console Experience (Angular 16)

I'm experimenting with creating a console/CLI style experience using HTML. The goal is to have the input fixed at the bottom of the window, while commands and output rise up from the bottom and eventually disappear off the top of the screen, enabling ...

When the page loads in the React platform, a section of the footer unexpectedly overlaps with another component

After working on this section some time ago, I returned to find a strange behavior that I couldn't fix by changing the CSS or wrapping components in divs. If you want to see the issue, check out this screenshot: issueReact To view the code where the ...

Leveraging the ng-hide property of one controller to adjust the ng-style attribute of another controller through a centralized global controller

Seeking assistance with accessing the boolean value of ng-hide from one controller in order to alter CSS properties of another controller utilizing a global controller. Here is the jsfiddle link: https://jsfiddle.net/dqmtLxnt/ HTML <div ng-controller= ...