I'm working on setting up a navbar with three elements. I want to have a left-aligned brand, a group of right-aligned links that will collapse on smaller screens, and an additional button that stays visible at all times.
To achieve the alignment of the button and links, I used the ml-auto class. This worked well on mobile devices (see 1st image), but on desktops, the button ends up above the links (as shown in the 2nd image). I attempted to use the btn-group class, but the button ended up merging with the links, which didn't look right (3rd image).
https://i.sstatic.net/cgun9.jpg
How can I ensure proper horizontal alignment of these elements?
I should mention that I do not want the button to be grouped with the other right links. I want it to remain visible even on small screens. While I've come across similar questions on SO, they all involve placing the right-aligned items in a collapsible group, which doesn't suit my needs.
You can test the code below using the w3school TryIt editor
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<a class="navbar-brand" href="#">Home</a>
<div class="ml-auto">
<button class="btn btn-success">My Button</button>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto"/>
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 1</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Right Link 2</a>
</li>
</ul>
</div>
</nav>