Search code examples
csstabsautomarginsbootstrap-5

Auto margins with tabs in bootstrap 5


I'm learning to use bootstrap 5, so bear with me if this question seems a bit basic!

I'm toying around with tabs to generate tabbable panes of local content. I've used the code provided in the documentation here : https://getbootstrap.com/docs/5.0/components/navs-tabs/#javascript-behavior

<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item" role="presentation">
    <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true">Home</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false">Profile</button>
  </li>
  <li class="nav-item" role="presentation">
    <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
  <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">...</div>
</div>

And it works perfectly. It responds well also to justify-content, so the tabs are easy to center or distribute. Exactly what I am looking for.

But, I can't make them split with auto margins (mr-auto and ml-auto) so that, for example, the first two tabs will be aligned on the left and the last one on the right. When I include them in the ul tag, it is just ignored (while the justify-content work just fine)

I'm looking to do something like in the flex documentation : https://getbootstrap.com/docs/4.0/utilities/flex/#auto-margins or in Wikipedia where the article and talk tabs are on the left, and the read, edit and history are on the right. How could I do that?


Solution

  • Change your last li like :

    <li class="nav-item ms-auto" role="presentation">
        <button class="nav-link" id="contact-tab" data-bs-toggle="tab" data-bs-target="#contact" type="button" role="tab" aria-controls="contact" aria-selected="false">Contact</button>
      </li>
    

    In Bootstrap 5,

    • t - for classes that set margin-top or padding-top
    • b - for classes that set margin-bottom or padding-bottom
    • s - for classes that set margin-left or padding-left in LTR, margin-right or padding-right in RTL
    • e - for classes that set margin-right or padding-right in LTR, margin-left or padding-* left in RTL
    • x - for classes that set both *-left and *-right
    • y - for classes that set both *-top and *-bottom
    • blank - for classes that set a margin or padding on all 4 sides of the element

    For More Info : here