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

คุณสมบัติ HTML DOM Style justifyContent


คุณสมบัติ HTML DOM justifyContent ใช้สำหรับจัดตำแหน่ง flex items แนวนอนบนแกนหลัก (main axis) เมื่อไม่ได้ใช้พื้นที่ทั้งหมดที่มีอยู่

ไวยากรณ์ (Syntax)

การตั้งค่าคุณสมบัติ justifyContent:

object.style.justifyContent = "flex-start|flex-end|center|space-between|space-around|initial|inherit"

ค่าของคุณสมบัติ (Property Values)

ค่า คำอธิบาย
flex-start จัดวาง items ที่จุดเริ่มต้นของ container (ค่าเริ่มต้น)
flex-end จัดวาง items ที่จุดสิ้นสุดของ container
center จัดวาง items ตรงกลางของ container
space-between จัดวาง items โดยมีช่องว่างระหว่างบรรทัด
space-around จัดวาง items โดยมีช่องว่างระหว่าง ก่อน และหลังบรรทัด
initial ตั้งค่าคุณสมบัติเป็นค่าเริ่มต้น
inherit สืบทอดค่าคุณสมบัติจากองค์ประกอบพาเรนต์

ตัวอย่างการใช้งาน

<!DOCTYPE html> <html> <head> <style> 
    #demo { 
        margin: auto; 
        width: 400px; 
        height: 120px; 
        box-shadow: 0 0 0 5px brown; 
        display: flex; 
        flex-wrap: wrap; 
    } 
    #demo div { 
        padding: 0; 
        width: 50px; 
        height: 50px; 
        border: 5px solid; 
        border-radius: 15%; 
    } 
    #demo div:nth-child(even) { 
        border-color: black; 
    } 
    #demo div:nth-child(odd) { 
        border-color: red; 
    } 
</style> <script> 
    function changeJustifyContent() { 
        document.getElementById("demo").style.justifyContent = "space-between"; 
        document.getElementById("Sample").innerHTML = "คุณสมบัติ justify-content ตั้งเป็น space-between แล้ว"; 
    } 
</script> </head> <body> 
    <div id="demo"> 
        <div></div> 
        <div></div> 
        <div></div> 
        <div></div> 
        <div></div> 
    </div> 
    <p>คลิกปุ่มด้านล่างเพื่อเปลี่ยนคุณสมบัติ justify-content ของ container ด้านบน</p> 
    <button onclick="changeJustifyContent()">เปลี่ยน Justify Content</button> 
    <p id="Sample"></p> 
</body> </html>

ผลลัพธ์

คุณสมบัติ HTML DOM Style justifyContent

เมื่อคลิกปุ่ม "เปลี่ยน Justify Content":

คุณสมบัติ HTML DOM Style justifyContent