CSS - 如何垂直对齐文本和图像?

时间:2016-10-03 10:41:59

标签: css html5

在一个html文件中,我有一个链接显示文本" PROTOCOL"和图像如下。

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

但链接看起来很糟糕 - 文本和图像没有垂直对齐。我需要在中间对齐它们,最好使用单独的CSS文件。

这是我的source at Plunker证明了这个问题。有人可以帮忙吗?

非常感谢!

3 个答案:

答案 0 :(得分:2)

vertical-align: middle添加到#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+PGcgaWQ9IkV4cGFuZGVkIj48Zz48Zz48cGF0aCBkPSJNMTQuMTAxLDQyLjMxNGMtMS44NywwLTMuNjI4LTAuNzI5LTQuOTUtMi4wNTFMNy43MzYsMzguODVjLTIuNzI5LTIuNzI5LTIuNzI5LTcuMTcxLDAtOS44OTlsOS4xOTItOS4xOT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MHMtMC4zOTEtMS4wMjMsMC0xLjQxNGw2LjM2NC02LjM2NCAgICAgYzIuNzI5LTIuNzI5LDcuMTctMi43MjksOS44OTksMGwxLjQxNCwxLjQxNWMyLjcyOSwyLjcyOSwyLjcyOSw3LjE3LDAsOS44OThsLTkuMTkyLDkuMTkyICAgICBDMjkuNzUsMjkuNTY0LDI3Ljk5MSwzMC4yOTMsMjYuMTIxLDMwLjI5M3oiLz48L2c+PC9nPjwvZz48L3N2Zz4="></span></a>
  </li>
</ul>
</div>

答案 1 :(得分:1)

使用弹性盒可以解决这个问题

#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/PjwhRE9DVFlQRSBzdmcgIFBVQkxJQyAnLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4nICAnaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkJz48c3ZnIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDQ4IDQ4IiBoZWlnaHQ9IjQ4cHgiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDQ4IDQ4IiB3aWR0aD0iNDhweCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayI+PGcgaWQ9IkV4cGFuZGVkIj48Zz48Zz48cGF0aCBkPSJNMTQuMTAxLDQyLjMxNGMtMS44NywwLTMuNjI4LTAuNzI5LTQuOTUtMi4wNTFMNy43MzYsMzguODVjLTIuNzI5L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LjUxNWMtMC4zOTEsMC4zOTEtMS4wMjMsMC4zOTEtMS40MTQsMHMtMC4zOTEtMS4wMjMsMC0xLjQxNGw2LjM2NC02LjM2NCAgICAgYzIuNzI5LTIuNzI5LDcuMTctMi43MjksOS44OTksMGwxLjQxNCwxLjQxNWMyLjcyOSwyLjcyOSwyLjcyOSw3LjE3LDAsOS44OThsLTkuMTkyLDkuMTkyICAgICBDMjkuNzUsMjkuNTY0LDI3Ljk5MSwzMC4yOTMsMjYuMTIxLDMwLjI5M3oiLz48L2c+PC9nPjwvZz48L3N2Zz4="></img></span></a>
            </li>
        </ul>
    </div>

答案 2 :(得分:0)

如果您知道图像的高度,则始终可以将伴随文本的行高设置为相同的值。这样做以及将图像向右浮动应解决