用vertical-align属性排列内联图片
最近,我收到了如下问题:
你如何将一张图片垂直的定位在一段文本中?
答案
让我们应用一个例子-包含一行文本和一张小图片(如下的蓝色盒子所显示)的段落。

接下来,我们需要观察6种主要的能够被用来垂直排列的行。
文本行
1. 头行 - 所有内容之上的头行

2. 文本头行 - 包括所有重音符号文本的头行

注意: 头行和 “文本” 头行看起来是一样的。然而,有时候头行(下面红色部分)更高一些,其中的内容也比文本头行(下面绿色部分)更高一些。一个例子就是一张图片在这行文本之中:

3. 中线 - x-height(字母x的高度)的垂直中央

4. 基线 - 所有字母所在的一条假想的线

5.文本底线 - 包括下行字符(诸如 “j”, “y”, “g” 等)的所有文本的底部。

6. 底线 - 所有内容的底线

图片的默认垂直排列
默认的,图片的底部将会和段落的基线对齐 - 除非给图片应用margin-bottom。那么,图片的底部的margin-bottom将会与段落的基线对齐。在下面的例子中,图片应用了“margin-bottom: 5px”,这使得图片的底部比基线高出了5px。

使用CSS来改变垂直排列
你可以使用CSS vertical-align 属性来改变图片相对于四周文本的竖直位置。
可以使用的属性包括:top, text-top, middle, baseline, text-bottom, bottom, sub, super, percentage and length。
头部
图片的顶端将会与头行对齐。
img.class-name { vertical-align: top; }

文本头部
图片的头部将会与文本头部对齐。
img.class-name { vertical-align: text-top; }

中部
图片的垂直中点将会与段落基线加上段落中x高度的一半对齐。
img.class-name { vertical-align: middle; }

基线
尽管图片会使用基线作为默认的行为来进行垂直排列,你仍然可以使用CSS来进行详细说明。
img.class-name { vertical-align: baseline; }

文本底部
图片的底部将会与文本底线对齐。
img.class-name { vertical-align: text-bottom; }

底部
图片的底部将会与底线对齐。
img.class-name { vertical-align: bottom; }

下标
图片的底部将会与下标内容的基线位置对齐(不管是否有任何的下标内容存在)。
img.class-name { vertical-align: sub; }

上标
图片的底部将会与上标内容的基线位置对齐(不管是否有任何的上标内容存在)。
img.class-name { vertical-align: super; }

百分比
通过设定详细值将会增加(正值)或者减少(负值)图片与基线位置的距离。‘0%’ 与 ‘baseline’表示相同的意思。各种浏览器在定位基于百分比的垂直排列上差别不大。
img.class-name { vertical-align: 30% }

img.class-name { vertical-align: -30% }

长度
通过设定详细值将会增加(正值)或者减少(负值)图片与基线位置的距离。 ‘0%’ 与 ‘baseline’表示相同的意思。
img.class-name { vertical-align: 2px }

img.class-name { vertical-align: -2px }

那么,你现在感受如何呢… 你发现 vertical-align 很有用处吗?你会用它排列内容中的图片吗?
Translation
- Russian by Nickolas Loiko.

October 5th, 2008
9:08 am
Permalink
I use it sometimes for removing the space left for text descenders when an image needs to sit flush with whatever follows it. The other time I use it is for aligning form inputs horizontally (I find if you have an image input next to a text input that vertical-align: middle lines them up well).
October 5th, 2008
9:12 am
Permalink
@John Faulds: I most often use vertical-align in the same way you describe - aligning text next to image inputs. The value I use depends on the visual effect I am after - but middle and text-bottom are ones I have used often.
October 5th, 2008
10:30 am
Permalink
I use vertical-align when aligning icons next to text (e.g icons with “descenders” that need to be aligned with the text bottom). But I didnt know about sub and super. Thanks for this great info!
October 5th, 2008
10:44 am
Permalink
@Divya: Thanks for the comment. Vertical-align is very handy when icons sit beside text - as you mention. Allows us to overwrite the default baseline position
I do not use this as much as aligning image-inputs. This may be because we are using icons in different ways? As most on my icons are decorative and do not need to be clicked directly, I generally try to place icons as background images using CSS… but again, this may be just because we are using them in different ways.
October 8th, 2008
11:59 am
Permalink
Nice breakdown of choices - I too had no idea about the sub and super positioning! Thanks for sharing.
October 8th, 2008
8:29 pm
Permalink
@Joseph R. B. Taylor: Thanks for the comment. I think a lot of people are aware of top and bottom vertical-align, but superscript and subscript are far less commonly known.
October 11th, 2008
4:22 am
Permalink
This is great. My students often have problems with inline images when used for something like a branding header with a background color, and no text. There would be a bottom gap because of the descender space. This space would grow as the text size was increased. Without any actual text in the div they can’t see what is causing the gap. Setting vertical-align: bottom eliminates the gap without requiring a height value on the div. Thanks for the great explanation.
October 11th, 2008
5:54 am
Permalink
@Pat: Good to hear from you. I have had the same concerns from students I have taught too
In the case where there is a single image and no other content inside a parent (like an paragraph or div), I prefer to set the image to “display: block” instead of “vertical-align: bottom”. This forces the image to operate like a block level item and the container then wraps around the image snuggly.
October 13th, 2008
10:10 pm
Permalink
[…] Оригинал: Aligning inline images with the vertical-align property […]
October 20th, 2008
5:31 am
Permalink
It’s very useful and interesting article. It is easy for understanding even for beginner.
October 21st, 2008
1:42 am
Permalink
@Nickolas Loiko: Thanks for the feedback. It was written for beginners, so good to hear!
October 28th, 2008
8:14 pm
Permalink
don’t waste your time, use position relative for this kinda things.
October 28th, 2008
8:43 pm
Permalink
@ federico: There are times I prefer to use “position: relative” too. However, when you want to position something in relationship to the content around it (such as aligning an images to the bottom of the text) then vertical align can sometimes be easier.
As always, there are no perfect answers - just lots of different solutions to the one problem
October 31st, 2008
9:15 pm
Permalink
[…] Оригинал: Aligning inline images with the vertical-align property […]
November 10th, 2008
11:42 am
Permalink
[…] Aligning inline images with the vertical-align property has now been translated into Russian by Nickolas Loiko. Translation: выравнивание inline изображений с помощью css свойства vertical-align […]