Bootstrap NavBar with left, center or right aligned items

Cover Image for Bootstrap NavBar with left, center or right aligned items
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Bootstrap NavBar with Left, Center, or Right Aligned Items: A Complete Guide

Are you struggling to create a navigation bar in Bootstrap that has Logo A on the left, menu items in the center, and Logo B on the right? Look no further, as we have a platform-friendly solution just for you! 🚀

The Problem

The current setup you've tried results in Logo A on the left, menu items aligned next to Logo A, and Logo B on the right. Let's dive into the solution to align the elements as desired.

The Solution

To achieve the desired alignment, we'll make a few modifications to your existing code.

  1. We'll wrap the menu items and the Logo B element inside another div, which we'll align to the right using the Bootstrap class navbar-right. This will ensure that Logo B stays on the right side of the navigation bar.

<div class="collapse navbar-collapse">
  <ul class="nav navbar-nav">
    <li class="active"><a href="#">Home</a></li>
    <li><a href="#about">Menu Item 1</a></li>
    <li><a href="#contact">Menu Item 2</a></li>
    <li><a href="#about">Menu Item 3</a></li>
  </ul>
  <ul class="nav navbar-nav navbar-right"> <!-- <--- Add this line -->
    <li><a href="#"><img src="images/Logo_B.png" class="img-responsive"></a></li>
  </ul>
</div>
  1. To center align the menu items along with Logo A, we'll set the parent div of the ul containing the menu items to have the class navbar-center. This class is not provided out-of-the-box by Bootstrap, so we'll need to create it in our custom styles.

Add the following CSS code to create the navbar-center class:

.navbar-center {
  position: absolute;
  width: 100%;
  text-align: center;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}
  1. Finally, let's assign the newly created navbar-center class to the parent div wrapping the ul containing the menu items:

<div class="collapse navbar-collapse navbar-center"> <!-- <--- Add this line -->
  <ul class="nav navbar-nav"> <!-- ... -->
  </ul>
</div>

With these modifications, your navigation bar will have Logo A aligned to the left, menu items centered, and Logo B aligned to the right. 🎉

Engage With Us!

We hope this guide has helped solve your Bootstrap navigation bar alignment issue. If you have any questions or need further assistance, feel free to leave a comment below. We would love to hear from you! 😊

Don't forget to share this post with your friends and colleagues who might find it useful. Happy coding! 👩‍💻👨‍💻


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello