CSS CSS垂直对齐文本到图像的中间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9348889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 03:01:16  来源:igfitidea点击:

CSS Vertical align text to the middle of image

cssvertical-alignment

提问by Lari13

Possible Duplicate:
How do I vertically align text next to an image with CSS?

可能的重复:
如何使用 CSS 垂直对齐图像旁边的文本?

I have difficulties to center spans vertically middlein div.

我很难vertically middle在 div 中居中跨度。

This simple code: http://jsfiddle.net/4hDTb/

这个简单的代码:http: //jsfiddle.net/4hDTb/

HTML:

HTML:

<div class="bar">
    <span>Simple text</span>
    <img src="" class="img1" />
    <span>Another text</span>
    <img src="" class="img2" />
</div>?

CSS:

CSS:

.bar
{
    width: 100%;
    height: 50px;
    border: 1px black solid;
}
.img1
{
    width: 100px;
    height: 50px;
    border: 1px black solid;
}

.img2
{
    width: 200px;
    height: 50px;
    border: 1px black solid;

}

How to center <span>sin div.barvertically middle?

如何<span>sdiv.bar垂直中间居中?

?

?

回答by Joseph

just add img{vertical-align:middle}. it's the image you would want to align, not the text.

只需添加img{vertical-align:middle}. 这是您想要对齐的图像,而不是文本。

回答by Vivek Chandra

if you know the height of the container -- you can use line-heighthttp://jsfiddle.net/4hDTb/3/check the fiddle..

如果您知道容器的高度 - 您可以使用line-heighthttp://jsfiddle.net/4hDTb/3/检查小提琴..