How to vertically align text and an image using CSS?

I am struggling with aligning a link in my html file. The link displays the text "PROTOCOL" along with an image, but they are not aligned properly. Here is the code snippet:

<a href="#">PROTOCOL<span id="my-image"><img src="xxxxx"></img></span>

The text and image are not vertically aligned, making the link look horrible. I need help to align them in the middle using a separate CSS file. You can find my code on Plunker for reference.

If anyone could assist me with this issue, I would greatly appreciate it. Thank you!

Answer №1

Include the following CSS rule to add vertical-align: middle to the styles of #my-image img:

#my-row{
  border:1px solid #ccc;
  border-width:1px 0;
  list-style:none;
  padding:0;
  text-align:center;
  background-color: white;
}
#my-row a{
  display:inline-block;
  padding:10px;
  color: black;;
  text-decoration:none;
}
#my-image img {
  vertical-align: middle;
}
<div>
  <ul id="my-row">        
    <li>
      <a href="#">PROTOCOL<span id="my-image"><img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjwhRE9DVFlQRSBzdmcgIFBVQkxJQyAnLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4nICAnaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkJz48c3ZnIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDQ4IDQ4IiBoZWlnaHQ9IjQ4cHgiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDQ4IDQ4IiB3aWR0aD0iNDhweCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGcgaWQ9IkV4cGFuZGVkIj48Zz48Zz48cGF0aCBkPSJNMTQuMTAxLDQyLjMxNGMtMS44NywwLTMuNjI4LTAuNzI5LTQuOTUtMi4wNTFMNy43MzYsMzguODVjLTIuNzI5LTIuNzI5LTIuNzI5LTcuMTcxLDAtOS44OTlsOS4xOTItOS4xOTIgICAgIGMxLjMyMi0xLjMyMiwzLjA4LTIuMDUxLDQuOTUtMi4wNTFzMy42MjgsMC43MjksNC45NDksMi4wNTFsMS40MTQsMS40MTRjMC4zOTEsMC4zOTEsMC4zOTEsMS4wMjMsMCwxLjQxNHMtMS4wMjMsMC4zOTEtMS40MTQsMCAgICAgbC0xLjQxNC0xLjQxNGMtMC45NDQtMC45NDQtMi4yLTEuNDY1LTMuNTM1LTEuNDY1Yy0xLjMzNiwwLTIuNTkyLDAuNTIxLTMu...

Answer №2

you can resolve this issue by utilizing flexbox

#my-row{
border:1px solid #ccc;
border-width:1px 0;
list-style:none;
padding:0;
text-align:center;
background-color: white;
}
#my-row a{
padding:10px;
color: black; ;
text-decoration:none;
        display: flex;
        justify-content: center;
        align-items: center;
}
<div>
        <ul id="my-row">        
            <li>
                <a href="#">PROTOCOL<span id="my-image"><img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjwhRE9DVFlQRSBzdmcgIFBVQkxJQyAnLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4nICAnaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkJz48c3ZnIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgICA9IDQ4ICAgNCNjcGM7IGNpdGVybCAuLi48L3N2Zz4 data:image/svg+xml;base64,></span></a>
            </li>
        </ul>
    </div>

Answer №3

One useful tip is to match the line-height of your text with the height of your image. This, combined with floating your image to the right, can help create a visually pleasing layout.

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

Using a template can sometimes display markup instead of the actual content when incorporating context and forms

I am a beginner with Django and I'm facing an issue while trying to display a dictionary and form on the same page. Instead of showing the actual page, it only displays the HTML markup. Below is the code snippet: home/views.py: from django.shortcuts ...

The div needs to be positioned above another div, not beneath it

Just returning to the world of coding and struggling with a basic problem. Any help would be greatly appreciated. I am trying to position the 'header-top' (red) div at the top, and the 'header-bottom' (blue) div at the bottom. However, ...

Using Jquery to create a fading effect when changing the color by checking a checkbox

I'm struggling with adding a fading effect to change the background color of the body element when a checkbox is checked. The color changes successfully, but I can't figure out how to make it fade smoothly. Here is the HTML code: <div class=& ...

Tracking the User Interface efficiency of a Java Applet running on a website

I have developed a Java Swing based messenger applet that runs smoothly on browsers. My next step is to host this applet on a webpage and allow users to test it out. I want to track the time they spend on each task while using the messenger, as well as any ...

When trying to iterate through a list, I am unable to access a property using `<html:checkbox property="...">`

Within my Struts form, there lies a list. Within the JSP file, I am iterating through it in the following manner: <c:forEach items="${MyForm.types}" var="type"> <tr> <td>${type.name}</td> <td>${type.forced ...

Failure of Outlook 2007, 2010, and 2013 to Recognize Conditional CSS

I am currently working on a design for a responsive email. The layout is functioning well in all email clients tested using Litmus, except for Outlook 07/10/13 due to its challenging word rendering engine versions. To ensure correct display across differen ...

Using automatic margins with tabs in the latest version of Bootstrap, known as Bootstrap

Currently exploring Bootstrap 5 and still getting the hang of it - so please bear with me if this question seems a bit rudimentary! I've been experimenting with tabs to create tabbed panes of local content. I've followed the code provided in the ...

Tips for allowing an HTML form to submit only if the numbers in the fields total to a specific sum

One of my forms is responsible for inserting data into a database. Within this form, there are multiple input fields, with 4 specifically designated for numerical values: <form action="page.php" method="post"> <input type="text" name="Text1"> ...

The hover effect on the menu button is not covering the entire button when the screen is resized

It appears that I am encountering a problem with the hover functionality. When my screen is at full size, hovering over a menu option causes the entire background color to change. However, upon resizing the screen, only a part of the background color chang ...

The behavior of -webkit-border-radius differs from that of -moz-border-radius

My website is displaying differently on Safari compared to Firefox. I want the CSS to make it look consistent across both browsers. I understand that I could use two div boxes - one for the outline and one for the image. However, I prefer how Firefox only ...

When the md-menu is clicked, the Top Nav shifts upwards along with the mdDialog buttons

Currently, I am in the process of developing a website using AngularJS and ASP.NET, incorporating Angular Material. However, I encountered an issue where scrolling on the page causes the top navigation to move up the body or essentially "disappear" when cl ...

I am looking for JavaScript or jQuery code that allows me to incorporate a "Save This Page As" button

Is there a way to enhance the saving process for users visiting an HTML page, rather than requiring them to go through File > Save As? I am interested in implementing a "Save Page As" button on the page that would trigger the save as dialog when clicke ...

Avoiding fuzzy text and pictures

Dealing with blurry images on my website has been a challenge. After trying out various solutions, I finally found one that worked for me: img{ -webkit-transform: translate3d(0,0,0); transform: translate3d(0,0,0); } Implementing this CSS fixed the ...

Issues with the functionality of the Angular JS scope variable

Currently trying to understand angularJS, I am encountering an issue with the code in my index.html file. The dynamic typing is functioning correctly in the upper section, but the injection part seems to be malfunctioning. Any idea what might have gone wr ...

Where would you start looking if the right side of a table border is not visible?

Where should I start looking if the right side border of a table element in a div is not visible? ...

Displaying all information across the page width is a feature that

My gridview has 13 columns, all bound fields. When data is bound to the gridview, it causes the page to stretch and the layout becomes lackluster. The page where the gridview is located is inherited from a master page and the gridview is within a panel. We ...

showcasing a set of div elements by using forward and backward buttons

I am dealing with 5 divs that have text content inside. To navigate through each div, I have implemented a back and next button system. <a href="" class="back-btn off">Back</a> | <a href="" class="next-btn">Next</a> <div class ...

eliminate the offspring of a component (chessboard)

Hey there! I'm currently working on developing a chess game and I could really use your expertise to help me solve an issue. In my code, when I try to move a piece in the game, this is what happens: 1. First, I remove the existing piece from its cu ...

Can a Material UI Select element be made unresponsive to hover effects?

When a user hovers over an element, I want to achieve the following functionality: document.getElementById("dropdownCategory").disabled = true; <Tooltip arrow placement="right" title={props.description} > <FormControl id="dro ...

When hovering over .nav-item, the nav-link color remains unchanged

Is there a way to modify the code below to change the color of a navbar item to red when it is hovered over? .nav-item.active .nav-link{ color: blue !important; } .nav-item.hover .nav-link{ color: red !important; } ...