What is Content in Box Model in CSS Explained
content is the innermost part of the box model where text, images, or other elements appear. It is surrounded by padding, border, and margin, which add space and styling around the content.How It Works
Think of the CSS box model like a gift box. The content is the actual gift inside — the text, image, or element you want to show. Around this gift, you have layers that add space and decoration.
The first layer around the content is padding, which is like soft wrapping that keeps the gift from touching the box edges. Next is the border, which is the box's frame or edge. Finally, the margin is the empty space outside the box that separates it from other boxes.
The content area size depends on the element's width and height, and it changes if you add padding or borders unless you use special CSS rules.
Example
This example shows a box with a visible content area, padding, border, and margin. The content area holds the text inside the box.
div {
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid #4CAF50;
margin: 30px;
background-color: #f0f0f0;
box-sizing: content-box;
}
When to Use
Understanding the content area is important when you want precise control over how much space your text or images take inside a box. For example, if you want a button or card to have a fixed size for the content only, you focus on the content area.
Use this knowledge when designing layouts that need consistent spacing, like forms, menus, or galleries. It helps avoid surprises when padding or borders change the total size of elements.
Key Points
- The
contentis the actual area where your element's content appears. - Padding adds space inside the box around the content.
- Border surrounds the padding and content.
- Margin is the space outside the border separating elements.
- By default, width and height set the content size only (
content-box).