.justify-text {
text-align: justify; /* 核心:兩端對齊 */
text-justify: inter-ideograph; /* IE/Edge:中文表意字間距調整 */
text-align-last: justify; /* 現代標準:最後一行也強制對齊 */
-moz-text-align-last: justify; /* Firefox */
-webkit-text-align-last: justify; /* Chrome/Safari */
}
標籤長度不一(如「用戶名」「密碼」「確認密碼」「興趣愛好」),需左側標籤右對齊、右側輸入框左對齊,且冒號對齊。
<form class="form-justify">
<div class="form-row">
<span class="label">用 戶 名</span>
<input type="text" id="username" placeholder="請輸入用戶名">
</div>
<div class="form-row">
<span class="label">密 碼</span>
<input type="password" id="password" placeholder="請輸入密碼">
</div>
<div class="form-row">
<span class="label">確 認 密 碼</span>
<input type="password" id="confirm" placeholder="請再次輸入">
</div>
<div class="form-row">
<span class="label">興 趣 愛 好</span>
<input type="text" id="hobby" placeholder="請輸入興趣">
</div>
<div class="form-submit">
<button type="submit">提交註冊</button>
</div>
</form>
.form-justify {
width: 430px;
margin: 20px auto;
font-family: 'Microsoft JhengHei', 'PingFang TC', sans-serif;
}
.form-row {
display: flex; /* 現代方案:Flex 更簡單可靠 */
align-items: center;
height: 40px;
margin-bottom: 12px;
}
.label {
flex: 0 0 100px; /* 固定標籤寬度 */
text-align: justify; /* 兩端對齊 */
text-align-last: justify; /* 最後一行也對齊 */
line-height: 40px;
padding-right: 12px;
color: #333;
/* 關鍵:字符間必須有空格或零寬空格 */
/* "用 戶 名" 中間有空格 */
}
/* Chrome/Safari 修正:強制展開最後一行 */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
.label::after {
content: "";
display: inline-block;
width: 100%;
overflow: hidden;
height: 0;
}
}
/* 傳統非 Flex 方案(參考) */
.form-row-legacy {
height: 40px;
line-height: 40px;
word-spacing: -1em; /* 抵消字符間距副作用 */
}
.form-row-legacy .label {
display: inline-block;
width: 100px;
text-align: justify;
text-justify: distribute-all-lines; /* IE6-8 */
text-align-last: justify; /* IE9+ */
}
.form-row-legacy input {
display: inline-block;
vertical-align: middle;
width: 200px; height: 28px;
margin-left: 10px;
}
| 值 | 行為 | 適用語言 |
|---|---|---|
auto | 瀏覽器自動選擇 | 通用 |
inter-word | 增加單詞間距(空格) | 拉丁語系、英文 |
inter-ideograph | 增加表意字(中日韓)間距 | ✅ 中文首選 |
newspaper | 字/詞間距微調,精度最高 | 拉丁語系專業排版 |
distribute | 類似 newspaper,優化東亞 | 東亞文檔、泰文 |
distribute-all-lines | 包含最後一行的 distribute | 表意字文檔 |
.form-row-modern {
display: flex;
align-items: center;
gap: 12px;
height: 44px;
}
.label-modern {
flex: 0 0 100px; /* 固定寬度 */
text-align: right; /* 右對齊即可,無需 justify */
padding-right: 8px;
color: #333;
}
.input-modern {
flex: 1;
height: 36px;
padding: 0 12px;
border: 1px solid #d1d5db;
border-radius: 6px;
font-size: 14px;
}
/* 若必須嚴格對齊冒號:標籤內用 span 包裹文字 + 伪元素補齊 */
.label-modern::after {
content: ":";
margin-left: auto; /* 推到最右 */
}
.article-content {
text-align: justify;
text-justify: inter-ideograph;
text-align-last: justify;
line-height: 1.8;
font-size: 16px;
color: #1f2937;
}
.article-content p {
margin: 0 0 1.5em;
text-indent: 2em; /* 首行縮進 */
}
.article-content p:first-child {
text-indent: 0; /* 首段不縮進 */
}
text-align: justify:全瀏覽器支援text-justify: inter-ideograph:僅 IE/舊 Edge,現代瀏覽器忽略text-align-last: justify:Chrome 47+、Firefox 49+、Safari 16.4+、Edge 79+justify 支援有限(需字符間有空格/零寬空格),Flex 方案更可靠.chinese-justify {
/* 使用 JavaScript 在漢字間插入零寬空格 U+200B */
/* 或服務端渲染時插入 */
}
/* CSS 偽元素無法插入零寬空格,需 JS 處理 */
function enableChineseJustify(selector) {
document.querySelectorAll(selector).forEach(el => {
el.innerHTML = el.textContent.split('').join('\u200B');
});
}
text-align: justify + text-align-last: justify,配合首行縮進text-justify: inter-ideograph:非標準、僅 IE 支援