Alignment slightly off when trying to vertically center inline elements

I'm struggling with aligning an icon (<img>) vertically next to some text. The icon always appears slightly lower than the text, as shown in the example below. Can someone please suggest a solution to center it vertically with the text?

Example:

p {
  font: 10pt Arial, sans-serif;
}

.icon {
  vertical-align: middle;
}
<p>This is some text with an <img src="https://i.imgur.com/IJs3M2P.png" class="icon" alt="Icon" width="16" height="16">icon in it.</p>

Answer №1

One reason for this is that browsers apply a default line-height to elements, so adjusting the vertical-align values can help. Try using text-bottom.

p {
  font: 10pt Arial, sans-serif;
}

.icon {
  vertical-align: text-bottom;
}
<p>This is some text with an <img src="https://i.imgur.com/IJs3M2P.png" class="icon" alt="Icon" width="16" height="16">icon in it.</p>

If you're looking for a quick solution without diving deep into vertical-align, consider using flexbox.

p {
  display: flex;
  align-items: center;
  font: 10pt Arial, sans-serif;
}
<p>This is some text with an <img src="https://i.imgur.com/IJs3M2P.png" class="icon" alt="Icon" width="16" height="16">icon in it.</p>

Answer №3

Attempt using vertical-align: text-top; as a solution.

p {
  font: 10pt Arial, sans-serif;
}
.icon {
  vertical-align: text-top;
}
<p>Here is some content with an <img src="https://i.imgur.com/IJs3M2P.png" class="icon" alt="Icon" width="16" height="16> icon included.</p>

Answer №4

Insert into .icon

  position: relative;
  left: -2px;

This adjustment will shift it two pixels towards the left from its original position.

p {
  font: 10pt Arial, sans-serif;
}
.icon {
  vertical-align: middle;
  position: relative;
  left: -2px;
}
<p>Here lies some text containing an <img src="https://i.imgur.com/IJs3M2P.png" class="icon" alt="Icon" width="16" height="16">icon within.</p>

Answer №5

p {
  font: 10pt Arial, sans-serif;
}
.icon {
  vertical-align: sub;
}
<p>There is text here along with an <img src="https://i.imgur.com/IJs3M2P.png" class="icon" alt="Icon" width="16" height="16">icon.</p>

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

encountering issues while attempting to create personalized profile pages with dynamic content

I'm currently working on creating a dynamic profile page that can be accessed by everyone using the link www.example.com/user/$id. Currently, the profile pages are not public and cannot be viewed by other users. I attempted to add the following code ...

Identify the moment when the SPAN element reappears in sight

I have a question that may have been asked before, but I haven't found a satisfactory answer yet. Within my HTML code, I have a SPAN element with an onclick event, like so: <span onclick='loadDiv()'>Text</span> When a user cli ...

Tips for transferring simple CSS formatting to an independent stylesheet

As part of a university project, I am working on a basic website. Initially, I used in-line styling for the website design. However, the subject coordinator has now changed the requirements and we are only allowed to use an external CSS stylesheet. Most of ...

What could be the reason for receiving an "undefined" response in my API ajax

I'm encountering an issue while attempting to call my API using a search term from a submit form. I keep getting an error that says "cannot read properties of undefined." I'm unsure why there is no data being returned when I send the ajax request ...

Unable to wrap text with CSS in Internet Explorer

I am encountering some issues with certain styles that are functioning correctly in Chrome and Firefox but not in IE. The main problem I am facing is that a specific style that works fine in Chrome does not wrap text into the column in IE. To reproduce t ...

Having trouble selecting elements that have been dynamically generated through an Ajax callback function

During an AJAX callback, I am trying to insert a DIV with a unique ID into another specific DIV called A. However, after each call, if the div with the given id is not found as a child element of DIV A, a new DIV is created and appended. Strangely, every t ...

Disable password styling in Angular by changing the type attribute to "none."

I'm curious about something, I want to add a checkbox in my application that can toggle the visibility of the password input field. HTML <input type="text" ng-model="data.username" placeholder="username" value="username" class="form-control" popo ...

tips for resizing bootstrap dropdown menu to fill entire page

The image clearly shows that the zabubi solution network platform has a dropdown menu with the label "no request for collaboration to be accepted" on top. The menu occupies half of the page and does not seem to be responsive. Even when the page is resized, ...

Creating Text Transitions with JavaScript in Qualtrics: An In-Depth Guide

Currently, I am creating a survey using Qualtrics and I require various texts that will fade in and out within the same area. The goal is to have text that changes every second, displaying the following: Choose a language Escolha um idioma Elija un idiom ...

implementing a smooth transition effect for image changes upon hover

I've been working on a React project where I created a card that changes its image when hovered over. I wanted to add a smoother transition effect to the image change using transition: opacity 0.25s ease-in-out;, but it doesn't seem to be working ...

Ways to evenly distribute divs into rows

Greetings if this question has already been inquired about, but my search turned up nothing quite like it. My current setup is as follows: .main{ display: inline-flex; flex-direction: row; } <div class = "main"> <div class="sub-div ...

What caused the slide show to malfunction during the live preview?

1-Why is the slide show not working in the live preview? It works on my desktop, but when I share it in the live preview, it doesn't work. <div class="slide-show-content"><!-- /image gallery slideshow--> <div class="slide-show-default ...

Tips for creating inline links

Data is taken from a table using $number and printed as follows: echo "<div class='pagination'><a href='page_pagination.php?number=". $number ."' > ". $number ." </a></div>"; The output does not display in one ...

Tips for validating a textarea within a form using JQuery?

I have created a validation form, but I am facing a problem with validation in the Comment or textarea field. I am using the required attribute, but in the textarea, I am not using required, so the textarea is showing as required automatically. When I in ...

Dynamic Code for Removing List Items Based on Date

I need assistance in resolving an issue with my company's website design and function. Specifically, I am working on a page that displays a list of events where employees will be present throughout the year. Here is an example: <div class="contai ...

What is the best way to incorporate a sliding div into these CSS Message Bubbles?

Just dipping my toes into web development, so please bear with me. I am working on a prototype of a Messaging Application and have most of the basics sorted out. Now, I am adding some finishing touches to enhance user experience. My goal is to make it so t ...

What types of devices are compatible with the different versions of the Android operating system?

I am currently working on a jquerymobile application for Android versions 2, 3, and 4, as well as iPhone, iPad, and iPod touch. I am analyzing user-agent strings using jQuery and jquerymobile. After testing with an emulator, I have noticed that all device ...

Customizing the sizes of HTML components (h1, h2, h3, etc.) with Tailwindcss for both desktop and mobile platforms

Currently, I am working on a project using Tailwind CSS and Next.js. I want to customize the font size of h1 element from 40px to 46px for desktop screens (1440px) and then adjust it to 30px for mobile web (375px). I attempted to add a new property in the ...

"Troubleshooting: The Challenge of a Persistent Footer's

I came across a sticky footer that met my needs, but now I am facing an issue. When I increase the resolution beyond my normal setting, the code works fine but there is spacing between the container div and the footer div. I have attached a screenshot to s ...

Stopping the audio player in HTML5 and JavaScript when the next track starts playing

My webpage contains several audio players, and I've implemented this script to handle them. While the players are functioning correctly, the issue arises when a second player is clicked while the first one is still playing. I've searched for a so ...