Understand the CSS Box Model
Reason about content, padding, borders and outer spacing.
By BizAIPro Learning · Prerequisites: Read the HTML course first or understand elements and attributes.
What you will learn
- Identify the four box layers
- Use border-box for predictable sizing
- Distinguish padding from margin
Every element has a box
The content area holds text or other content. Padding creates space inside the border. The border surrounds content and padding. Margin creates outside separation from neighboring boxes. When a card feels cramped, decide which layer needs space rather than adding arbitrary widths.
Width depends on box sizing
With the default content-box model, a declared width covers only the content. Padding and borders make the rendered box wider. With border-box, the declared width includes padding and borders. This helps a 100% wide card fit its parent instead of overflowing after padding is added.
Read the example
The preview card uses width: 100% with a maximum width. It can shrink on a narrow screen while remaining comfortable on a large screen. Padding separates the content from the border. Margin provides breathing room outside. A box-sizing rule makes the width calculation easier to predict.
Check the content too
A correct box model does not solve every overflow. A long unbroken URL or fixed-width child can still stretch a layout. Inspect both the parent and the content inside it. Try longer text in the example and check the narrow layout. Prefer flexible dimensions over a collection of device-specific fixed widths.
Read the example
.card {
box-sizing: border-box;
width: 100%;
max-width: 28rem;
padding: 1.5rem;
border: 1px solid #cbd5e1;
margin: 1rem auto;
}Line-by-line explanation
- border-box includes padding and border in width.
- max-width limits growth without preventing shrinking.
- padding adds inner space; margin adds outer space.
Expected output
A card whose padding stays inside its declared width.
Common mistakes to avoid
- Adding padding to a 100% content-box width
- Using margin to fix cramped inner content
- Ignoring overflow from a child element
Make a change. See what happens.
HTML/CSS renders in a script-free preview. JavaScript runs in a separate, time-limited console worker with network access blocked. DOM scripting and package imports are not supported. Browser memory limits still depend on your device.
Page preview
Render your code to see the page.
Keep padding inside a card
Write a .card rule with box-sizing: border-box, width: 100%, and padding: 20px.
Which property adds space inside the border?
Summary & next step
- Can you now identify the four box layers?
- Can you now use border-box for predictable sizing?
- Can you now distinguish padding from margin?
Complete the exercise and pass the knowledge check to complete this lesson. Guest progress stays on this browser and does not sync across devices.

