Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

คุณสมบัติความสูงต่ำสุดใน CSS


เราสามารถกำหนดความสูงขั้นต่ำคงที่สำหรับกล่องเนื้อหาขององค์ประกอบโดยใช้คุณสมบัติ CSS ความสูงขั้นต่ำ ซึ่งไม่อนุญาตให้กล่องเนื้อหาขององค์ประกอบมีขนาดเล็กลงแม้ว่าความสูงจะน้อยกว่าความสูงต่ำสุดก็ตาม

ไวยากรณ์

ไวยากรณ์ของคุณสมบัติ CSS min-height มีดังนี้ -

Selector {
   min-height: /*value*/
}

ตัวอย่าง

มาดูตัวอย่างคุณสมบัติ CSS min-height กัน −

<!DOCTYPE html>
<html>
<head>
<title>CSS min-height Property</title>
</head>
<style>
* {
   padding: 2px;
   margin:5px;
}
button {
   border-radius: 10px;
}
#containerDiv {
   width:70%;
   margin: 0 auto;
   padding:20px;
   background-image: linear-gradient(135deg, #dc3545 0%, #9599E2 100%);
   text-align: center;
   border-radius: 10px;
}
#contentDiv{
   min-height:150px;
}
</style>
<body>
<div id="containerDiv">
<div id="contentDiv">
This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
</div>
<button onclick="add()" class="btn">Set minHeight</button>
</div>
<script>
   function add() {
      document.querySelector('#contentDiv').style.minHeight = "100px";
   }
</script>
</body>
</html>

ผลลัพธ์

ต่อไปนี้เป็นผลลัพธ์สำหรับโค้ดด้านบน -

ก่อนคลิกปุ่ม 'Set minHeight' -

คุณสมบัติความสูงต่ำสุดใน CSS

หลังจากคลิกปุ่ม 'Set minHeight' -

คุณสมบัติความสูงต่ำสุดใน CSS

ตัวอย่าง

มาดูตัวอย่างอื่นสำหรับคุณสมบัติ CSS min-height กัน −

<!DOCTYPE html>
<html>
<head>
<title>CSS min-height Property</title>
</head>
<style>
* {
   padding: 2px;
   margin:5px;
}
button {
   border-radius: 10px;
}
#containerDiv {
   width:70%;
   margin: 0 auto;
   padding:20px;
   background-image: linear-gradient(135deg, #dc3545 0%, #9599E2 100%);
   text-align: center;
   border-radius: 10px;
}
#contentDiv1, #contentDiv2{
   width:50%;
   border: 2px solid black;
   margin: 0 auto;
}
</style>
<body>
<div id="containerDiv">
<div id="contentDiv1">
This is paragraph 1 with some dummy text. This is paragraph 1 with some dummy text.
</div>
<div id="contentDiv2">
This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy text.
This is paragraph 1 with some dummy.
</div>
<button onclick="add()" class="btn">Set minHeight</button>
</div>
<script>
   function add() {
      document.querySelector('#contentDiv1').style.minHeight = "95px";
   }
</script>
</body>
</html>

ผลลัพธ์

ต่อไปนี้เป็นผลลัพธ์สำหรับโค้ดด้านบน -

ก่อนคลิกปุ่ม 'Set minHeight' -

คุณสมบัติความสูงต่ำสุดใน CSS

หลังจากคลิกปุ่ม 'Set minHeight' -

คุณสมบัติความสูงต่ำสุดใน CSS