โมเดลกล่อง CSS
องค์ประกอบ HTML ทั้งหมดถือเป็นกล่อง
โมเดลกล่อง CSS
ใน CSS คำว่า "box model" ใช้เมื่อพูดถึงการออกแบบและเลย์เอาต์
โมเดลกล่อง CSS นั้นเป็นกล่องที่ล้อมรอบทุกองค์ประกอบ HTML ประกอบด้วย: ระยะขอบ ขอบ ช่องว่างภายใน และเนื้อหาจริง ภาพด้านล่างแสดงรูปแบบกล่อง:
คำอธิบายของส่วนต่างๆ:
- เนื้อหา - เนื้อหาของกล่องที่มีข้อความและรูปภาพปรากฏขึ้น
- Padding - ล้างพื้นที่รอบ ๆ เนื้อหา แผ่นรองมีความโปร่งใส
- เส้น ขอบ - เส้นขอบที่ล้อมรอบช่องว่างภายในและเนื้อหา
- Margin - ล้างพื้นที่นอกเส้นขอบ ระยะขอบมีความโปร่งใส
โมเดลกล่องช่วยให้เราเพิ่มเส้นขอบรอบองค์ประกอบ และกำหนดช่องว่างระหว่างองค์ประกอบได้
ตัวอย่าง
การสาธิตรูปแบบกล่อง:
div {
width: 300px;
border: 15px solid
green;
padding: 50px;
margin: 20px;
}
ความกว้างและความสูงขององค์ประกอบ
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
Important: When you set the width and height properties of an element with CSS, you just set the width and height of the content area. To calculate the full size of an element, you must also add padding, borders and margins.
Example
This <div> element will have a total width of 350px:
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}
Here is the calculation:
The total width of an element should be calculated like this:
Total element width = width + left padding + right padding + left border + right border + left margin + right margin
The total height of an element should be calculated like this:
Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin