Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

วิธีตั้งค่าความสูงสูงสุด (Max Height) ขององค์ประกอบด้วย JavaScript

ใช้คุณสมบัติ maxHeight เพื่อจำกัดความสูงสูงสุดขององค์ประกอบ เนื้อหาที่เกินจะถูกตัดหรือแสดง scroll ตาม overflow

ตัวอย่าง

<!DOCTYPE html>
<html>
  <head>
    <style>
      #box {
        width: 300px;
        background-color: gray;
        overflow: auto;
      }
    </style>
  </head>
  <body>
    <p>Click below to set Maximum height.</p>
    <button type="button" onclick="display()">Max Height</button>
    <div id="box">
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
      <p>This is a div. This is a div. This is a div.</p>
    </div>
    <br>
    <script>
      function display() {
        document.getElementById("box").style.maxHeight = "70px";
      }
    </script>
  </body>
</html>