萬盛學電腦網

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

HTML 表格對齊技巧全解析:文字、垂直、欄位與響應式對齊策略

表格對齊的三個維度

水平對齊、垂直對齊、欄位對齊。善用 CSS 而非過時 HTML 屬性(align、valign),能獲得一致、可維護、響應式的對齊效果。

水平對齊

/* 表頭置中、資料靠左(預設) */
th { text-align: center; }
td { text-align: left; }

/* 數字/金額/日期靠右,利於比較 */
td.numeric { text-align: right; }
td.currency { text-align: right; font-variant-numeric: tabular-nums; }
td.date { text-align: center; }

/* 狀態/標籤置中 */
td.status { text-align: center; }

/* 響應式:手機改左對齊 */
@media (max-width: 640px) {
  th, td.numeric, td.currency, td.date, td.status { text-align: left; }
}

垂直對齊

/* 預設 middle,常需調整為 top */
th, td { vertical-align: middle; }

/* 多行內容靠上 */
td.multiline, td.description { vertical-align: top; }

/* 圖片/圖示垂直置中 */
td.icon { vertical-align: middle; }
td.icon img { vertical-align: middle; }

/* Flexbox 方案(現代、功能更強) */
td.flex-center {
  display: flex;
  align-items: center;    /* 垂直置中 */
  justify-content: center; /* 水平置中 */
  min-height: 48px;       /* 觸控目標大小 */
}

欄位對齊:跨表格、跨區塊對齊

多個表格、表單與表格、側邊欄與主表格需對齊欄位寬度。

方案一:CSS Grid 統一欄寬(推薦現代專案)

.grid-aligned {
  display: grid;
  grid-template-columns: 60px 1fr 150px 200px auto; /* 固定/比例/彈性 */
  gap: 0;
}

.grid-aligned > * { /* thead, tbody, tfoot 或 div 區塊 */
  display: contents; /* 子元素參與 grid 佈局 */
}
.grid-aligned th,
.grid-aligned td {
  padding: 12px 16px;
  border-bottom: 1px solid #dee2e6;
}

/* 表單欄位對齊表格欄位 */
.form-aligned {
  display: grid;
  grid-template-columns: 150px 1fr; /* 標籤寬、輸入框彈性 */
  gap: 8px 16px;
  align-items: center;
}
.form-aligned label { text-align: right; }

方案二:table-layout: fixed + 共享 colgroup

<style>
  .shared-width { table-layout: fixed; width: 100%; }
  .shared-width col:nth-child(1) { width: 60px; }
  .shared-width col:nth-child(2) { width: 30%; }
  .shared-width col:nth-child(3) { width: 150px; }
  .shared-width col:nth-child(4) { width: auto; }
</style>

<table class="shared-width"><colgroup><col><col><col><col></colgroup>...</table>
<table class="shared-width"><colgroup><col><col><col><col></colgroup>...</table>

表單輸入框在表格中的對齊

<table class="form-table">
  <tr>
    <th><label for="name">姓名</label></th>
    <td><input id="name" name="name" class="form-input"></td>
  </tr>
</table>

.form-table td { vertical-align: middle; }
.form-input {
  width: 100%;
  box-sizing: border-box; /* 含 padding/border */
  padding: 8px 12px;
  border: 1px solid #ced4da;
  border-radius: 4px;
  font: inherit; /* 繼承字體,避免字級差異 */
}

/* 移除 input 原生邊框,用 td 控制邊框 */
.form-table.transparent-input input {
  border: none;
  background: transparent;
  box-shadow: none;
  padding: 4px 0;
  width: 100%;
}
.form-table.transparent-input td {
  border-bottom: 1px solid #dee2e6;
}

數字對齊進階:等寬數字字體

.numeric {
  text-align: right;
  font-variant-numeric: tabular-nums; /* 等寬數字,對齊小數點 */
  font-family: 'SF Mono', 'Fira Code', 'Roboto Mono', monospace; /* 確保等寬 */
}

/* 貨幣符號對齊 */
.currency::before { content: '$ '; } /* 或用 data-currency 屬性 */

/* 小數點對齊(CSS 尚無原生支援,方案) */
/* 方案 A:字串分割兩個 span */
1,234.56
/* 方案 B:monospace + text-align 右對齊(近似) */
/* 方案 C:Canvas/WebGL 渲染(極致精準) */

無障礙對齊檢查

  • 文字對齊不影響螢幕閱讀器讀取順序
  • 數字右對齊需確保 lang 屬性正確(影響數字格式)
  • 垂直置中不隱藏內容(overflow: hidden 配合 line-height 注意)

表格對齊核心在於:文字用 text-align、垂直用 vertical-align/flex、跨區塊用 Grid/共享 colgroup、數字用 tabular-nums。拒絕 HTML align/valign,擁抱 CSS 彈性與可維護性。

copyright © 萬盛學電腦網 all rights reserved