HTML DOM Style height 属性

htmlweb developmentfront end technology更新于 2024/3/15 1:48:17

HTML DOM Style height 属性用于设置或返回元素的高度。

以下是语法 −

设置 height 属性 −

object.style.height = "auto|length|%|initial|inherit"

以上属性解释如下 −

值
描述
auto
这是默认值,由浏览器设置的高度。
length
以长度单位设置浏览器高度。
%
设置子元素相对于其父元素的百分比高度。
initial
将此属性设置为初始值。
inherit
继承父级属性值

我们来看一个 height 属性的示例 −

示例

<!DOCTYPE html>
<html>
<head>
<style>
   #demo1 {
      width:100px;
      height:100px;
      background-color:lightpink;
   }
   #demo2 {
      margin-top:4px;
      width:100px;
      height:100px;
      background-color:coral;
   }
</style>
<script>
   function changeHeight() {
      document.getElementById("demo1").style.height="200px";
      document.getElementById("Sample").innerHTML="The height for the first div has been increased by    100px";
   }
</script>
</head>
<body>
   <div id="demo1" >DIV1</div>
   <div id="demo2" >DIV2</div>
   <p>Change the font height for the text inside above divs by clicking the below button</p>
   <button onclick="changeHeight()">Change Height</button>
   <p id="Sample"></p>
</body>
</html>

输出

点击"更改高度"按钮 −


相关文章


有用资源