Skip to main content
Frontend is a free bonus on CodeMentor AI β€” the main product is Python. Register to save your progress and unlock all 290 Frontend lessons + 1,000+ Python lessons.Sign up β€” free
← 🎨 CSS as a design systemΒ·Module B1 Β· Lesson 5
TaskStyle the .card div: padding 20px, border 1px solid #ccc, margin 12px, background #f5f5f5. The card's background must show INSIDE the padding (because padding is inside), but NOT inside the margin (margin is outside).

The box model: content + padding + border + margin

100 XP8 minFREE
Theory

Every element is a box. The box has four parts.

From the inside out:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  ← margin
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ border                            β”‚ β”‚
β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚
β”‚ β”‚ β”‚ padding                       β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β”‚ content (your text)       β”‚ β”‚ β”‚ β”‚
β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β”‚
β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. Content β€” the text or child elements.
  2. Padding β€” clear space INSIDE the border. Background color extends here.
  3. Border β€” the visible outline. Has its own thickness, style, color.
  4. Margin β€” clear space OUTSIDE the border. Background does NOT extend here (it's transparent).

Why "padding extends background, margin doesn't"

Padding is INSIDE the box. Whatever background you set fills it. Margin is OUTSIDE the box β€” it's the space BETWEEN this box and the next one. The parent's background shows through.

Shorthand syntax

padding: 16px;                /* all four sides */
padding: 16px 24px;           /* top/bottom 16, left/right 24 */
padding: 8px 16px 24px;       /* top 8, left/right 16, bottom 24 */
padding: 8px 16px 24px 32px;  /* top, right, bottom, left (clockwise from top) */

Same shorthand applies to margin and border-width.

Per-side longhand

padding-top: 16px;
padding-right: 24px;
padding-bottom: 16px;
padding-left: 24px;

border-top: 1px solid #ccc;
border-radius: 8px;
2 tabs
Live preview
← PreviousNext lesson β†’

Get one Python or web tip a day β€” by email

Short, hand-written, no spam. Unsubscribe in one click.