How to perfectly align the toggle button using Bootstrap 3

I have implemented the default nav-bar from Bootstrap 3 in my WordPress theme, with some modifications renaming it '.navbar-custom'.

On mobile-sized screens, the default 'toggle' appears in the right corner, while the '.navbar-brand' is on the left.

My goal is to center the 'toggle' in mobile mode and also center the '.navbar-brand' (it would be ideal if the toggle could go underneath the site logo).

I have tried various examples in jsfiddle, but none seem to work for me. The only one that showed any visual change was the following CSS snippet within a media query:

.navbar-toggle {
width: 100%;
float: none;
margin: 0 auto;
}

However, this code simply moved the toggle to the left of the nav-bar. Here is my typical navbar code:

<header>    
        <nav class="navbar navbar-custom row" role="navigation">   
            <!-- Brand and toggle get grouped for better mobile display --> 
                <div class="navbar-header"> 
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> 
                        <span class="sr-only">Toggle navigation</span> 
                        <span class="icon-bar"></span> 
                        <span class="icon-bar"></span> 
                        <span class="icon-bar"></span> 
                    </button> 

                <div class="navbar-brand">
                    <?php if (get_theme_mod(FT_scope::tool()->optionsName . '_logo', '') != '') { ?>
                        <a class="mylogo" rel="home" href="<?php bloginfo('siteurl');?>/" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>"><img relWidth="<?php echo intval(get_theme_mod(FT_scope::tool()->optionsName . '_maxWidth', 0)); ?>"  relHeight="<?php echo intval(get_theme_mod(FT_scope::tool()->optionsName . '_maxHeight', 0)); ?>" id="ft_logo" src="<?php echo get_theme_mod(FT_scope::tool()->optionsName . '_logo', ''); ?>" alt="" /></a>
                    <?php } else { ?>
                        <h1 class="site-title logo"><a id="blogname" rel="home" href="<?php bloginfo('siteurl');?>/" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>"><?php bloginfo( 'name' ); ?></a></h1>
                    <?php } ?>
                    </div>      
                </div> 

            <!-- Collect the nav links, forms, and other content for toggling --> 
                <div class="collapse navbar-collapse navbar-ex1-collapse navbar-right"> 
                    <?php /* Primary navigation */
                        wp_nav_menu( array(
                        'menu' => 'top_menu',
                        'depth' => 2,
                        'container' => false,
                        'menu_class' => 'nav navbar-nav',
                        //Process nav menu using our custom nav walker
                        'walker' => new wp_bootstrap_navwalker())
                        );
                    ?>
                </div>
        </nav>

     <div class="row"> 
         <div class="col-md-12 col-xs-12">
            <h1>content</h1>
         </div>
     </div>

   </header>

Answer №1

Make sure to enclose your button in another div with the class text-center

<div class="text-center">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse"> 
       <span class="sr-only">Toggle navigation</span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
       <span class="icon-bar"></span> 
    </button> 
</div>

UPDATE :

It is recommended to review this CSS code snippet below

.navbar-toggle {
  width: 100%;
  float: none;
  margin-right: 0;
}

VIEW DEMO

Answer №2

Choose between:

.header-nav { text-align: center; }

or:

.nav-toggle {float: none; margin-right: 0; width: 100%; }

.icon-menu { margin-left: auto; margin-right: auto; }

Answer №3

Keep the design consistent by maintaining a fixed width instead of making it 100% wide, and position the toggle in the center

.navbar-toggle {
    float: unset; 
    margin-right: 0; 
}

.navbar-header { 
    text-align: center; 
}

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

Extract information from a database table for presentation as simple text

I am looking to extract information from each row and display it as plain text on the same page within a paragraph. Here is an example table for reference: <table> <thead> <tr> <th class="a header">A</th ...

Utilizing the module pattern for efficient event handler implementation

My concern revolves around optimizing the utilization of JavaScript Module Patterns. Here is a basic module snippet from my code that I aim to break down into modules. Each module would consist of a few hundred lines of code. Inquiry : Among methods 1, 2, ...

Exploring Laravel: A Guide to Sending JSON Data from Controller to View Using Ajax(GET)

I'm encountering a challenge at the moment. I've been attempting to pass a specific date to the controller (in order to create a where clause and send it back to the view), but it seems unnecessary to use a controller for this purpose since a whe ...

Managing information retrieved from an asynchronous HTTP request

Here is the script I am using to load an HTML page generated by a node.js server: (function nodeLoader(){ $.ajax({ url: "http://oclock.dyndns.org:8000", method: "get", data: {hk: hk }, success: f ...

Is it appropriate to include JavaScript and CSS in views?

In my ASP.NET MVC project, I have set up a _Layout, a controller, and several views. Certain pieces of code are clearly global in nature, such as the CSS included in the _Layout file and other styles that are consistent throughout the site. However, when ...

python code to locate element using selenium

Currently, I am utilizing selenium in conjunction with Python to automate tasks. I am facing an issue where I have a checkbox element that I need to click on, but the only information I have about it is the text it contains. The text is located within a &l ...

Even with the no-pause feature enabled, the Angular Bootstrap Carousel still stops when you hover over it

I have incorporated Bootstrap's carousel into my project, with the controller set up as follows: .controller('HomeController', function () { var self = this; self.interval = 2000; self.designer = [{ image: '/assets ...

When you drag down on mobile Safari on an iPad, touch events may cease to fire in HTML5

When I implement event listeners to handle touch events like touchmove and touchstart document.addEventListener("touchstart", function(event){ event.preventDefault(); document.getElementById("fpsCounter").innerHTML = "Touch ...

What is the method for setting tabstops in XHTML?

Is there a way to create a tabstop in an xhtml webpage without using the "&nbsp;" method? I want to avoid putting a space between the ampersand and 'n'. Any suggestions? Edit: My initial post did not display the &nbsp;. I am looking for ...

Trouble with setting the color attribute using setProperty in a GXT text area

Help needed in dynamically changing the font color of a text area from the color menu. Existing attempts to change it have not been successful. Can someone provide assistance? final ColorMenu fontColorMenu = new ColorMenu(); fontColorMenu.getPalette().a ...

Spinning text within a responsive bootstrap card

I have a card containing text that is not wrapping properly within the card due to rotation using CSS. <div class="card border-info"> <div id="heading_card" class="card-header"> a very long string of text which is not g ...

Tips on Importing a Javascript Module from an external javascript file into an <script> tag within an HTML file

I'm facing an issue while attempting to import the 'JSZip' module from an external node package called JSZip in my HTML File. The usual method of importing it directly using the import command is not working: <script> import ...

The mouseover animation doesn't reset to its original position during quick movements, but instead continues to move without interruption

Greetings! Welcome to my live page. Currently, I am facing an issue with the sliding animation on the header. Specifically, the small gray slide-up divs at the bottom of the sliding pictures are not behaving as expected. They are meant to slide up on mous ...

The jQuery code I'm using is incorrectly selecting all elements with the class name "a2" when I click on them, instead of providing me with

I was hoping to see one a2 element displayed at a time when clicking on a div element, but instead, all a2 elements are appearing simultaneously. I would like to see only one a2 class element at a time upon clicking. <!DOCTYPE html> <html> < ...

Retrieving JSON Information in HTML using Angular 4

Is it possible to retrieve specific data from a JSON file in my HTML using Angular 4's MatCellDef? Specifically, I am interested in accessing the FROM, TO, PERCENT, and SUBTRACT values of the RateBands Array. JSON Data Sample: [ { "year ...

Inheriting Django templates for a unique CSS class markup approach

I want to create a master.html file for inheritance, but I'm facing an issue where the code is repetitive in three different places except for the body class. Here's my current master.html: <html> <head>...</head> <body& ...

Having trouble sending the information to Parse.com using the website

I am a beginner with the Parse database and I am currently working on implementing a code that allows users to sign up, with their information stored in the Parse database. However, I am encountering an issue where the values are not uploading as expected. ...

Utilize Jquery to extract the functions from a PHP file

I'm a beginner with both jQuery and PHP. Currently, I have an HTML page where I need to utilize functions from a PHP file using jQuery, and then display the results in the HTML. My goal is to count the number of files in three different directories a ...

Enclose each set of objects, such as text, within a separate div, except for div elements

Is there a way to wrap each immediate group of objects that are not contained in a div with a wrap class? Input: var $code = 'Lorem Ipsum <p>Foo Bar</p> <div class="myclass"></div> <p>Foo Bar</p> <div clas ...

React Bootstrap encountered rendering issues

Recently, I decided to delve into the world of React Bootstrap and started my learning journey. To get started, I followed some tutorials on <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <meta name="vi ...