Docsity

CSS Box Model - Margin, Padding, Border & Width Guide, Study notes of Computer science

Partial preview of the text

Download CSS Box Model - Margin, Padding, Border & Width Guide and more Study notes Computer science in PDF only on Docsity!

CSS Box Model

Margin, Padding, Border & Width

A Beginner-Friendly Guide to Understanding and Using the CSS Box Model

Table of Contents

1. Introduction to CSS

2. What is the CSS Box Model?

3. Why is the Box Model Important?

4. The Four Main Parts

5. Content

6. Padding

7. Padding Shorthand

8. Border

9. Border Styles

10. Border Radius

11. Margin

12. Margin Sides

13. Margin Shorthand

14. Auto Margin

15. Width

16. Width Units

17. Height

18. Complete Box Model

19. Calculating Total Width

20. box-sizing

21. content-box

22. border-box

23. Margin vs Padding

24. Combining Everything

25. Practical Card Example

26. Common Mistakes

27. Complete Example

28. Practice Exercises

29. Quick Revision

30. Important Questions

Final Summary

border-radius rounds the corners of an element.

button {\n border-radius: 8px;\n} Chapter 11: Margin

Margin is space outside an element's border. It creates separation between elements.

div {\n margin: 20px;\n} Chapter 12: Margin on Different Sides

You can control each side independently.

margin-top: 20px;\nmargin-right: 10px;\nmargin-bottom: 30px;\nmargin-left: 10px; Chapter 13: Margin Shorthand

Margin shorthand follows the same top โ†’ right โ†’ bottom โ†’ left pattern.

margin: 20px;\nmargin: 10px 20px;\nmargin: 10px 20px 30px;\nmargin: 10px 20px 30px 40px; Chapter 14: Auto Margin

margin: 0 auto is commonly used to horizontally center a fixed-width block element when there is enough available

space.

.box {\n width: 300px;\n margin: 0 auto;\n} Chapter 15: Width

The width property controls the content width when box-sizing is content-box.

div {\n width: 300px;\n} Chapter 16: Different Width Units

Common units include px, %, em, rem, and vw. Percentages are relative to the containing block, while vw is

relative to viewport width.

.container { width: 80%; }\n.card { width: 300px; }\n.title { width: 20rem; } Chapter 17: Height

The height property controls an element's height. Its relationship with padding and border depends on box-sizing.

div {\n height: 150px;\n} Chapter 18: Complete Box Model

A box can combine width, padding, border, and margin.

.box {\n width: 300px;\n padding: 20px;\n border: 5px solid black;\n margin: 30px;\n} Chapter 19: Calculating Total Width

With content-box: 300px content + 40px left/right padding + 10px left/right border = 350px total border-box width.

Margin is outside that border-box.

Content width = 300px\nPadding = 20px + 20px = 40px\nBorder = 5px + 5px = 10px\nTotal = 300 + 40 + 10 = 350px Chapter 20: box-sizing Property

The box-sizing property controls how declared width and height are calculated. The two important values are

content-box and border-box.

box-sizing: border-box; Chapter 21: content-box

content-box is the default. Declared width applies to content, while padding and border are added outside.

box-sizing: content-box;\nwidth: 300px;\npadding: 20px;\nborder: 5px solid black;\n\nTotal border-box width = 3 Chapter 22: border-box

With border-box, the declared width includes content, padding, and border. This makes sizing more predictable.

  • {\n box-sizing: border-box;\n} Chapter 23: Margin vs Padding

Margin is outside the border and creates space between elements. Padding is inside the border and creates space

around content. Padding is part of the element's box; margin is outside it.

Chapter 24: Combining Everything

You can combine width, padding, border, margin, and border-radius to create clean components.

.card {\n width: 320px;\n padding: 20px;\n border: 2px solid #333;\n margin: 20px;\n border-radius: 10px; Chapter 25: A Practical Card Example

This card uses border-box, padding, border, margin, and rounded corners.

.card {\n width: 300px;\n padding: 20px;\n border: 2px solid black;\n margin: 30px auto;\n border-radius: Chapter 26: Common Beginner Mistakes

Avoid confusing margin with padding, forgetting that padding and borders affect total size under content-box, using

too many fixed widths, and forgetting box-sizing when calculating dimensions.

Chapter 27: Complete Example

A practical container can use global border-box sizing, a percentage width, max-width, auto margins, padding,

border, and rounded corners.

  • {\n box-sizing: border-box;\n}\n\nbody {\n margin: 0;\n font-family: Arial, sans-serif;\n}\n\n.container { Chapter 28: Practice Exercises
  • Create a box with 300px width and 20px padding on all sides.
  • Create a box with a 3px solid border and 15px margin.
  • Create a 400px-wide box and center it with margin: 0 auto.