align 屬性(已廢棄)早期 HTML 透過 <table align="left|center|right"> 控制表格在頁面中的水平位置:
LEFT:靠左對齊(預設)CENTER:置中對齊RIGHT:靠右對齊<!-- 舊式寫法,不建議使用 -->
<table align="center" border="3" width="400" height="100">
<tr><td>網頁設計軟體</td><td>Dreamweaver</td></tr>
</table>
缺點:屬於表現層屬性,HTML5 已廢棄,無法透過 CSS 覆寫,維護困難。
table {
margin-left: auto;
margin-right: auto;
/* 或簡寫 */
margin: 0 auto;
}
這是 W3C 標準推薦做法,相容所有瀏覽器,且不影響表格內部儲存格對齊。
/* 浮動方式 - 需清除浮動 */
table.float-left { float: left; }
table.float-right { float: right; }
/* Flexbox 容器方式 - 更彈性 */
.container {
display: flex;
justify-content: flex-start; /* 靠左 */
/* justify-content: center; 置中 */
/* justify-content: flex-end; 靠右 */
}
使用 CSS text-align 取代舊有的 <td align="...">:
td, th {
text-align: left; /* 靠左 */
text-align: center; /* 置中 */
text-align: right; /* 靠右 */
/* 針對數字欄位建議靠右對齊,方便比較大小 */
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.table-wrapper {
overflow-x: auto; /* 小螢幕可滾動 */
margin: 2rem 0;
}
table {
border-collapse: collapse;
width: 100%;
min-width: 600px; /* 確保內容不被擠壓 */
margin: 0 auto; /* 表格整體置中 */
}
th, td {
border: 1px solid #ddd;
padding: 0.75rem;
text-align: left; /* 預設靠左 */
}
th { background: #f5f5f5; }
.num-col { text-align: right; } /* 數字欄靠右 */
.center-col { text-align: center; }
</style>
</head>
<body>
<div class="table-wrapper">
<table>
<thead>
<tr>
<th>產品名稱</th>
<th class="num-col">單價</th>
<th class="center-col">庫存</th>
</tr>
</thead>
<tbody>
<tr><td>筆記型電腦</td><td class="num-col">$35,000</td><td class="center-col">12</td></tr>
<tr><td>無線滑鼠</td><td class="num-col">$590</td><td class="center-col">56</td></tr>
</tbody>
</table>
</div>
</body>
</html>
| 需求 | 廢棄屬性 | 現代 CSS 方案 |
|---|---|---|
| 表格整體置中 | table { margin: 0 auto; } | |
| 表格靠左 | table { margin-right: auto; } 或容器 justify-content: flex-start | |
| 表格靠右 | table { margin-left: auto; } 或容器 justify-content: flex-end | |
| 儲存格文字置中 | td { text-align: center; } | |
| 儲存格文字靠右 | td { text-align: right; } |
棄用 align 屬性,全面擁抱 CSS 佈局,不僅符合標準,更能獲得靈活的響應式控制與更易維護的程式碼結構。