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

คุณสมบัติ Display Property ใน CSS


องค์ประกอบ HTML ทั้งหมดมีค่าเริ่มต้นสำหรับคุณสมบัติ CSS Display property คุณสมบัตินี้ระบุว่าองค์ประกอบจะถูกเรนเดอร์ในเอกสารอย่างไร

หมายเหตุ - คุณสมบัติ display เริ่มต้นสามารถทับท書 (override) ได้ แต่สิ่งนั้นไม่เปลี่ยนประเภทขององค์ประกอบ เพียงแค่เปลี่ยนพฤติกรรมการแสดงผลบนเอกสารเท่านั้น

ต่อไปนี้คือค่าบางส่วนสำหรับคุณสมบัติ CSS Display property:

  • Block
  • Inline
  • Inline-Block
  • None

ตัวอย่าง

ลองดูตัวอย่างของ CSS Display property:

<!DOCTYPE html> <html> <head> <title>CSS Display</title> <style> form {    width:70%;    margin: 0 auto;    text-align: center; } * {    padding: 2px;    margin:5px;    box-sizing: border-box; } input[type="button"] {    border-radius: 10px; } .child{    display: block; /*default value for <div>*/    height: 40px;    width: 100%;    color: white;    border: 4px solid black; } .child:nth-of-type(1){    background-color: #FF8A00; } .child:nth-of-type(2){    background-color: #F44336; } .child:nth-of-type(3){    background-color: #C303C3; } .child:nth-of-type(4){    background-color: #4CAF50; } .child:nth-of-type(5){    background-color: #03A9F4; } .child:nth-of-type(6){    background-color: #FEDC11; } </style> </head> <body> <form> <fieldset> <legend>CSS-Display</legend> <div id="container"> <div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div> </div><br> </body> </html>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้:

คุณสมบัติ Display Property ใน CSS

ตัวอย่าง

ลองดูตัวอย่างเพิ่มเติมของ CSS Display property:

<!DOCTYPE html> <html> <head> <title>CSS Display</title> <style> form {    width:70%;    margin: 0 auto;    text-align: center; } * {    padding: 2px;    margin:5px;    box-sizing: border-box; } input[type="button"] {    border-radius: 10px; } .child{    height: 40px;    color: white;    border: 4px solid black;    display: inline; } .child:nth-of-type(1){    background-color: #FF8A00; } .child:nth-of-type(2){    background-color: #F44336; } .child:nth-of-type(3){    background-color: #C303C3; } .child:nth-of-type(4){    background-color: #4CAF50; } .child:nth-of-type(5){    background-color: #03A9F4; } .child:nth-of-type(6){    background-color: #FEDC11; } </style> </head> <body> <form> <fieldset> <legend>CSS-Display</legend> <div id="container"> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div><br> </body> </html>

ผลลัพธ์

จะได้ผลลัพธ์ดังนี้:

คุณสมบัติ Display Property ใน CSS