คุณสมบัติความสูงของ HTML DOM Style ใช้สำหรับการตั้งค่าหรือส่งคืนความสูงขององค์ประกอบ
ต่อไปนี้เป็นไวยากรณ์สำหรับ −
การตั้งค่าคุณสมบัติความสูง −
object.style.height = "auto|length|%|initial|inherit"
อธิบายคุณสมบัติข้างต้นดังนี้ −
| ค่า | คำอธิบาย |
|---|---|
| อัตโนมัติ | นี่คือค่าเริ่มต้นและความสูงที่กำหนดโดยเบราว์เซอร์ |
| ความยาว | กำหนดความสูงของเบราว์เซอร์เป็นหน่วยความยาว |
| % | กำหนดความสูงขององค์ประกอบลูกเป็นเปอร์เซ็นต์ที่สัมพันธ์กับองค์ประกอบหลัก |
| ค่าเริ่มต้น | การตั้งค่าคุณสมบัตินี้เป็นค่าเริ่มต้น |
| สืบทอด | การสืบทอดค่าคุณสมบัติหลัก |
เรามาดูตัวอย่างคุณสมบัติความสูงกัน −
ตัวอย่าง
<!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> ผลลัพธ์

เมื่อคลิกปุ่ม “เปลี่ยนความสูง ปุ่ม −
