我有一段HTML代码:
<a href="href" class="my-a-class>
<img alt="alt" src="/path/to/image.jpg" class="my-img-class" /> My text
</a>
我需要垂直对齐"我的文本",但不影响锚点中的图像。我不能添加任何HTML标记,我需要只用CSS来做到这一点。有什么建议吗?
您可以使用 .my-a-class { line-height: value }
来实现此操作。将value
替换为您的图像高度。
如今,更好的方法是使用flexbox:
.my-a-class {
display: flex;
align-items: center;
justify-content: space-between;
}
Try this code:
a {
position:relative;
height:11em;
}
a img{
position: absolute:
top:0;
bottom:0;
margin-top:auto;
margin-bottom:auto;
}
或者
.my-a-class {
position:relative;
height:11em;
}
.my-a-class .my-img-class{
position: absolute:
top:0;
bottom:0;
margin-top:auto;
margin-bottom:auto;
}
设置为a img{vertical-align:middle;}
或.my-img-class{vertical-align:middle;}
,它应该可以正常工作。
将图像和图标与锚标签对齐的正确方法是
在您的CSS中
a {
background: transparent url(/images/your_image) scroll no-repeat left center;
padding: 2px 0px 2px 20px; //fit as you need
}
在你的HTML中
<a href="url">Text</a>