ml-auto is not pushing navbar links to the right
Asked Answered
S

6

32

I'm using reactstrap and have been following this link:

https://reactstrap.github.io/components/navbar/

In the example, the <nav className='ml-auto' navbar> is pushing the <NavItem> to the right. However, what I am trying to implement (which is really similar to the example) the <NavItem> is rendering right next to the <NavbarBrand>.

I've checked the syntax like 100 times and it looks correct. The custom CSS I have, which is very little, does not seem to be overriding anything. The CSS in the console looks pretty similar and it appears to be affected by the:

.ml-auto, .mx-auto {
    margin-left: auto!important;
}

At least toggling it off in the console in the example, moves the <NavItem> right next to the <NavbarBrand> like it is in my app (which I don't want). Here is what I am looking at:

Reactstrap Example (correct spacing):

Correct spacing enter image description here

Console for my app (incorrect spacing): enter image description here enter image description here

How do I get the spacing right in my app?

It really isn't clear to me what is affecting margin-left: auto !important to work in one and not the other.

Stalnaker answered 28/2, 2018 at 5:50 Comment(1)
unwrap the navbar-toggler and .navbar-collapse from div....its look like you are using extra div just after navbar-brand...Spawn
N
10

The navbar components (and many other components) will only work if you use them the way they've been designed to be used.

For example, ml-auto works on sibling elements.

It does NOT work if you destroy that sibling relationship.

In other words, you cannot just randomly wrap elements in unnecessary divs.

Remove the div you put around the navbar-toggler and the collapse component to make the sibling selector work as intended.

Also: Do NOT use the !important flag as a permanent solution in any of your custom css. That flag is for quick testing only.

P.S. You seem to have a habit of wrapping things in unnecessary divs. The container is also wrapped in such a useless div. Don't add unnecessary code for some sort of "esthetics". Only add code if it actually does something and use comments for everything else.

Nuthouse answered 28/2, 2018 at 7:0 Comment(1)
!important is added by Bootstrap. They did not make those CSS rules.Overseer
R
166

For Bootstrap-5 we have to use ms-auto instead of ml-auto

Romanticist answered 4/1, 2021 at 11:50 Comment(3)
THIS DESERVES MORE CREDIT SERIOUSLY. Under react's bootstrap page, there is NOOOO mention whatsoever of ms-auto. ml-auto was all it gave me as well as every other single post here. damn dude you're a legend.Vasques
This worked for me. I'm using react-bootstrap and installed bootstrap using yarn add bootstrap which installed the latest v5.0. Reinstalled bootstrap (yarn add [email protected]) and is working fine now.Leitman
should be the accepted answer if using bootstrap 5Encrimson
P
31

Starting from Bootstrap 5 2021, There are some changes in the side names of spacing utilities.

  • Margin Left is now called Margin Start, therefore use ms instead of ml

  • Example: For setting margin-left to auto : use ms-auto

  • Similarly for setting margin left to 3 : use ms-3 instead of ml-3

You can learn more about the new side names for margins and paddings in the official bootstrap documentation BootStrap Spacing Utilities

Procaine answered 20/5, 2021 at 8:39 Comment(0)
N
10

The navbar components (and many other components) will only work if you use them the way they've been designed to be used.

For example, ml-auto works on sibling elements.

It does NOT work if you destroy that sibling relationship.

In other words, you cannot just randomly wrap elements in unnecessary divs.

Remove the div you put around the navbar-toggler and the collapse component to make the sibling selector work as intended.

Also: Do NOT use the !important flag as a permanent solution in any of your custom css. That flag is for quick testing only.

P.S. You seem to have a habit of wrapping things in unnecessary divs. The container is also wrapped in such a useless div. Don't add unnecessary code for some sort of "esthetics". Only add code if it actually does something and use comments for everything else.

Nuthouse answered 28/2, 2018 at 7:0 Comment(1)
!important is added by Bootstrap. They did not make those CSS rules.Overseer
C
9

Explanation on how ml-auto works.

It appears this was already resolved in the comments. I just wanted to add an explanation that ml-auto was not working because it's flexbox parent needs to be full-width of the navbar. So, it could have also been solved by adding w-100 to the extra div..

<nav class="navbar navbar-expand-md navbar-light bg-faded">
     <a class="navbar-brand" href="#">reactstrap</a>
     <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="navbar-toggler-icon"></span>
     </button>
     <div class="w-100">
        <div class="navbar-collapse collapse">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="#">Link</a>
                </li>
                ...
            </ul>
        </div>
     </div>
 </nav>

ml-auto works because of the way the flexbox parent "shrinks" the child block element(s) by default. It has nothing to do with sibling elements, and would work fine with just one child in the flexbox parent: https://www.codeply.com/go/xPaoNJMzbG

Here's a good article on how auto-margins work with flexbox

Cytaster answered 28/2, 2018 at 11:58 Comment(1)
I tried added w-100 to the parent element (row in my case) and it made no difference. When I used ml-auto the buttons still aren't right aligning. However, adding the class text-right worked and the buttons right align properly.Basswood
K
4

In Bootstrap-5 there are some changes, earlier it was ml-auto for margin-left:auto which is now changed to ms-auto, similarly mr-auto is changed to me-auto. However for top and bottom it is same as earlier versions of bootstrap i.e versions before bootstrap-5

for more details visit docs

Kislev answered 20/2, 2021 at 7:35 Comment(1)
Yes, this answer is correct if you are using Bootstrap version 5Rebak
T
4

I am using [email protected] If you are stuck with this problem in 2021, It is because there has been migration and changes in the class name.

.ml-* and .mr-* to .ms-* and .me-*.

similarly many others class name has been changed.

Link to check migration

Bootstrap Migration link

Tomb answered 7/5, 2021 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.