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

ประเภทของกล่องที่สร้างขึ้นใน CSS


กล่องหนึ่งหรือหลายกล่องจะถูกสร้างขึ้นสำหรับทุกองค์ประกอบในต้นไม้เอกสาร (document tree) หลังจากประมวลผลภายใต้โมเดลการจัดรูปแบบภาพ (visual formatting model) กล่องที่สร้างขึ้นมีคุณสมบัติ CSS บางอย่างเชื่อมโยงอยู่และแสดงผลใน HTML ตามนั้น

กล่องต่อไปนี้ถูกสร้างขึ้นใน CSS:

  • องค์ประกอบระดับบล็อกและกล่องบล็อก
    • กล่องบล็อกแบบไม่ระบุชื่อ
  • องค์ประกอบระดับอินไลน์และกล่องอินไลน์
    • กล่องอินไลน์แบบไม่ระบุชื่อ

ตัวอย่าง

ลองดูตัวอย่างของ Block-level Elements และ Block Boxes:

<!DOCTYPE html> <html> <head> <title>CSS Block-level Elements and Block Boxes</title> <style> form {    width:70%;    margin: 0 auto;    text-align: center; } * {    padding: 2px;    box-sizing: border-box;    /*margin:5px;*/ } input[type="button"] {    border-radius: 10px; } .child{    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 Block-level Elements and Block Boxes</legend> <div id="container">Color Orange <div class="child"></div>Color Red <div class="child"></div>Color Violet <div class="child"></div> </div><br> </body> </html>

ผลลัพธ์

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

ประเภทของกล่องที่สร้างขึ้นใน CSS

ตัวอย่าง

ลองดูตัวอย่างสำหรับ Inline-level Elements และ Inline Boxes:

<!DOCTYPE html> <html> <head> <title>CSS Inline-level Elements and Inline Boxes</title> <style> form {    width:70%;    margin: 0 auto;    text-align: center; } * {    padding: 2px; } input[type="button"] {    border-radius: 10px; } .child{    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 Inline-level Elements and Inline Boxes</legend> <div><span class="child">Orange</span> Color<span class="child">Red</span> Color<span class="child">Violet</span> Color</div><br> </body> </html>

ผลลัพธ์

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

ประเภทของกล่องที่สร้างขึ้นใน CSS