HTML DOM Style verticalAlign 属性
htmlweb developmentfront end technology更新于 2025/6/11 8:44:17
HTML DOM Style VerticalAlign 属性返回并修改 HTML 文档中 HTML 元素内容的垂直对齐方式。
语法
以下是语法 −
返回 VerticalAlign
object.style.verticalAlign
修改 VerticalAlign
object.style.verticalAlign = “value”
值
此处,值可以为 -
| 值 | 说明 |
|---|---|
| initial | 将此属性值设置为默认值。 |
| inherit | 从其父元素继承此属性值。 |
| length | 将元素增加或减少特定长度。 |
| percentage(%) | 根据 line-height 属性的百分比增加或减少元素。 |
| baseline | 对齐基线元素与其父元素基线对齐。 |
| sub | 将元素对齐为下标。 |
| super | 将元素对齐为上标。 |
| top | 将元素的顶部与行上最高元素的顶部对齐。 |
| text-top | 将元素的顶部与父元素字体的顶部对齐。 |
| middle | 将元素对齐到父元素的中间。 |
| bottom | 将元素的底部与行上最低的元素对齐行。 |
| text-bottom | 它将元素的底部与父元素字体的底部对齐。 |
示例
让我们看一个 HTML DOM Style VerticalAlign 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background: lightblue;
height: 100vh;
}
table {
border: 2px solid #fff;
height: 150px;
}
.btn {
background: #db133a;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
margin: 1rem 0;
}
</style>
</head>
<body>
<h1>DOM Style verticalAlign Property Example</h1>
<table>
<tr>
<td id="myTd">Table Data</td>
</tr>
</table>
<button onclick="add()" class="btn">Set verticalAlign</button>
<script>
function add() {
document.querySelector('td').style.verticalAlign = "top";
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击“设置verticalAlign”按钮调整文本的垂直对齐方式−

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

