--- title: text-align slug: Web/CSS/text-align tags: - CSS文字 - 参考 - 属性 - 布局 translation_of: Web/CSS/text-align ---
text-align
CSS属性定义行内内容(例如文字)如何相对它的块父元素对齐。text-align
并不控制块元素自己的对齐,只控制它的行内内容的对齐。
/* 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; /* 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;
<div class="grid"> <div class="col"> <div class="row"> <div class="cell"> left <p class="taLeft">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> <div class="cell"> start (ltr) <p class="taStart ltr">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> <div class="cell"> start (rtl) <p class="taStart rtl">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> </div> <div class="row"> <div class="cell"> right <p class="taRight">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> <div class="cell"> end (ltr) <p class="taEnd ltr">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> <div class="cell"> end (rtl) <p class="taEnd rtl">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> </div> <div class="row"> <div class="cell"> center <p class="taCenter">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> <div class="cell"> justify <p class="taJustify">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> <div class="cell"> justify-all <p class="taJustifyAll">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellen tesque vehicu la con vallis.</p> </div> </div> </div> </div>
html,body { height: 100%; box-sizing: border-box; } .grid { width: 100%; height: 100%; display: flex; background: #EEE; font: 1em monospace; } .row { display: flex; flex: 1 auto; flex-direction: row; } .col { display: flex; flex: 1 auto; flex-direction: column; } .cell { width: calc(33.33% - 1em); box-sizing: border-box; margin: .5em; padding: .5em; background-color: #FFF; overflow: hidden; } @media (max-width: 30em) { .row { flex-direction: column; } } p { font: .8em sans-serif; max-width: 100%; box-sizing: border-box; overflow: hidden; resize: horizontal; background: #E4F0F5; padding: .5em; margin: .5em 0 0; } .taLeft { text-align: left; } .taRight { text-align: right; } .taCenter { text-align: center; } .taJustify { text-align: justify; } .taJustifyAll { text-align: justify-all; } .taStart { text-align: start; } .taEnd { text-align: end; } .rtl { direction: rtl; } .ltr { direction: ltr; }
{{EmbedLiveSample("text-align", "100%", 350, "", "", "example-outcome-frame")}}
{{cssinfo}}
text-align属性指定为从以下值列表中选择的单个关键字。
start
{{experimental_inline}}left
,反之则为right
。end
{{experimental_inline}}right
,反之则为left
。left
right
center
<string>
{{experimental_inline}}justify
justify-all
match-parent
{{experimental_inline}}inherit
类似,区别在于start
和end
的值根据父元素的{{cssxref("direction")}}确定,并被替换为恰当的left
或right
。p { background:gold; width:22em; }
some more inline content...p { background:gold; width:22em; margin: 1em auto; }
some more inline content...p { background:gold; width:22em; }
some more inline content...居中一个块元素且不居中它的行内内容的标准兼容的方法是将它的左、右margin设为
auto, 例如:
margin:auto;
或margin:0 auto;
或margin-left:auto; margin-right:auto;
<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>
.example {
text-align: left;
border: solid;
}
{{EmbedLiveSample("Left_alignment","100%","100%")}}
<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>
.example {
text-align: center;
border: solid;
}
{{EmbedLiveSample("Centered_text","100%","100%")}}
<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>
.example {
text-align: justify;
border: solid;
}
{{EmbedLiveSample("Justify","100%","100%")}}
The standard-compatible way to center a block itself without centering its inline content is setting the left and right {{cssxref("margin")}} to auto
, e.g.:
.something {
margin: auto;
}
.something {
margin: 0 auto;
}
.something {
margin-left: auto;
margin-right: auto;
}
规范 | 状态 | 备注 |
---|---|---|
{{SpecName('CSS3 Text', '#text-align', 'visibility')}} | {{Spec2('CSS3 Text')}} | 添加了start 和end关键字 keyword. Changed the unnamed initial value to start (which it was). Added the {{cssxref("<string>")}} value, the match-parent value and the start end double value. |
{{SpecName('CSS2.1', 'text.html#alignment-prop', 'text-align')}} | {{Spec2('CSS2.1')}} | 无改变 |
{{SpecName('CSS1', '#text-align', 'text-align')}} | {{Spec2('CSS1')}} | 首次定义 |