Box Model
The CSS box model describes how every element is a rectangular box made of four layers: content, padding, border, and margin. Understanding it is key to controlling spacing and size.
Web
From the inside out: content holds the text or image, padding adds space inside the border, the border wraps the padding, and margin adds space outside the border between elements.
By default an element's width applies only to the content. Setting box-sizing: border-box makes width include padding and border, which is usually more intuitive.
Examples
CSS
div {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 2px solid gray;
margin: 10px;
}Output:
Renders a 200px-wide box (including padding and border) with internal padding, a gray border, and outer spacing.