CSS BOx Model
The CSS box model is a fundamental concept that describes the layout of HTML elements on a web page. The box model consists of four parts:
- Content: The content area is the space where the actual content of the element is displayed, such as text or images.
- Padding: The padding is the space between the content and the element’s border. It can be set using the
padding
property in CSS. - Border: The border is the area that surrounds the padding and content. It can be styled using properties like
border-width
,border-style
, andborder-color
. - Margin: The margin is the space between the border and any adjacent elements. It can be set using the
margin
property in CSS.
Here is an illustration of the CSS box model:
The total width and height of an element is determined by adding the content, padding, border, and margin together. For example, if an element has a content area that is 200px wide and 100px tall, with 10px of padding, a 2px border, and 20px of margin on all sides, the total width and height of the element would be:
- Width: 200px (content) + 20px (left and right padding) + 4px (left and right border) + 40px (left and right margin) = 264px
- Height: 100px (content) + 20px (top and bottom padding) + 4px (top and bottom border) + 40px (top and bottom margin) = 164px
Understanding the CSS box model is essential for creating layouts on the web, as it allows you to control the spacing and positioning of elements in relation to one another.