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

คุณสมบัติความกว้างขั้นต่ำใน CSS


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

ไวยากรณ์

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

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

ตัวอย่าง

มาดูตัวอย่างคุณสมบัติความกว้างขั้นต่ำของ CSS −

<!DOCTYPE html>
<html>
<head>
<title>CSS min-width 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-width:200px;
   border: 1px solid black;
}
</style>
<body>
<div id="containerDiv">
<div class="contentDiv">
This is paragraph 1 with some dummy text.
</div>
<div class="contentDiv">
This is paragraph 2 with some dummy text.
</div>
<div class="contentDiv">
This is paragraph 2 with some dummy text.
</div>
<button onclick="add()" class="btn">Set minWidth</button>
</div>
<script>
function add() {
document.querySelectorAll('.contentDiv')[1].style.minWidth = "100%";
}
</script>
</body>
</html>

ผลลัพธ์

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

ก่อนคลิก 'ตั้งค่า minWidth' ปุ่ม −

คุณสมบัติความกว้างขั้นต่ำใน CSS

หลังจากคลิก 'ตั้งค่า minWidth' ปุ่ม −

คุณสมบัติความกว้างขั้นต่ำใน CSS

ตัวอย่าง

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

<!DOCTYPE html>
<html>
<head>
<title>CSS min-width Property</title>
</head>
<style>
* {
padding: 2px;
margin:5px;
}
button {
border-radius: 10px;
}
#containerDiv {
width:70%;
margin: 0 auto;
padding:20px;
background-image: linear-gradient(62deg, #fbab7e 0%, #f7ce68 100%);
text-align: center;
border-radius: 10px;
}
.contentDiv{
min-width:200px;
border: 1px solid black;
}
</style>
<body>
<div id="containerDiv">
<div class="contentDiv">
This is paragraph 1 with some dummy text.
</div>
<div class="contentDiv">
Lorem Text
</div>
<div class="contentDiv">
This is paragraph 3 with some dummy text.
</div>
<button onclick="add()" class="btn">Set minWidth</button>
</div>
<script>
function add() {
   var all = document.querySelectorAll('.contentDiv');
   for(var i=0; i<all.length; i++)
   all[i].style.minWidth = "100%";
}
</script>
</body>
</html>

ผลลัพธ์

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

ก่อนคลิก 'ตั้งค่า minWidth' ปุ่ม −

คุณสมบัติความกว้างขั้นต่ำใน CSS

หลังจากคลิก 'ตั้งค่า minWidth' ปุ่ม −

คุณสมบัติความกว้างขั้นต่ำใน CSS