图像和文本垂直对齐?

发布于 2024-12-21 23:02:21 字数 334 浏览 2 评论 0原文

我有一个如下所示的锚标记设置,

<a href='' title='' class='video-author-box hoverbox'>
    <img style='height:32px;' src='IMAGE_URL' alt='' /> 
    Username
</a>

问题是文本与图像顶部对齐,但我希望文本在锚和图像的中间对齐。

示例: http://jsfiddle.net/qZFFX/2/

I have an anchor tag setup like below

<a href='' title='' class='video-author-box hoverbox'>
    <img style='height:32px;' src='IMAGE_URL' alt='' /> 
    Username
</a>

The trouble is that the text aligns to top of image, but I want the text to align in middle of the anchor and image.

Example: http://jsfiddle.net/qZFFX/2/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朮生 2024-12-28 23:02:21

这里有两个简单的解决方案:

  1. 删除图像的 float: left ,这样它将变成内联,并添加 vertical-align: middle

Fiddle 1

  1. 如果您知道图像的高度(在您的情况下为 32px),请设置 line-height它的容器。

小提琴 2

Here are two simple solutions for that:

  1. Remove float: left of the image, so it will become inline, and add vertical-align: middle

Fiddle 1

  1. If you know the height of your image (32px in your case), set the line-height of it's container to it.

Fiddle 2

贱人配狗天长地久 2024-12-28 23:02:21

您可以

在用户名和样式周围添加 span ,如下所示

.video-author-box span{
    display:inline-block;
    padding-top:8px;
}

示例: http:// /jsfiddle.net/qZFFX/3/

然后您可以根据需要调整填充

或者,您可以不使用 span

.video-author-box{
    display:inline-block;
    padding-top:8px;
}

.video-author-box img{
    margin-top:-6px;
}

示例 2:http:// jsfiddle.net/qZFFX/5/

You could do this

Add a span around the username and style like so

.video-author-box span{
    display:inline-block;
    padding-top:8px;
}

Example: http://jsfiddle.net/qZFFX/3/

Then you can adjust the padding as you see fit.

Alternately, you could do this, without the use of span

.video-author-box{
    display:inline-block;
    padding-top:8px;
}

.video-author-box img{
    margin-top:-6px;
}

Example 2: http://jsfiddle.net/qZFFX/5/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文