Positioning Image in the Center of Navigation Bar

This particular code snippet is very useful for centering a Navigation Bar that consists of text both horizontally and vertically.

However, the issue arises when attempting to use an image, such as a house icon for the Home button. This causes the navigation bar to become offset and no longer centered in the middle vertically.

.navt {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: center;
  background-color: black;
}
.navt li {
  display: inline;
}
.navt a {
  display: inline-block;
  padding: 12px;
  font-weight: normal;
  color: white;
}
.navt li a:hover {
  color: white;
  background-color: pink;
}
<ul class="navt">
  <li>
    <a href="_index.cfm">
      <img src="../images/hm_wht_tr30.png" height="20" border="0">
    </a>
  </li>
  <li><a href="page.cfm">This</a>
  </li>
  <li><a href="page.cfm">And</a>
  </li>
  <li><a href="page.cfm">That</a>
  </li>
  <li><a href="page.cfm">Here</a>
  </li>
  <li><a href="page.cfm">There</a>
  </li>
</ul>

Answer №1

By adding the code

.navt img { vertical-align: middle; }
, you can ensure that the image aligns vertically in relation to the text.

.navt {
  list-style: none;
  margin: 0;
  padding: 0;
  text-align: center;
  background-color: black;
}
.navt li {
  display: inline;
}
.navt a {
  display: inline-block;
  padding: 0 12px;
  font-weight: normal;
  color: white;
  height: 44px;
  line-height: 44px;
}
.navt li a:hover {
  color: white;
  background-color: pink;
}
.navt img {
  vertical-align: middle;
}
<ul class="navt">
  <li>
    <a href="_index.cfm">
      <img src="../images/hm_wht_tr30.png" height="20" border="0">
    </a>
  </li>
  <li><a href="page.cfm">This</a>
  </li>
  <li><a href="page.cfm">And</a>
  </li>
  <li><a href="page.cfm">That</a>
  </li>
  <li><a href="page.cfm">Here</a>
  </li>
  <li><a href="page.cfm">There</a>
  </li>
</ul>

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

Can JQuery be used to specifically target attributes with vendor prefixes?

Is there a way to change the Thumb color for my slider without needing to select the thumb with a vendor prefix? I want to be able to dynamically pick the color without simply appending a class. $('.button').on('click', function (e) { ...

What is the best way to evenly space divs apart so that they perfectly align with the width of the screen?

How can I make three divs fit the screen without expanding, with each one touching a different edge of the screen? The first should touch the left edge, the second in the middle and the third on the right edge. Check out this code snippet for reference ...

Understanding the functionality of sessions in CodeIgniter when using AJAX

I am experiencing an issue with the Session in my code. It only works when the page is refreshed. I have a controller called ravi and the view named myview. Here is the code snippet: <?php class Ravi extends CI_Controller { public funct ...

What is causing the issue with this pseudo class selector not functioning properly?

I've been experimenting with the :after selector to create an inside-border effect in every div I hover over. However, I'm facing an issue where the ":after" pseudo-class doesn't seem to work. Any insights on why this selector isn't fun ...

Step-by-step guide on resolving the error: TypeError - Unable to access the 'trim' property of an object that is

I encountered an issue while running my program. How can I resolve it? HTML <div class="form-group row"> <label class="col-md-2 col-form-label" for="subcompanyname">Division *</label> <div class="col-md-3"> <input cla ...

I am experiencing difficulties with my jQuery animation function as it does not seem to be functioning properly despite successful

Can someone knowledgeable in jQuery and JavaScript please help me understand why this code isn't functioning as expected? The goal is for the image with the id "previmage" to expand and then shrink when the changeclass function is triggered. However ...

Upon clicking on another div, command it to expand to full height and width

I am encountering an issue when loading an HTML file into a content div after clicking on a menu bar option. The HTML file contains 5 tables, but the div only displays the first 5 lines of the first table before requiring a scroll bar to view the rest of t ...

What is the best way to adjust the position of a dropdown menu in Material UI?

I am currently working on a Gatsby application and I'm trying to achieve a dropdown menu effect when the Menu icon is clicked, with the menu appearing right under the header and expanding to 100% width. Below is the code snippet that I have been using ...

Having difficulty uploading files to my website [PHP]

I'm having trouble with a PHP tutorial I followed for uploading an image. Despite my best efforts, the upload always fails and I can't figure out why. I've rewritten the code multiple times but the problem persists. The goal of this code is ...

Remove a group of rows by using checkboxes in a MySQL table with PHP

I'm having trouble with deleting selected rows in my table using checkboxes. Even though I select items, nothing gets deleted. Here is the code I have tried: <?php $con=mysqli_connect("localhost","root","","annualdb"); // Check connection if (my ...

Guide to selecting multiple rows at once using ng-options in a list

Need assistance with an HTML list: I have a box displaying a list where users can select multiple items to save. The saving and retrieving process is functional as the selectedList stores the user's previous selections, while fullList contains all av ...

Display the item exactly where the cursor is located once the transform scale is implemented

How can I accurately determine the mouse position when a transform scale, such as transform: scale(1.6);, is applied? Currently, the popup element is displaying too far down and to the right in this scenario. Check out the example here $(" ...

Is it possible to incorporate shadows on a pie chart using recharts?

Does anyone know how to enhance a pie chart with shadows using the recharts library? https://i.sstatic.net/JLGVF.png https://i.sstatic.net/f5qv4.png If you have any solutions, please share. Thank you! ...

Moving the center div to the top of the content for mobile screens

Would like some help in positioning the col-md-6 div to the top of my website on mobile, causing both col-md-3 elements to stack on top of each other. <div class="row"> <div class="col-md-3"> <div class="box"> & ...

What causes the odd centering of elements within Bootstrap5 rows on a card? Is there a way to ensure they are centered vertically?

Let me share with you an example of the work I have been doing. <!DOCTYPE html> <html> <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35575a5a41464147544 ...

Transforming Google Maps from using jQuery to AngularJS

If I have a Jquery google map with find address and routing system, my goal is to convert it to an angular google map with more options. The current Jquery google map code looks like this: var directionsDisplay = new google.maps.DirectionsRenderer({ d ...

Having trouble getting my parallax slideshow to work with jquery preventDefault

-UPDATE- After countless hours of online courses, tutorials, and programming, I finally completed my website! Check it out here: The site is almost where I want it to be, but there are a few remaining challenges: 1) AJAX: I'm struggling to get the ...

Spin a picture in Angular 9

One of my tasks involves rotating an image in HTML using a component I'm working on. The code snippet I used is: (document.querySelector('#image') as HTMLElement).style.transform = `rotate(${degree}deg)`; Alternatively, I also tried: docu ...

Align the images in a div to the center of the page

After discovering a jQuery code that rotates images within a div, I am now aiming to centralize this entire process on the webpage. The CSS styles used for the images include: #rotating-item-wrapper { position: relative; } In addition, the following ...

Tips for generating click event with Angular directive

I am completely new to AngularJs directive creation. I have created a directive where, when the user clicks on the delete button, I am trying to print the values of scope, element, and attrs in the console. However, nothing is getting printed. The json dat ...