DEV Community

Kishokanth
Kishokanth

Posted on

3 2

Positioning nav-items in navbar with Flexbox

Hello all :)

This is my first official blog post here as an aspiring front-end dev.

Today's topic is on how to position any number of nav-items in a Bootstrap navbar (within the ul) using Flexbox which is already enabled in Boostrap.

Let's start with the code required for the full navbar first.

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Pricing</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
        </li>
      </ul>
    </div>
  </nav>
}

So the basic navbar I have looks like this

Alt Text

If you need a navbar with more features (a search bar perhaps?) check the Boostrap docs out here.

Centering the nav-items

Alt Text

To center the nav-items as you see above all you have to is enter "justify-content-center" into your code with the id="navbarNav". See below for actual code.

<div class="collapse navbar-collapse justify-content-center" id="navbarNav">

Right-aligning the nav-items

Alt Text

Again all you have to do is "justify-content-center" to "justify-content-end".

And that's it!

BONUS- when the items are in the center you can space them apart by playing with the margin property in bootstrap. See what I did to get the below.

By inserting mr-4 (margin-right-4) into each nav-item class we can make our nav items get more space for each other making our navbar look like

Alt Text

to this

Alt Text

That's it folks! I dream of becoming a great front-end dev soon and hope that this little piece of information can help you as it helped me :)

Image of Docusign

πŸ› οΈ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay