Aligning the toggle menu in the center when it is collapsed

Hello everyone, I am new to Bootstrap 4 and struggling to figure out how to center the toggle menu on mobile devices. Can anyone provide some guidance or solutions on how to achieve this?

Here's an example of what I'm aiming for.

<body>
    <div class="container-fluid">
        <nav class="navbar-expand-lg navbar navbar-light">
            <button aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-target="#navbarNav" data-toggle="collapse" type="button"><span class="navbar-toggler-icon"></span>Menu</button>

            <div class="collapse navbar-collapse justify-content-center" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link" href="about.html">ABOUT</a>
                    </li>




                    <li class="nav-item">
                        <a class="nav-link" href="Contact.html">CONTACT</a>
                    </li>


                    <li class="nav-item active">
                        <a class="btn btn-outline-primary" href="https://spartan-safety-limited.gogecko.com/users/login">TRADE ACCOUNT</a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>
    <!--CAROUSEL-->
</body>

CSS

.navbar-toggler {

   font-family: 'Open Sans', sans-serif;
    padding: .25rem .75rem;
    font-size: 1.25rem;
    line-height: 1;
    background-color: #FF6600 ;
    border: 1px solid transparent;
    border-radius: 0;
    margin-right: 50px;
    text-align: center;
}


.navbar-light .navbar-toggler{
  color: #ffffff;
}

Answer №1

To align the navbar centrally on mobile, similar to how it appears on the referenced website, you will need to apply the following CSS properties to the .navbar and .navbar-toggler classes: display: block, margin-left: 0, and margin-right: 0. These changes should be made outside of any media queries. Within the media query, reset these styles to fit the full page view.

By implementing these adjustments, the .navbar will be centered on the page, and the .navbar-toggler button will be positioned inside the .navbar element.

.navbar-toggler {
  font-family: 'Open Sans', sans-serif;
  padding: .25rem .75rem;
  font-size: 1.25rem;
  line-height: 1;
  background-color: #FF6600;
  border: 1px solid transparent;
  border-radius: 0;
  margin-right: 50px;
  text-align: center;
}

.navbar-light .navbar-toggler {
  color: #ffffff;
}


/* New styles */

.navbar {
  margin-left: auto;
  margin-right: auto;
}

.navbar-toggler {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

.navbar-nav {
  list-style-type: none;
  padding: 0;
}

@media (min-width: 768px) {
  .navbar {
    margin-left: 0;
    margin-right: auto;
  }
  .navbar-toggler {
    display: inline-block;
    margin-left: 0;
    margin-right: auto;
  }
}
<div class="container-fluid">
  <nav class="navbar-expand-lg navbar navbar-light">
    <button aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-target="#navbarNav" data-toggle="collapse" type="button"><span class="navbar-toggler-icon"></span>Menu</button>

    <div class="collapse navbar-collapse justify-content-center" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="about.html">ABOUT</a>
        </li>


        <li class="nav-item">
          <a class="nav-link" href="Contact.html">CONTACT</a>
        </li>


        <li class="nav-item active">
          <a class="btn btn-outline-primary" href="https://spartan-safety-limited.gogecko.com/users/login">TRADE ACCOUNT</a>
        </li>
      </ul>
    </div>
  </nav>
</div>
<!--CAROUSEL-->

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

Changing color of entire SVG image: a step-by-step guide

Check out this SVG image I found: https://jsfiddle.net/hey0qvgk/3/ <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" width="90" height="9 ...

Tips for adjusting the height of a Safari extension during runtime

After successfully developing a Safari extension, I am facing an issue with adjusting the width and height of the popover dynamically at runtime. Can anyone provide insights on how to modify the dimensions of the popover based on its content? ...

Personalized Firefox Scrollbar - Rounded Corners

I have successfully customized the default CSS of browser scrollbars for Chrome and Edge, but I am facing issues with Firefox. Is there a way to sync the scrollbar styling in Firefox with Chrome and Edge? Currently, I am unable to apply border radius to th ...

Formatting Dates and Times in a Bootstrap Form

I'm working on a form that includes date and time type inputs. The fields are set up for mm/dd/yyyy date format and 12-hour AM/PM notation for time. However, I prefer to use dd/mm/yyyy date format and 24-hour notation for time. Is there a way to make ...

What's the reason behind the absence of applied bootstrap style in jsFiddle?

Link to the code: Click here I'm having trouble understanding why the default style of <ol> from Bootstrap is not being applied in this particular code. Any insights? Here's a snippet of the HTML code taken from the fiddle. <ul> ...

Encountering an undefined variable error with Ruby on Rails 6 Bootstrap after restarting the server

I'm facing an issue in rails 6 where upon server restart, I encounter an Error: Undefined variable. Oddly enough, if I comment out the SCSS variables in my files, refresh the page, then un-comment them and refresh again, everything works as expected a ...

The visibility of buttons can be controlled based on the selected radio option

I have just completed setting up 4 buttons (add, edit, delete, cancel) and a table that displays data received via ajax. Each row in the table contains a radio button identified by the name "myRadio". When a radio button is clicked, I need the buttons to ...

Web fonts are functioning properly only on Chrome and Safari for Macintosh operating systems

A web designer provided me with some fonts to use on a website. Below is the content of my fonts.css file: /* Font Awesome */ /* Font Awesome Bold */ @font-face{ font-family: 'font-awesome'; src: url('fonts/font-awesome-bold-webf ...

Select the five previous siblings in reverse order using jQuery's slice method

I have a unique approach to displaying a series of divs where only 5 are shown at a time using the slice() method. To navigate through the items, I include an ellipsis (...) link after every 4th item, which allows users to easily move on to the next set of ...

the neighboring div sliding to the left causing the div not to expand with its content

I am trying to create a website similar to this one. When I click on the arrow, the left div collapses and the right one expands. However, the content of the right div does not expand as expected! Below is my HTML code: <div class="tools-bg"> & ...

CSS Selector for when a radio button is selected

I'm currently using images in place of radio buttons: <label class="radio-inline thumbs"><input type="radio" id="type_nee" name="type" value="nee" onkeyup="validate()" onclick=" ...

How can I use a CSS file in PHP to conceal the folder structure?

main.css $theme->assign('css_file',SITE_URL.'styles.php?theme='.THEME_ID); $theme->display(TEMPLATES_PATH.'header.tpl'); header.tpl <link rel="stylesheet" href="{$css_file}"> styles.php include TEMPLATES_PATH. ...

Unusual issue with horizontal menu display in Firefox on Mac

I recently completed a website project using Contao for a client (visit www.medivas.de). Everything is working perfectly except for one issue: the last menu item in the main navigation (located at the top, above the slideshow) is causing a line break. This ...

Underline Rows in CSS

When designing my website, I decided to organize the content into rows using CSS instead of tables. To create a line separating each row, I used height:px; for each row manually. However, this method resulted in the line jumping around inconsistently acr ...

Ways to showcase a collection of divs in a dual-column layout while minimizing any unnecessary vertical spacing

Is there a way to create two columns without changing the HTML structure? The content within each child element is dynamic, so there needs to be dynamic vertical spacing as well. I'm experimenting with different CSS properties for both the parent and ...

Textfield with autocomplete dropdown

I have a text field on my website that needs to work as an autocomplete box. I have included the following code in the "headerbar" section of my page: Find :<input type="text" class="input2" style="margin-bottom:0px;" id="customersearchfield"> < ...

Challenges with jQuery Image Sliders

Currently, I am learning from a tutorial on creating a custom jQuery content slider and you can find the tutorial here. Everything is working smoothly in terms of creating the image slider. However, I am facing an issue where I want to have the 'Next ...

Div element obstructing the ability to view markers

Issue with a positioned DIV over Google Maps: The DIV is causing accessibility problems for the map, taking up the entire width when it should stop at the green square borders. Interaction with the map is limited to the very bottom after the green square. ...

What is the best way to eliminate the appearance of the blue square that shows up when the button is clicked on smaller screens?

When testing my project on different screen sizes in Chrome dev tools or on my phone, I noticed a blue square briefly appearing around the button when it is clicked. I attempted to remove this by setting outline: none or outline: 0 on various pseudo-classe ...

Can you restrict the selection of text to only the current element?

<div class="container"> <div class="item1">text text text in div element 1</div> <div class="item2">text text text in div element 2</div> </div> Is there a way using HTML nodes, CSS, or JavaScript to restrict the select ...