在網頁開發中,經常需要讓表格內容垂直置頂顯示。雖然現代 CSS 提供了更優雅的解決方案,但了解傳統 HTML 屬性的用法對於維護舊專案或快速原型開發仍很有幫助。
最經典的做法是使用嵌套表格,外層表格設定高度 100% 並配合 valign="middle" 屬性:
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="middle">
<table>
<tr><td>表格內容</td></tr>
</table>
</td>
</tr>
</table>
建議使用 Flexbox 或 Grid 佈局,語義更清晰且易於維護:
.container {
display: flex;
align-items: flex-start; /* 垂直置頂 */
justify-content: center; /* 水平置中 */
min-height: 100vh;
}
掌握多種對齊技巧,能讓你在不同專案需求下靈活選擇最適合的方案。