คุณสมบัติ maxWidth สไตล์ DOM ส่งคืนและแก้ไขความกว้างสูงสุดขององค์ประกอบในเอกสาร HTML
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์ -
-
คืนค่า maxWidth
object.style.maxWidth
-
กำลังแก้ไขความกว้างสูงสุด
object.style.maxWidth = “value”
คุณค่า
ในที่นี้ ค่าสามารถเป็น −
| ค่า | คำอธิบาย |
|---|---|
| ไม่มี | ไม่จำกัดความกว้างขององค์ประกอบ |
| สืบทอด | รับค่าคุณสมบัตินี้จากองค์ประกอบหลัก |
| ค่าเริ่มต้น | ตั้งค่าคุณสมบัตินี้เป็นค่าเริ่มต้น |
| ความยาว | ตั้งค่าเป็นหน่วยความกว้าง |
| percentage(%) | ตั้งค่าเป็นเปอร์เซ็นต์ความกว้างขององค์ประกอบหลัก |
ตัวอย่าง
ให้เราดูตัวอย่างของคุณสมบัติ maxWidth สไตล์ −
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
background-image: linear-gradient(to top, #a18cd1 0%, #fbc2eb 100%);
height: 100vh;
}
p {
border: 2px solid #fff;
}
.btn {
background: coral;
border: none;
height: 2rem;
border-radius: 2px;
width: 40%;
display: block;
color: #fff;
outline: none;
cursor: pointer;
}
</style>
</head>
<body>
<h1>DOM Style maxWidth Property Example</h1>
<p>
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.
</p>
<button onclick="add()" class="btn">Set maxWidth</button>
<script>
function add() {
document.querySelector('p').style.maxWidth = "200px";
}
</script>
</body>
</html> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

คลิกที่ “ตั้งค่าความกว้างสูงสุด ” เพื่อกำหนดความกว้างสูงสุดขององค์ประกอบย่อหน้า -
