萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 文本兩端對齊實戰:text-align: justify 與中文優化完整指南

核心屬性組合

.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 */
}

表單標籤對齊經典場景

標籤長度不一(如「用戶名」「密碼」「確認密碼」「興趣愛好」),需左側標籤右對齊、右側輸入框左對齊,且冒號對齊。

HTML 結構

<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>

CSS 實現(關鍵:字符間插入空格)

.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;
}

text-justify 取值詳解(主要針對 IE/舊 Edge)

值行為適用語言
auto瀏覽器自動選擇通用
inter-word增加單詞間距(空格)拉丁語系、英文
inter-ideograph增加表意字(中日韓)間距✅ 中文首選
newspaper字/詞間距微調,精度最高拉丁語系專業排版
distribute類似 newspaper,優化東亞東亞文檔、泰文
distribute-all-lines包含最後一行的 distribute表意字文檔

現代最佳實踐:Flex 佈局替代兩端對齊

.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;         /* 首段不縮進 */
}

兼容性現狀(2024)

  • text-align: justify:全瀏覽器支援
  • text-justify: inter-ideograph:僅 IE/舊 Edge,現代瀏覽器忽略
  • text-align-last: justify:Chrome 47+、Firefox 49+、Safari 16.4+、Edge 79+
  • 中文兩端對齊現狀:現代瀏覽器對中文 justify 支援有限(需字符間有空格/零寬空格),Flex 方案更可靠

零寬空格輔助技巧(純 CSS 無法解決時)

.chinese-justify {
    /* 使用 JavaScript 在漢字間插入零寬空格 U+200B */
    /* 或服務端渲染時插入 */
}

/* CSS 偽元素無法插入零寬空格,需 JS 處理 */
function enableChineseJustify(selector) {
    document.querySelectorAll(selector).forEach(el => {
        el.innerHTML = el.textContent.split('').join('\u200B');
    });
}

總結建議

  1. 表單標籤對齊:首選 Flex 佈局 + 固定標籤寬度 + text-align: right,最穩健、無兼容性問題
  2. 文章段落兩端對齊:text-align: justify + text-align-last: justify,配合首行縮進
  3. 避免依賴 text-justify: inter-ideograph:非標準、僅 IE 支援
  4. 字符間必須有斷行機會:空格、零寬空格、連字號,否則 justify 不生效
copyright © 萬盛學電腦網 all rights reserved