跳轉到

建立 2025-02-25 更新 2026-09-11

文字對齊(Text Alignment)

text-align 控制區塊容器裡行框的水平對齊。它對齊的是文字行(以及 inline / inline-block 子元素),不是用它來排整頁多欄。

常用值

效果
start / end 依書寫方向的起點 / 終點(建議優先於 left/right)
left / right 實體左 / 右
center 置中
justify 兩端對齊,最後一行仍靠 start
justify-all 連最後一行也拉開(支援度較差,少用)
.prose p { text-align: start; }
.hero h1 { text-align: center; }
.caption { text-align: end; color: #666; }

justify 在英文長文可能出現疏密不均;中文有時能讓版面更整齊,但仍要注意詞間空隙。窄欄位(手機)通常不要 justify。

適用範圍

  • 作用在 block 容器 上,影響裡面的行內內容
  • 對純 inline 元素本身設 text-align 沒有意義
  • 置中一個 block 按鈕:把 text-align: center 設在父層,或改用 Flex
<div class="actions">
  <button>送出</button>
</div>
.actions { text-align: center; }

若按鈕是 display: blocktext-align 置中的是它內部的文字,不是按鈕這個盒子。盒子置中請用 margin-inline: auto 或 Flex。

垂直方向:vertical-align

只對 inline / inline-block / table-cell 有效,不能拿來把 div 在頁面垂直置中。

.icon { vertical-align: -0.15em; } /* 與文字對齊微調 */

區塊垂直置中請用 Flex / Grid 的 align-items: center

其他相關

屬性 用途
text-indent 首行縮排(中文排版偶用)
direction / unicode-bidi 雙向文字,通常用 HTML dir
text-wrap: balance 標題換行較均勻(新屬性)
h1, h2 { text-wrap: balance; }

總結

需求 做法
標題置中 標題或父層 text-align: center
按鈕列置中 父層 text-aligndisplay: flex; justify-content: center
整頁垂直置中 Flex / Grid,不是 vertical-align
多語言 優先 start / end

接著用 顏色 把對比與主題做好。