Is there a way to place a logo image at the center of a bootstrap fixed navigation bar?

I have previous experience using bootstrap to create websites, and this time I am working on a site with a fixed navigation bar. My goal is to position the logo in the middle of the navigation links but also have it appear on top of the links.

Here is an image illustrating what I am trying to achieve:

https://i.sstatic.net/CdyTJ.jpg

The current code I have is as follows:

html, body {
  margin: 0;
  padding: 0;
  background: #fff;
  color: #000;
  font-size: 14px;
  font-family: 'Helvetica', serif;
}

.navbar {
  background: none;
  border: none;
  margin-top: 20px;

}

.container-fluid {
  padding-left: 0px;
  padding-right: 0px;
  margin-left: -40px;
}

.navbar ul li {
  display: block;
  text-align: center;
  float: left;
  width: 20%;

}

.fourth-color {
  background-color: #ffecec;
  padding-top: 30px;
  padding-bottom: 30px;
}

.third-color {
  background-color: #122212;
  padding-top: 30px;
  padding-bottom: 30px;
}

.second-color {
  background-color: #aaa;
  padding-top: 30px;
  padding-bottom: 30px;
}

.first-color {
  background-color: #f25727;
  padding-top: 30px;
  padding-bottom: 30px;
}
<!-- Start of Nav Bar -->
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <ul>
      <li class="first-color"><a href="#">First</a></li>
      <li class="second-color"><a href="#">Second</a></li>
      <li><a href="#">Logo</a></li>
      <li class="third-color"><a href="#">Third</a></li>
      <li class="fourth-color"><a href="#">Fourth</a></li>
    </ul>
  </div>
</nav>

When attempting to add an image link, it doesn't align properly and disrupts the entire navigation bar layout. Any assistance would be greatly appreciated!

Answer №1

The issue you are facing is that only the list items with classes have padding. The anchor tag for the logo is not centered because it does not have a class with padding applied.

For example:

.navbar ul li {
  display: block;
  text-align: center;
  float: left;
  width: 20%;
}

.fourth-color, .third-color, .second-color, .first-color {
  padding-top: 30px;
  padding-bottom: 30px;
}

To fix the problem, apply padding to all list items.

.navbar ul li {
  padding-top: 30px;
  padding-bottom: 30px;
}

To center the middle logo, use position: relative and add additional styles using either a class or :nth-child as shown below.

@import url("http://fonts.googleapis.com/css?family=Raleway&subset=latin,latin-ext");

html, body {
  margin: 0;
  padding: 0;
  background: #fff;
  color: #000;
  font-size: 14px;
  font-family: 'Helvetica', serif;
}

.navbar {
  margin-top: 1em;
  text-align: center;
}

.navbar ul {
  padding: 0;
  text-decoration: none;
  list-style: none;
}

.navbar li {
  display: block;
  float: left;
  width: calc(20% - 1px - 0.906752000000002em);
  padding: 1.16em 1em;
  font: 100 21px "Raleway", sans-serif;
  background: #000;
  color: #fff;
  opacity: 1;
  transition: all 0.1s linear;
}

.navbar li:nth-child(1) {
  background: #ff410e;
}

.navbar li:nth-child(2) {
  background: #f4ca00;
}

.navbar li:nth-child(3) {
  background: #fff;
  color: #000;
  border: 1px solid #000;
  border-radius: 2.84em;
  padding: 1.76em 0;
  width: 20%;
  position: relative;
  margin: -0.64em -2.16em;
  z-index: 1;
}
.navbar li:nth-child(3) a {
  color: #000;
}

.navbar li:nth-child(4) {
  background: #99d81b;
}

.navbar li:nth-child(5) {
  background: #00abf3;
}

.navbar li:hover:not(:nth-child(3)) {
  opacity: 0.7;
}

.navbar a {
  color: #fff;
  text-decoration: none;
}
.navbar .active {
  text-decoration: underline;
}
<!-- Start of Nav Bar -->
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <ul>
      <li><a class="active" href="#">First</a></li>
      <li><a href="#">Second</a></li>
      <li><a href="#">Logo</a></li>
      <li><a href="#">Third</a></li>
      <li><a href="#">Fourth</a></li>
    </ul>
  </div>
</nav>

If you do not plan on creating a dropdown menu, consider styling the anchors instead of using a list for the menu bar. You can customize the anchors according to your requirements. Remember to keep your code DRY (Don't Repeat Yourself):

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transform PHP String to Integer in SQL

Below is a simple HTML form that I have created: <form action="" method="post"> Name: <input type="text" name="name" /><br><br> College: <select name = "colleges"> <option> ---Select College---</option> &l ...

Is there a glitch in Safari's CSS involving max-height?

Currently, I am in the process of developing a popup for a registration form. The CSS rules have been configured accordingly and after testing on various browsers, it has come to my attention that Safari is displaying an additional white space between elem ...

Is it possible for the browser to ignore the style.min.css file and only use the style.css file when both files are linked in the HTML?

I have a question about file downloads: If I have two files named Style.css and Style.min.css in the same directory, and my HTML page references Style.css, will the browser or server ever download the Style.min.css file instead? (Even if they have the sa ...

Bootstrap: Utilizing a full width grid system with columns contained within a container

Looking to design a unique layout that spans the full width of the page, featuring a blue half on the left and a red half on the right. Next, I would like to insert text into the layout, but within a container element. Is this doable? UPDATE: Notice that ...

What is causing this css style to fail in Internet Explorer 8?

Check out this JS Fiddle. The code works perfectly in Firefox and Chrome, however when I try it in IE 8, it appears like this: Can anyone help me figure out why it's not compatible with IE 8? Thank you! ...

Utilize JavaScript code with AJAX content without repeated inclusion

My index.php file allows users to input day, month, year, and more. Using ajax, I dynamically display the results. $(document).ready(function() { $('#searchBtn').click(function() { $.ajax({ url: &a ...

Display a text over a full-screen HTML5 video

Is there a way to display text on a fullscreen video in HTML? I have tried using absolute positioning for the text and relative/fixed/none positioning for the video, but it does not work when the video is in fullscreen mode. I also attempted to call the ...

Creating the Perfect Layout: Unveiling the Secrets of Bootstrap 4

Currently in the process of working on a website, I have encountered a particular challenge regarding the layout design using Bootstrap 4. (Highlighted in Red) https://i.sstatic.net/k8bKv.png The issue at hand is that the first column is a col-md-7 and t ...

Updating the Origin of a Sound Element in HTML5

I am currently working on developing a personalized browser-based music application. I am at the initial stage of this project where my main goal is to create a button that allows users to change the song being played by an HTML5 audio player. I have attem ...

Why is Property CSS of Undefined Giving Error?

var random; var squareArray = [$('#square1'),$('#square2'),$('#square3'),$('#square4')]; var randomOrder = []; var i = 0; var j = 0; function computerPlays() { random = Math.floor(Math.random() * 4); randomO ...

Difficulty with animated presentations

As a developer with limited CSS experience, I am trying to create a CSS3 slideshow using only two images. After discovering some interesting code for this purpose, which can be found here, I decided to make a small change to the original code (specifically ...

Benefits of leveraging UI component libraries such as Bootstrap-Vue and React-Bootstrap

What advantages does Bootstrap-Vue offer over traditional Bootstrap? Does it eliminate the need for jQuery dependency? ...

Reduce the size of the header in the Sydney theme for WordPress as you scroll

Currently, I am attempting to reduce the size of the header once scrolling down. My focus is on refining the child theme. Displayed below is a screenshot illustrating the appearance of the header at the top of the page: https://i.stack.imgur.com/wojGf.pn ...

The basic CSS container fails to properly align its elements as intended

Recently, I created a CSS div using Bootstrap and added some custom styling. You can check out the fiddle for a live demo of the code: https://jsfiddle.net/w9gyc0t5/. Here's the actual code: <!-- Bootstrap docs: https://getbootstrap.com/docs -- ...

Is it permissible to utilize the submit button in order to trigger an AJAX function?

I have a dilemma with my HTML form. Currently, it submits data directly to a PHP file. I am interested in updating the code so that when the submit button is clicked, the data is sent to a JavaScript function instead, allowing me to incorporate an AJAX f ...

jQuery is the key to unlocking the potential of Bootstrap 4 plugins. Discover the ingenious ways

When examining the code for registering plugins in jQuery within Bootstrap 4, I am having trouble understanding a few lines: https://github.com/twbs/bootstrap/blob/v4-dev/js/src/tooltip.js 1) Why did the author add the Constructor property to the Tooltip ...

As the window size decreases, adjust the negative left margin to increase dynamically

Is there a way to adjust the margin-left property of mydiv-2 to become increasingly negative as the browser window is scaled down horizontally? #mydiv-1{ background-color:orange; width:60%; height:400px; margin-left: 150px } #mydiv-2{ backgr ...

Pug: exploring the wonders of bootstrap in the node_modules directory

I am currently utilizing pug to generate HTML content which is later converted into PDF format using html-pdf. This is how my controller looks like: // read file var source = fs.readFileSync(path.resolve(__dirname, 'templates/pdf.pug'), &ap ...

Show or conceal a bootstrap spinner using a function

Quandary I am facing an issue with displaying a bootstrap spinner while a function is running. The spinner should be visible during the execution of the function and disappear once it is done. Here is the code for the bootstrap element: <div id="res ...

When utilizing the `express.static(__dirname)` function in Node.js with Express, the visitor may be directed to an incorrect HTML page

My website consists of a login page named login.html and an index page named index.html. I want to implement authentication so that only logged in users can access the index page. I have not set up the post method on the login HTML page. Therefore, I have ...