vertical-align - CSS: Cascading Style Sheets 编辑

The vertical-align CSS property sets vertical alignment of an inline, inline-block or table-cell box.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

The vertical-align property can be used in two contexts:

  • To vertically align an inline element's box inside its containing line box. For example, it could be used to vertically position an <img> in a line of text:

  • To vertically align the content of a cell in a table:

Note that vertical-align only applies to inline, inline-block and table-cell elements: you can't use it to vertically align block-level elements.

Syntax

/* 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;

The vertical-align property is specified as one of the values listed below.

Values for inline elements

Parent-relative values

These values vertically align the element relative to its parent element:

baseline
Aligns the baseline of the element with the baseline of its parent. The baseline of some replaced elements, like <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>
Aligns the baseline of the element to the given percentage above the baseline of its parent, with the value being a percentage of the line-height property. A negative value is allowed.

Line-relative values

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.

Values for table cells

baseline (and sub, super, text-top, text-bottom, <length>, and <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.

Formal definition

Initial valuebaseline
Applies toinline-level and table-cell elements. It also applies to ::first-letter and ::first-line.
Inheritedno
Percentagesrefer to the line-height of the element itself
Computed valuefor percentage and length values, the absolute length, otherwise the keyword as specified
Animation typea length

Formal syntax

baseline | sub | super | text-top | text-bottom | middle | top | bottom | <percentage> | <length>

Examples

Basic example

HTML

<div>An <img src="https://mdn.mozillademos.org/files/12245/frame_image.svg" alt="link" width="32" height="32" /> image with a default alignment.</div>
<div>An <img class="top" src="https://mdn.mozillademos.org/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://mdn.mozillademos.org/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://mdn.mozillademos.org/files/12245/frame_image.svg" alt="link" width="32" height="32" /> image with a middle alignment.</div>

CSS

img.top { vertical-align: text-top; }
img.bottom { vertical-align: text-bottom; }
img.middle { vertical-align: middle; }

Result

Specifications

SpecificationStatusComment
CSS Level 2 (Revision 1)
The definition of 'vertical-align' in that specification.
RecommendationAdds the <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.
RecommendationInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:73 次

字数:15758

最后编辑:6 年前

编辑次数:0 次

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