SamDevx SamDevx - 2 months ago 10
CSS Question

CSS - how to align text and an image vertically?

In an html file, I have a link that displays the text "PROTOCOL" and an image as follows.

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


But the link looks horrible - the text and image are not aligned vertically. I need to align them in the middle, preferably using a separate CSS file.

Here is my source at Plunker demonstrating the issue. Can someone help?

Thanks a lot!

Answer

Add vertical-align: middle to 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+PGcgaWQ9IkV4cGFuZGVkIj48Zz48Zz48cGF0aCBkPSJNMTQuMTAxLDQyLjMxNGMtMS44NywwLTMuNjI4LTAuNzI5LTQuOTUtMi4wNTFMNy43MzYsMzguODVjLTIuNzI5LTIuNzI5LTIuNzI5LTcuMTcxLDAtOS44OTlsOS4xOTItOS4xOTIgICAgIGMxLjMyMi0xLjMyMiwzLjA4LT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