When the browser window decreases in size, the logo in the Bootstrap NavBar causes the hamburger menu to shift

Today I was experimenting with a Bootstrap NavBar and noticed an interesting behavior. When the logo (navbar-brand) is text-only, the hamburger icon stays on the right side. However, when the logo is an image, the hamburger shifts to the left when resizing the browser. It's puzzling me and I can't figure out why.

I am currently enrolled in Angela Yu's Udemy course and have just completed the Bootstrap module. Now, I am attempting to apply what I've learned to create my own website.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />
<section id="title">
  <div class="container">
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <a class="navbar-brand" href="#">
        <img src="img/techsnazzy-logo.png" width="30%" height="30%" alt="TechSnazzy Logo" />
      </a>
      <!-- <a class="navbar-brand" href="#">Navbar</a> -->
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

      <div class="collapse navbar-collapse id=" navbarSupportedContent>
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">Home</li>
        </ul>
      </div>
    </nav>
  </div>
</section>

view image description here

Answer №1

To ensure that your navbar layout does not wrap, apply the flex-nowrap bootstrap class. You can find more detailed information in the documentation here.

According to w3schools:

The flex-wrap property determines whether flexible items wrap or remain on a single line.

It is recommended to avoid setting width and height directly on the logo image as this can create unnecessary space between the logo and nav-items. Instead, use width: 30% on the .navbar-brand class and set width: 100% for the logo image itself.

I have made some adjustments to your code. Please review:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" />

<section id="title">
  <div class="container">
    <nav class="navbar navbar-expand-lg navbar-light bg-light flex-nowrap">
      <a class="navbar-brand" href="#" style="width: 30%;">
        <img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a" alt="TechSnazzy Logo" class="w-100" />
      </a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

      <div class="collapse navbar-collapse id=" navbarSupportedContent>
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">Home</li>
        </ul>
      </div>
    </nav>
  </div>
</section>

Answer №2

The toggle button is automatically shifting to the next line depending on the width of the element .navbar-brand, which is the expected behavior causing it to align to the left. To keep the toggler always right-aligned, you can use the class ml-auto.

<button class="navbar-toggler ml-auto" ...

For more information, visit https://getbootstrap.com/docs/4.2/utilities/flex/#auto-margins

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

In what ways can a distant ancestor influence its subsequent generations down the line?

As a beginner utilizing Bootstrap v5, I am in the process of creating a navbar using the following HTML code. Within this code, the parent element (ul) of my link items contains a class called "navbar-nav" which results in my link items being styled to s ...

merge various CSS styles into a single one

My div has a mix of styles applied to it. <div class="alert alert-secondary login-alert" role="alert">Please contact the administrator to receive credentials!</div> The styles alert and alert-secondary are default styles from bootstrap 4, whi ...

The art of expertly placing the arrow in <details> with CSS

I'm looking to adjust the placement of the arrow within details. I've experimented with using float:left, but when the line is too long, like in the example provided, the arrow shifts to the beginning of the line below when I resize the window. I ...

Display a div every 3 seconds with the help of jQuery

I am seeking a solution to display the second div (box2) every 3 seconds. Can anyone help me achieve this using jQuery? <div id="box1" style="background-color:#0000FF"> <h3>This is a heading in a div element</h3> <p>This ...

Issue with empty attribute selector not functional in Edge browser

In my current version of Edge, I am facing an issue where the empty attribute selector is not functioning correctly. You can find more information about this problem here. The selector I am using is: [ct-validation]:not([ct-validation=""]) { border-col ...

Font modification is unsuccessful in Vue-Material

Here is a snippet from my App.vue file where I encountered an issue with changing the font in the md-tabs component, taken from . The CSS line: background: #42A5F5; works perfectly fine, but this one does not: color: #E3F2FD; App.vue: <template> ...

Align list items in the center horizontally regardless of the width

I have created this container element: export const Container = styled.section<ContainerProps>` display: flex; justify-content: center; `; The current setup is vertically centering three list items within the container. However, I am now looking ...

Adapting the top margin dynamically for different screen sizes

I am attempting to style an HTML DIV so that the margin-top is adjusted responsively based on the height of the div: jsfiddle Within the wrapper, there is another div that is initially set to display: none, but this may change if the user enters an incorr ...

Can you please inform me of the latest class for labeling controls in Bootstrap 4?

Working on an ASP.NET web app in Visual Studio 2013 with .Net 4.5. According to the Bootstrap 4 migration document: The .control-label class has been renamed to .form-control-label. However, I am unable to locate this new class in any of the Bootstrap ...

Adjust the cursor style using Javascript

I am currently working on a paint software program and I have a feature where pressing the ctrl key while moving the mouse turns on the eraser. However, when the key is pressed, I want the cursor (currently a crosshair) to change to none. I have tried impl ...

What is preventing me from merging font sub properties?

When working with CSS, you have the option to combine sub-properties. For example, instead of defining a border like this: border-style: solid; border-width: 1px; border-color: #555; You can use a single declaration for the border property as a shorthand ...

verifying the user's screen dimensions

I'm currently exploring ways to customize the appearance of my website based on the viewer's screen resolution. I am interested in potentially optimizing the layout for vertical screens on mobile devices, and horizontal screens for laptops. Addit ...

What is the process for converting inline SVG images on the server?

Looking to use inline SVG as a background-image for another element? Check out this example that currently only works in Chrome. If you need an alternative method, take a look at the simpler inline SVG from the RaphaelJS demo here. <svg height="480" ve ...

Achieving vertical alignment of text next to an image using a skeleton grid

Does anyone know how to align the images vertically with the text on the "Board of Advisors" tab on THIS PAGE? Here is the shortened HTML code: <div class="four columns alpha"><img class="ImageBorder" src="LINK" alt="" /></div> <div ...

Hide the .prev button on the jQuery slider when it is on the first

Is there a way to hide the .prev button when it is the first one? Also, why does the slider go back to the first slide when the .prev button is clicked at the last slide? How can this issue be resolved? var nextPane = function (e) { e &&am ...

Problem with the padding on the right side

I'm currently working on implementing responsive design for a website. My main challenge right now is ensuring that the navigation menu stretches to the total width without any padding issues on the last element ("Equipo"). I want this element to reac ...

What's the best way to position a div within a <form> at the bottom right corner of a modal dialog window?

I have a unique scenario that I need help with. I am currently working on a responsive chat application and I am creating a sign up form within a modal dialog using HTML, CSS, and JavaScript. Within the modal, I have a header with an <h1> heading and ...

How can I resolve the recurring multiValueDictKey Error in my Django/Bootstrap project?

Below is the login form code: <form class="user" action="/returningAgent/" method="post" > <div class="form-group"> <input type="email" class="form-control form-control-user" id= "InputEmail" name="InputEma ...

What is the best way to determine the location of just one element?

I am currently working on a rating application where only the selected circle should remain green, not all of them. $('#rating-container').click(function () { var element = $('#target'); var container = $('#rating-containe ...

Is it possible to increase the font size of the text on my MUI Slider's thumb for customization

Currently, I am utilizing the MUI-Slider component within React to showcase a value. My objective is to personalize the 'thumb' and valueLabel to boast a larger font size and thumb appearance. Despite referencing MUI's documentation on CSS c ...