1. Explain the CSS box model. What is the difference between content-box and border-box?
A strong answer covers:
- Every element is a box of content, padding, border and margin
- content-box: width applies to content only
- border-box: width includes padding and border
- border-box makes layouts easier to reason about
- Margins are outside the box and can collapse vertically
Show model answer
Every element in CSS is drawn as a rectangular box with four layers: the content in the middle, then padding, then border, and finally margin outside. With the default box-sizing: content-box, the width and height you set apply only to the content, so padding and border are added on top. A 200px box with 20px padding becomes 240px wide. With box-sizing: border-box, the width includes padding and border, so the box stays 200px and the content shrinks. Most teams set border-box globally because it makes sizes predictable, especially in responsive grids. Margin is never part of the width; it is space between boxes, and vertical margins between block elements can collapse into one margin.