萬盛學電腦網

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

CSS垂直排版實現:writing-mode、text-orientation 與現代邏輯屬性完整指南

核心屬性:writing-mode

值書寫方向塊流向內聯流向適用場景
horizontal-tb水平、從上到下垂直向下水平向右 (LTR)默認,拉丁文、中文橫排
vertical-rl垂直、從右到左水平向左垂直向下中文傳統豎排、日文漢字
vertical-lr垂直、從左到右水平向右垂直向下蒙古文、某些設計排版
sideways-rl垂直、字符順時針旋轉 90°水平向左垂直向下拉丁文強制豎排
sideways-lr垂直、字符逆時針旋轉 90°水平向右垂直向下拉丁文強制豎排

text-orientation:控制字符直立/旋轉

.vertical-text {
  writing-mode: vertical-rl;
  text-orientation: mixed;      /* 默認:漢字直立,拉丁字母旋轉 90° */
  text-orientation: upright;    /* 強制所有字符直立 (適合全中文) */
  text-orientation: sideways;   /* 強制所有字符旋轉 (適合拉丁文) */
}

經典豎排實現:寬度限制 + 換行

.vertical-classic {
  writing-mode: vertical-rl;
  width: 1.2em;        /* 僅容納一個漢字寬度,強制換行形成豎列 */
  line-height: 1.5;    /* 字符間距 */
  text-align: center;  /* 水平居中 (在垂直流中是塊方向居中) */
  height: 200px;       /* 固定高度容器 */
  overflow-y: auto;    /* 內容過長可滾動 */
}

現代豎排最佳實踐:邏輯屬性 + 容器查詢

.vertical-layout {
  writing-mode: vertical-rl;
  text-orientation: upright;
  
  /* 邏輯屬性:自適應書寫模式 */
  inline-size: 300px;        /* 塊方向尺寸 (垂直流中是高度) */
  block-size: auto;          /* 內聯方向尺寸 (垂直流中是寬度) */
  padding-block: 24px;       /* 塊方向內邊距 (上下) */
  padding-inline: 16px;      /* 內聯方向內邊距 (左右) */
  margin-block: 16px;        /* 塊方向外邊距 */
  border-block-start: 2px solid var(--color-primary); /* 頂部邊框 */
  
  /* 字體優化 */
  font-feature-settings: "vert" 1, "vrt2" 1;  /* OpenType 垂直替換特性 */
  font-variant: upright;                      /* 等同 text-orientation: upright */
}

@container (max-width: 400px) {
  .vertical-layout {
    writing-mode: horizontal-tb;  /* 窄容器自動切回橫排 */
    inline-size: auto;
    block-size: auto;
  }
}

實戰組件:古詩/歌詞/時間軸豎排卡片

<article class="poem-card">
  <header class="poem-header">
    <h2 class="poem-title">靜夜思</h2>
    <p class="poem-author">李白</p>
  </header>
  <div class="poem-content">
    <p>床前明月光</p>
    <p>疑是地上霜</p>
    <p>舉頭望明月</p>
    <p>低頭思故鄉</p>
  </div>
</article>

.poem-card {
  writing-mode: vertical-rl;
  text-orientation: upright;
  inline-size: 280px;
  padding-block: 32px;
  padding-inline: 24px;
  background: linear-gradient(180deg, #fdf6e3 0%, #f5e6c8 100%);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  font-family: "Noto Serif SC", "Source Han Serif", serif;
  font-size: 1.125rem;
  line-height: 2;
  color: #3d2b1f;
}

.poem-header {
  margin-block-end: 24px;
  padding-block-end: 16px;
  border-block-end: 1px dashed #d4b896;
  text-align: center;  /* 在垂直流中 = 塊方向居中 */
}

.poem-title { font-size: 1.5em; font-weight: 600; margin-block-end: 8px; }
.poem-author { font-size: 0.875em; color: #8b6d4a; }

.poem-content p { margin: 0; text-indent: 0; }  /* 詩歌無需首行縮進 */

混合排版:橫向標題 + 豎向內容

.mixed-layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 24px;
  align-items: start;
}

.mixed-layout .label {
  writing-mode: horizontal-tb;  /* 標題橫向 */
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--color-text-secondary);
  white-space: nowrap;
}

.mixed-layout .content {
  writing-mode: vertical-rl;
  text-orientation: upright;
  inline-size: 200px;
  font-size: 1rem;
  line-height: 1.8;
}

避坑指南

  1. writing-mode 會改變盒模型方向:width/height、margin-top/bottom、border-left/right 等物理屬性含義互換,強制使用邏輯屬性 (inline-size/block-size, margin-block/inline, border-block/inline-start/end)
  2. Flex/Grid 容器內的 writing-mode 子元素:需確保容器 align-items: start 或 stretch,避免交叉軸對齊異常
  3. 表單控件:<input>、<textarea> 在垂直書寫模式下表現不一,建議容器橫排、內部控件獨立 writing-mode: horizontal-tb
  4. 滾動條方向:垂直流中 overflow-y 是塊方向滾動 (視覺上左右),overflow-x 是內聯方向 (視覺上上下)
  5. 文本選中/複製:垂直文本選中體驗差,純展示場景可 user-select: none,編輯場景建議提供橫排編輯模式

瀏覽器支持 (2024)

  • writing-mode:全現代瀏覽器支持 (IE11+ 需 -ms- 前綴)
  • text-orientation:Chrome 48+, Firefox 41+, Safari 15.4+, Edge 79+
  • font-feature-settings: "vert":依賴字體支持,Noto Serif SC / Source Han Serif 完美支持
  • 邏輯屬性:全現代瀏覽器支持

垂直排版不再是「黑科技」,而是現代 CSS 書寫模式規範的標準能力。結合邏輯屬性、容器查詢、OpenType 特性,可構建優雅、響應式、無障礙的中文傳統排版體驗。

copyright © 萬盛學電腦網 all rights reserved