vertical-align
CSS
property sets vertical alignment of an inline, inline-block or table-cell box.
The vertical-align property can be used in two contexts:
<img>
in a line of text:
<p> top:<img style="vertical-align:top" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> middle:<img style="vertical-align:middle" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> bottom:<img style="vertical-align:bottom" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> super:<img style="vertical-align:super" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> sub:<img style="vertical-align:sub" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> </p> <p> text-top:<img style="vertical-align:text-top" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> text-bottom:<img style="vertical-align:text-bottom" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> 0.2em:<img style="vertical-align:0.2em" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> -1em:<img style="vertical-align:-1em" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> 20%:<img style="vertical-align:20%" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> -100%:<img style="vertical-align:-100%" src="https://yari-demos.prod.mdn.mozit.cloud/files/15189/star.png"/> </p>
#* {
box-sizing: border-box;
}
img {
margin-right: 0.5em;
}
p {
height: 3em;
padding: 0 .5em;
font-family: monospace;
text-decoration: underline overline;
margin-left: auto;
margin-right: auto;
width: 80%;
}
<table>
<tr>
<td style="vertical-align: baseline">baseline</td>
<td style="vertical-align: top">top</td>
<td style="vertical-align: middle">middle</td>
<td style="vertical-align: bottom">bottom</td>
<td>
<p>There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.</p>
<p>There is another theory which states that this has already happened.</p>
</td>
</tr>
</table>
table {
margin-left: auto;
margin-right: auto;
width: 80%;
}
table, th, td {
border: 1px solid black;
}
td {
padding: 0.5em;
font-family: monospace;
}
注意,
vertical-align
only applies to inline, inline-block and table-cell elements: you can't use it to vertically align
block-level elements
.
/* Keyword values */ vertical-align: baseline; vertical-align: sub; vertical-align: super; vertical-align: text-top; vertical-align: text-bottom; vertical-align: middle; vertical-align: top; vertical-align: bottom; /* <length> values */ vertical-align: 10em; vertical-align: 4px; /* <percentage> values */ vertical-align: 20%; /* Global values */ vertical-align: inherit; vertical-align: initial; vertical-align: unset;
vertical-align
property is specified as one of the values listed below.
These values vertically align the element relative to its parent element:
baseline
<textarea>
, is not specified by the HTML specification, meaning that their behavior with this keyword may vary between browsers.
sub
Aligns the baseline of the element with the subscript-baseline of its parent.
super
Aligns the baseline of the element with the superscript-baseline of its parent.
text-top
Aligns the top of the element with the top of the parent element's font.
text-bottom
Aligns the bottom of the element with the bottom of the parent element's font.
middle
Aligns the middle of the element with the baseline plus half the x-height of the parent.
<length>
Aligns the baseline of the element to the given length above the baseline of its parent. A negative value is allowed.
<percentage>
line-height
property. A negative value is allowed.
The following values vertically align the element relative to the entire line:
top
Aligns the top of the element and its descendants with the top of the entire line.
bottom
Aligns the bottom of the element and its descendants with the bottom of the entire line.
For elements that do not have a baseline, the bottom margin edge is used instead.
baseline
(and
sub
,
super
,
text-top
,
text-bottom
,
<length>
,和
<percentage>
)
Aligns the baseline of the cell with the baseline of all other cells in the row that are baseline-aligned.
top
Aligns the top padding edge of the cell with the top of the row.
middle
Centers the padding box of the cell within the row.
bottom
Aligns the bottom padding edge of the cell with the bottom of the row.
Negative values are allowed.
| 初始值 |
baseline
|
|---|---|
| 适用于 |
inline-level and table-cell elements. It also applies to
::first-letter
and
::first-line
.
|
| 继承 | no |
| 百分比 |
refer to the
line-height
of the element itself
|
| 计算值 | for percentage and length values, the absolute length, otherwise the keyword as specified |
| 动画类型 | a length |
baseline | sub | super | text-top | text-bottom | middle | top | bottom | <percentage> | <length>
<div>An <img src="https://yari-demos.prod.mdn.mozit.cloud/files/12245/frame_image.svg" alt="link" width="32" height="32" /> image with a default alignment.</div> <div>An <img class="top" src="https://yari-demos.prod.mdn.mozit.cloud/files/12245/frame_image.svg" alt="link" width="32" height="32" /> image with a text-top alignment.</div> <div>An <img class="bottom" src="https://yari-demos.prod.mdn.mozit.cloud/files/12245/frame_image.svg" alt="link" width="32" height="32" /> image with a text-bottom alignment.</div> <div>An <img class="middle" src="https://yari-demos.prod.mdn.mozit.cloud/files/12245/frame_image.svg" alt="link" width="32" height="32" /> image with a middle alignment.</div>
img.top { vertical-align: text-top; }
img.bottom { vertical-align: text-bottom; }
img.middle { vertical-align: middle; }
| 规范 | 状态 | 注释 |
|---|---|---|
|
CSS Level 2 (Revision 1)
The definition of 'vertical-align' in that specification. |
推荐 |
添加
<length>
value and allows it to be applied to elements with a
display
type of
table-cell
.
|
|
CSS Level 1
The definition of 'vertical-align' in that specification. |
推荐 | 初始定义。 |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
vertical-align
|
Chrome 完整支持 1 | Edge 完整支持 12 | Firefox 完整支持 1 | IE 完整支持 4 | Opera 完整支持 4 | Safari 完整支持 1 | WebView Android 完整支持 1 | Chrome Android 完整支持 18 | Firefox Android 完整支持 4 | Opera Android 完整支持 14 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
完整支持
line-height
,
text-align
,
margin
vertical-align
, or "How (Not) To Vertically Center Content"
border-collapse
border-spacing
caption-side
empty-cells
table-layout
vertical-align