text-align CSS property sets the horizontal alignment of a block element or table-cell box. This means it works like vertical-align but in the horizontal direction.

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.

句法

/* Keyword values */
text-align: left;
text-align: right;
text-align: center;
text-align: justify;
text-align: justify-all;
text-align: start;
text-align: end;
text-align: match-parent;
/* Character-based alignment in a table column */
text-align: ".";
text-align: "." center;
/* Block alignment values (Non-standard syntax) */
text-align: -moz-center;
text-align: -webkit-center;
/* Global values */
text-align: inherit;
text-align: initial;
text-align: unset;
					

text-align property is specified in one of the following ways:

start
如同 left if direction is left-to-right and right if direction is right-to-left.
end
如同 right if direction is left-to-right and left if direction is right-to-left.
left

The inline contents are aligned to the left edge of the line box.

The inline contents are aligned to the right edge of the line box.

center

The inline contents are centered within the line box.

justify

The inline contents are justified. Text should be spaced to line up its left and right edges to the left and right edges of the line box, except for the last line.

justify-all
如同 justify , but also forces the last line to be justified.
match-parent
类似于 inherit , but the values start and end are calculated according to the parent's direction and are replaced by the appropriate left or right 值。
<string>

When applied to a table cell, specifies the alignment character around which the cell's contents will align.

可访问性关注

The inconsistent spacing between words created by justified text can be problematic for people with cognitive concerns such as Dyslexia.

形式定义

初始值 start , or a nameless value that acts as left if direction is ltr , right if direction is rtl if start is not supported by the browser.
适用于 block containers
继承 yes
计算值 as specified, except for the match-parent value which is calculated against its parent's direction value and results in a computed value of either left or right
动画类型 discrete

形式句法

start | end | left | right | center | justify | match-parent
					

范例

Left alignment

HTML

<p class="example">
  Integer elementum massa at nulla placerat varius.
  Suspendisse in libero risus, in interdum massa.
  Vestibulum ac leo vitae metus faucibus gravida ac in neque.
  Nullam est eros, suscipit sed dictum quis, accumsan a ligula.
</p>
					

CSS

.example {
  text-align: left;
  border: solid;
}
					

结果

Centered text

HTML

<p class="example">
  Integer elementum massa at nulla placerat varius.
  Suspendisse in libero risus, in interdum massa.
  Vestibulum ac leo vitae metus faucibus gravida ac in neque.
  Nullam est eros, suscipit sed dictum quis, accumsan a ligula.
</p>
					

CSS

.example {
  text-align: center;
  border: solid;
}
					

结果

Justify

HTML

<p class="example">
  Integer elementum massa at nulla placerat varius.
  Suspendisse in libero risus, in interdum massa.
  Vestibulum ac leo vitae metus faucibus gravida ac in neque.
  Nullam est eros, suscipit sed dictum quis, accumsan a ligula.
</p>
					

CSS

.example {
  text-align: justify;
  border: solid;
}
					

结果

注意事项

The standard-compatible way to center a block itself without centering its inline content is setting the left and right margin to auto , e.g.:

.something {
  margin: auto;
}
					
.something {
  margin: 0 auto;
}
					
.something {
  margin-left: auto;
  margin-right: auto;
}
					

规范

规范 状态 注释
CSS Logical Properties and Values Level 1
The definition of 'text-align' in that specification.
编者草案 No changes
CSS Text Module Level 4
The definition of 'text-align' in that specification.
编者草案 添加 <string> 值。
CSS Text Module Level 3
The definition of 'text-align' in that specification.
工作草案 添加 start , end ,和 match-parent values. Changed the unnamed initial value to start (which it was).
CSS Level 2 (Revision 1)
The definition of 'text-align' in that specification.
推荐 No changes
CSS Level 1
The definition of 'text-align' in that specification.
推荐 初始定义

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
text-align Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 1 IE 完整支持 3 Opera 完整支持 3.5 Safari 完整支持 1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 1 Samsung Internet Android 完整支持 1.0
Prefixed center , left ,和 right values for block alignment 弃用 非标 Chrome 完整支持 1 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Edge 完整支持 79 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox 完整支持 1 Prefixed
完整支持 1 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE 不支持 No Opera 完整支持 15 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari 完整支持 1.3 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 1 Prefixed
Prefixed Implemented with the vendor prefix: -khtml-
WebView Android 完整支持 37 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Chrome Android 完整支持 18 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Firefox Android 完整支持 4 Prefixed
完整支持 4 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android 完整支持 14 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Safari iOS 完整支持 1 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 1 Prefixed
Prefixed Implemented with the vendor prefix: -khtml-
Samsung Internet Android 完整支持 1.0 Prefixed
完整支持 Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
Flow-relative values start and end Chrome 完整支持 1 Edge 完整支持 79 Firefox 完整支持 1 IE 不支持 No Opera 完整支持 15 Safari 完整支持 3.1 WebView Android 完整支持 37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 14 Safari iOS 完整支持 2 Samsung Internet Android 完整支持 1.0
justify-all Chrome 不支持 No Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
match-parent Chrome 完整支持 16 Edge 完整支持 79 Firefox 完整支持 40 IE 不支持 No Opera 完整支持 15 Safari 不支持 No WebView Android 完整支持 37 Chrome Android 完整支持 18 Firefox Android 完整支持 40 Opera Android 完整支持 14 Safari iOS 不支持 No Samsung Internet Android 完整支持 1.0
Character-based alignment in a table column ( <string> value) Experimental Chrome 不支持 No Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 不支持 No Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. CSS Text
  4. 特性
    1. hanging-punctuation
    2. hyphens
    3. letter-spacing
    4. line-break
    5. overflow-wrap
    6. paint-order
    7. tab-size
    8. text-align
    9. text-align-last
    10. text-indent
    11. text-justify
    12. text-size-adjust
    13. text-transform
    14. white-space
    15. word-break
    16. word-spacing

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1