表格儲存格(`
<!DOCTYPE html>
<html>
<head>
<script>
function valignCell() {
var cells = document.getElementById('myTable').rows[0].cells;
cells[0].vAlign = "bottom"; // 設為底部對齊
}
</script>
</head>
<body>
<table id="myTable" border="1" height="70%">
<tr>
<td>First cell</td>
<td>Second cell</td>
</tr>
<tr>
<td>Third cell</td>
<td>Fourth cell</td>
</tr>
</table>
<form>
<input type="button" onclick="valignCell()" value="Vertical align cell content">
</form>
</body>
</html>
| 值 | 說明 |
|---|---|
top | 頂部對齊 |
middle | 垂直置中(預設) |
bottom | 底部對齊 |
baseline | 基線對齊 |
HTML5 已不建議使用 `valign` 屬性,改用 CSS:
/* CSS */
td.bottom { vertical-align: bottom; }
td.middle { vertical-align: middle; }
td.top { vertical-align: top; }
// JS 切換 class
cells[0].className = "bottom"; // 或 addClass/removeClass
<style>
td { height: 80px; border: 1px solid #ccc; transition: 0.3s; }
.valign-top { vertical-align: top; }
.valign-middle { vertical-align: middle; }
.valign-bottom { vertical-align: bottom; }
</style>
<table id="demo">
<tr><td>內容</td><td>內容</td></tr>
</table>
<button onclick="setAlign('top')">頂部</button>
<button onclick="setAlign('middle')">置中</button>
<button onclick="setAlign('bottom')">底部</button>
<script>
function setAlign(val) {
document.querySelectorAll('#demo td').forEach(function(td) {
td.className = 'valign-' + val;
});
}
</script>