Course contentsShow
← Web Development
Lesson 265 of 2,9825. FlexboxFree lesson

Flexbox and the Box Model

How margin, padding, and border interact with flex sizing and alignment.

Flexbox and the Box Model

You'll learn how margin, padding, and border affect flex item sizing and alignment within flex containers.

How the Box Model Lives Inside Flexbox

Even though flexbox introduces powerful sizing and alignment properties, every flex item still respects the traditional CSS box model. This means:

  • Content is the innermost area (where text/images live)
  • Padding adds space inside the item, around the content
  • Border wraps around the padding
  • Margin creates space outside the item, between it and other flex items

The Critical Interaction

Here's where it gets interesting: when flexbox calculates sizes using properties like justify-content or align-items, it works with the outer edge of the margin box. Think of margins as creating "personal space bubbles" around each flex item—flexbox respects those bubbles when distributing space.

Padding and borders, however, are considered part of the flex item's size. If you set a flex item to grow and fill available space, its padding and border remain fixed—only the content area stretches.

Practical Implications

  • Margins collapse in normal flow, but in flexbox they never collapse—every margin value counts
  • gap (from earlier lessons) is often cleaner than margins for spacing between items
  • Padding is perfect for internal spacing without affecting item-to-item relationships
  • Borders add to the item's total size, which flexbox includes in its calculations

Key Takeaway: Flexbox respects the complete box model—margins create space between items, while padding and borders are part of each item's calculated size.