What is the best way to center the 'email promo' text in the top navigation bar alongside the image and other text links?

Struggling with learning CSS and HTML, particularly when it comes to positioning elements on the page. I'm having trouble vertically aligning the elements inside the top nav bar and can't seem to figure out what I'm doing wrong. Any insights into my mistakes that are preventing me from moving the elements where I want them would be greatly appreciated. Here's the code and a link to the js fiddle: https://jsfiddle.net/9trux138/2/

body {
  margin: 0;
}

.topbar {
  background-color: #17D8C7;
  height: 35px;
  overflow: hidden;
  align-items: center;
  padding: 0 2% 0 2%;
}

.topbar {
  display: flex;
  justify-content: space-between;
}

.topbar ul {
  margin: 0;
  padding-left: 0;
  list-style: none;
  position: relative;
  display: inline-block;
}

.topbar ul li {
  display: inline-block;
  vertical-align: middle;
}

.topbar ul li a {
  display: inline-block;
  text-align: center;
  vertical-align: middle;
  text-decoration: none;
}

.topbar ul li img {
  height: 24px;
  padding: 5px 5px 0px 0px;
}

.topright li {
  vertical-align: middle;
}
<!DOCTYPE html>
<html lang="en-gb">

<head>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <title>Complete Suites</title>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body class="body">
  <header class="topbar">
    <ul class="topleft">
      <li>
        <a href="#"><img src="https://s8.postimg.cc/6t7njymhx/location-pin.png
" alt="Location Pin"></a>
      </li>
      <li>
        <a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Contact Email"></a>
      </li>
      <li>
        <a href="#"><img src="https://s8.postimg.cc/fbh3oalat/open-time.png
" alt="Opening Times"></a>
      </li>
    </ul>
    <ul class="topmiddle">
      <li>
        <a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Email Promotions Sign Up"><span>Email Promotions</span></a>
      </li>
    </ul>
    <ul class="topright">
      <li><a href="#">Blog</a></li>
      <li><a href="#">About Us</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </header>


</body>

</html>

James

Answer №1

Make sure to eliminate the padding-top from the img tags to avoid default centering when using vertical-align: middle;

Additionally, remember to include vertical-align: middle on the span and img tags within the anchor, rather than the anchor itself.

.topbar ul li a span {
    vertical-align: middle;
}
.topbar ul li img {
  height: 24px;
  padding: 0 5px 0px 0px;
  vertical-align: middle;
}

body {
  margin: 0;
}

.topbar {
  background-color: #17D8C7;
  height: 35px;
  overflow: hidden;
  align-items: center;
  padding: 0 2% 0 2%;
}

.topbar {
  display: flex;
  justify-content: space-between;
}

.topbar ul {
  margin: 0;
  padding-left: 0;
  list-style: none;
  position: relative;
  display: inline-block;
}

.topbar ul li {
  display: inline-block;
  vertical-align: middle;
}

.topbar ul li a {
  display: inline-block;
  text-align: center;
  text-decoration: none;
}
.topbar ul li a span {
    vertical-align: middle;
}
.topbar ul li img {
  height: 24px;
  padding: 0 5px 0px 0px;
  vertical-align: middle;
}
<!DOCTYPE html>
<html lang="en-gb">

  <head>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <title>Complete Suites</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>

  <body class="body">
    <header class="topbar">
      <ul class="topleft">
        <li><a href="#"><img src="https://s8.postimg.cc/6t7njymhx/location-pin.png
" alt="Location Pin"></a></li>
        <li><a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Contact Email"></a></li>
        <li><a href="#"><img src="https://s8.postimg.cc/fbh3oalat/open-time.png
" alt="Opening Times"></a></li>
      </ul>
      <ul class="topmiddle">
        <li><a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Email Promotions Sign Up"><span>Email Promotions</span></a></li>
      </ul>
      <ul class="topright">
        <li><a href="#">Blog</a></li>
        <li><a href="#">About Us</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </header>


  </body>

</html>

Answer №2

The links for Blog, About Us, and Contact seem to be nicely centered vertically.

Your concern with Email Promotions lies in the fact that you're including a span tag within the same li tag as an img tag. As pointed out by another user, you could use some additional CSS to specifically align the span element vertically if desired.

Alternatively, why not split it into two separate li tags - one for the image and one for the text?

For example:

<li><a href="#"><img src="https://s8.postimg.cc/ytbr47v39/contact-email.png" alt="Email Promotions Sign Up"></a></li>
<li>Email Promotions</li> 

If you need further assistance, feel free to ask!

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

What is the complete list of features that ColorTranslator.FromHtml() supports and what are the reasons behind its departure from traditional CSS color interpretation guidelines

I'm looking to enhance an API by providing users with the ability to specify the color of something. I want it to accept various representations of colors, such as hex codes and CSS colors. Initially, I thought that ColorTranslator.FromHtml() would fu ...

Aligning an image in a way that adjusts to different screen sizes

Struggling with centering an image horizontally in a responsive manner while using Bootstrap v3.3.5? Here's my code: <div class="container"> <div style="margin:0 auto;"> <img src="images/logo.png" alt="logo" /> ...

Executing a JavaScript function using document.write()

When I try to click on the SWF part1 links within the document.write() function in order to call the openswf function, nothing happens. Here is my code: <html> <a href="#" onclick="Popup();">show popup</a> <script> functio ...

Issue with the table not being displayed when any of the submitted fields are left empty

After submitting the first field, I receive the second value, and after submitting the second field, I get the third value. I have a group of three fields that are interconnected. <?php $courtname=''; if(!empty($_POST['court_ ...

A step-by-step guide to showcasing dates in HTML with Angular

I have set up two datepickers in my HTML file using bootstrap and I am attempting to display a message that shows the period between the first selected date and the second selected date. The typescript class is as follows: export class Datepicker { ...

A guide to exporting a PDF in A4 size landscape mode with jspdf

As a novice in UI development, I am currently trying to export HTML content to PDF using the JSPDF library. However, I have encountered difficulties in generating the PDF in A4 size landscape mode. The HTML code includes data with charts (canvasjs/chartjs) ...

What is the best way to remove column and row gaps in a Bootstrap grid system?

Despite adjusting padding with the image, div section, and grid gap to 0px in the grid layout, the image still fails to cover the entire cell. I am seeking a solution to eliminate these unwanted paddings that are highlighted in blue. The Bootstrap 5 .g-0 ...

What is the best way to connect the state of a variable from one class to another in React?

How can I utilize the variable "restaurants" in Editar2.js? ---App.js--- const { restaurantes } = this.state; ---Editar2.js--- const ChooseRestaurant = (props) => { const options = props.restaurants.map((option) => { return(<opt ...

Incorporating a fixed header title when creating a customizable table

I am working on creating a dynamic table with rows and columns based on JSON data. JSON: $scope.dataToShow=[ tableHeder=["First Name","Age"], { name:"rahim", age:23 }, ...

Limiting the size of images within a specific section using CSS

When using CSS, I know how to resize images with the code snippets below: img {width:100%; height: auto; } img {max-width: 600px} While this method works effectively, it applies to every image on the page. What I really need is for some images to be ...

Ensure that the user's browser cache is cleared after deploying a new version on Firebase hosting

Utilizing create-react-app for my front end scripts and firebase hosting for deployment has been smooth overall. However, I've encountered an issue where updates to CSS and HTML files don't always reflect on the domain until I manually clear the ...

Is there a way to alter the background image of the logo specifically for mobile device viewing?

Here is a snippet of my HTML code: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="{{ route('home') }}"> <img src="{{ asset('assets/images/logo-school-icon.png') }}" ...

Avoid using CSS browser prefixes when selecting plugins or addons

I tried searching on Google for a solution, but I couldn't find anything! Is there a trick or library available to avoid repeating CSS extensions for browsers? Instead of using -webkit-, -moz-, and -o- prefixes? -webkit-animation: NAME-YOUR-ANIMATI ...

How to align text with an image as the size of the image adjusts

I'm brainstorming creative ways to handle a webpage section where the image will be swapped out with various images (with widths up to 620px) and a text caption is positioned over it. I aim for the text to adjust its absolute position based on the wid ...

Library with integrated Jquery functionalities

Hello again with another query. I recently came across a jQuery code that allows you to click on an entire table row (tr) to show/hide other rows (specified as .none). I managed to find the code and it works perfectly on JSFiddle. However, when I try to im ...

The HTML button triggers a function to execute on a different webpage when clicked

I'm facing a straightforward issue that I can't seem to figure out due to my limited experience with Angular and web development. The problem revolves around two components, namely home and dashboard. In the home.component.html file, there's ...

Set up an event listener for a specific class within the cells of a table

After spending the last couple of days immersed in various web development resources, I find myself stuck on a particular issue. As someone new to this field, the learning curve is quite steep... Let's take a look at a single row in my project: < ...

What is the best method for retrieving text that does not contain tags (such as text

My challenge involves storing headings and paragraphs into separate arrays, but I am struggling with handling text between <br>. Below is my HTML code and Python snippet: <p><strong>W Ognisku</strong><br>W londyńskim Ognisku ...

Issue with maplace.js preventing the display of the Google map

I have followed the setup instructions found at . After loading the maplace.js file on my server, I incorporated the provided code: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>minimal maplace</title> < ...

The jQuery .html() function seems to only be functioning properly on the initial element

I'm currently collaborating with a partner on a project using WordPress and PHP for an assignment. We've made progress, but I’m encountering a particular issue. My problem arises when utilizing the .html() function in Ajax—only the first tab ...