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

Fuzzy background when you scroll

My container has an image that blurs when you scroll down the page by 50 pixels. I feel like I've seen this effect somewhere before, but I can't quite recall where. I want the text to remain unaffected and not turn blue. Here is the HTML: <d ...

Insert a page break after content for desktop browsers

Is it possible to control the display of sections on different screen sizes? I have a page that looks good on 15" laptops, but larger resolutions cause the next section to appear on the first screen. Is there a way to show the next section only on th ...

Transforming the text color after clicking on it on Squarespace - here's how to do it!

How can I make text change color upon clicking in Squarespace? Where should I place the block id in the code to target a specific block? Check out the code snippet I've been experimenting with: <script> document.getElementById('chang ...

For a while now, I've been attempting to transform my paragraph into a block within a section that showcases elements in an inline-flex layout

I have been attempting to adjust the paragraph with the errorDate class so that it displays as a block element directly beneath the input element similar to the image provided here. The section is currently set to appear as an inline-flex. Below is the CS ...

Resolving the Material-UI NextJS Button Styling Dilemma

I've encountered an issue with the styling of a few buttons in JS. When I apply a styling class using className, the formatting works fine on the initial render but loses its styling on subsequent refreshes. This problem is specific to two individual ...

When using nextjs, there is an issue that arises when attempting to define the property 'id' using context.params.id. This results in

Attempting to retrieve data from a JSON file (response.json) hosted on localhost:8000/response.json. Below is the code snippet from pages/test.js: import React from "react"; import Link from "next/link"; import Image from "next/ima ...

I'm having difficulty grasping how this CSS is being used

There's something strange about the way this CSS is being used, and I can't quite wrap my head around it. .tablecontainer table { ... } I'm familiar with table.tablecontainer, which indicates that the class attribute is equal to "table ...

Dealing with models in Vue.js is proving to be quite a challenge

Why isn't myGame showing as 超級馬力歐 initially and changing when the button is pressed? It just displays {{myGame}} instead. I'm not sure how to fix it, thank you! let myApp = new vue({ el:'myApp', data:{ myGame:&a ...

Trigger the OnAppend event for a jQuery element upon its insertion into the DOM

After writing a code snippet that generates custom controls, I encountered an issue where the custom scrollbar was not being applied because the element had not yet been appended to the DOM. The code returns a jQuery element which is then appended by the c ...

AngularJS offers a single checkbox that allows users to select or

For my code, I need to implement a single checkbox. In my doc.ejs file: <tr ng-repeat="folder_l in folderlist | orderBy:created_date" > <td> <div class="permit"> <input class="chkbtn move_check" type="checkbox" id=" ...

before sending the url with fetch(url), it is adjusted

My front end code is currently fetching a URL from a local node.js server using the following snippet: fetch('http://localhost:3000/search/house') .then(.... Upon checking what is being sent to the server (via the network tab in Firefox dev ...

Issue with aligning CSS in Internet Explorer 7

Everything looks great on Firefox and IE8, but on IE 7 the middle "search input text field" is not aligned properly with the other two fields. It seems to be dropping down slightly. Here is the code - thank you for your help: <!DOCTYPE html PUBLIC "-/ ...

Top Method for Incorporating Syntax Highlighting into Code Blocks - React Sanity Blog

I'm currently exploring the most effective method to incorporate syntax highlighting into my react sanity.io blog. Here's a look at the article component I've developed using react: import React, {useEffect, useState} from "react"; import ...

Prevent the button from being enabled until the file input is updated

I want to create a form with an input file for uploading photos, but I need the submit button to be disabled until the input file has a value. Additionally, there are other validations that will also disable the button. Here is the structure of my HTML fi ...

Sorting of boxes is not possible when utilizing the sortable() function

Is there a way to swap the positions of the left and right boxes using the sortable() function from jQuery UI? It seems to work fine for numbers 1 and 2, but not for switching the boxes. $(".sort-me").sortable({ connectWith: ".connectedSortable" } ...

The proper way to link a style sheet within an MVC framework

Currently, I am working on transitioning a website that I created in the Atom text editor to an ASP.NET environment. To adhere to best practices, I have decided to implement the MODEL VIEW CONTROLLER design pattern in this project. While attempting to ad ...

How can you utilize CSS grid to position an alternate symbol alongside ordinary text?

Does anyone have suggestions on how to align an "alt symbol" with "regular text"? I'm struggling to center both elements without resorting to hacky solutions. Currently, I am using CSS grid, but I am open to exploring other solutions that are easy an ...

jQuery Autocomplete without dropdown menu suggestion available

I'm working on a form for my webpage and I want to enhance user experience by adding autocomplete functionality. The goal is to suggest existing names as users type in the 'name' input text box. Although I can see in my console that it&apos ...

Unusual CSS media queries behavior

During my project work, I faced a puzzling issue which can be illustrated with the following example: Here is the sample CSS code: *, *::after, *::before { box-sizing: border-box; margin: 0; border: 0; } @media only screen and (max-width ...

Troubleshooting issue: Angular not resolving controller dependency in nested route when used with requirejs

When the routes are multiple levels, such as http://www.example.com/profile/view, the RequireJS is failing to resolve dependencies properly. However, if the route is just http://www.example.com/view, the controller dependency is resolved correctly. Below ...