CSS Box Model
The browser creates a rectangle for each element in the HTML document. The Box Model describes how the padding, border, and margin are added to the content to create this rectangle.
The perimeter of each of the four areas is called an edge. Each edge defines a box.
- The innermost rectangle is the content box. The width and height of this depends on the element's rendered content (text, images and any child elements it may have).
- Next is the padding box, as defined by the padding property. If there is no padding width defined, the padding edge is equal to the content edge.
- Then we have the border box, as defined by the border property. If there is no border width defined, the border edge is equal to the padding edge.
- The outermost rectangle is the margin box, as defined by the margin property. If there is no margin width defined, the margin edge is equal to the border edge.
This CSS styles all div elements to have a top, right, bottom and left border of 5px in width; a top, right, bottom and left margin of 50px; and a top, right, bottom, and left padding of 20px. Ignoring content, our generated box will look like this: