The CSS box model is one of the first things you learn and one of the concepts you come back to most often. Understanding it properly — especially the difference between box-sizing: content-box and box-sizing: border-box — prevents a class of layout bugs that affect almost every developer at some point.
What is the box model?
Every HTML element is a rectangular box with four layers:
┌─────────────────────────────────────┐ │ MARGIN │ ← transparent, clears space around border │ ┌─────────────────────────────┐ │ │ │ BORDER │ │ ← the visible edge (if border is set) │ │ ┌───────────────────────┐ │ │ │ │ │ PADDING │ │ │ ← transparent, space inside border │ │ │ ┌─────────────────┐ │ │ │ │ │ │ │ CONTENT │ │ │ │ ← where text and child elements live │ │ │ └─────────────────┘ │ │ │ │ │ └───────────────────────┘ │ │ │ └─────────────────────────────┘ │ └─────────────────────────────────────┘
Enter fullscreen mode
Exit fullscreen mode
Content : the area where your text, images, or child elements appear
Padding :…