Padding vs margin

Also called margin vs padding or CSS box model

Padding is the space inside an element’s border, around its content, while margin is the space outside the border that pushes other elements away.

Padding vs margin explained

Every element is a set of nested boxes, called the box model: the content in the middle, then padding, then the border, then margin. Padding adds room inside the element and takes on its background color, so a button with more padding looks bigger. Margin adds empty, transparent space outside the border, so it moves neighbors away.

A quick rule: if the space should feel like part of the element (a roomy button, a card with space around its text), use padding. If it is distance between separate things (between cards, below a heading), use margin, or better, gap on the parent flex or grid container, which spaces every child evenly with no margins at all.

Two details trip people up. In normal page flow, the vertical margins of stacked blocks overlap: a 24px bottom margin above a 16px top margin leaves 24px in total, the larger of the two. And by default an element’s padding is added to its width; box-sizing: border-box, which Tailwind turns on everywhere, counts padding inside the width instead.

All terms

Flexbox

A CSS layout mode that lines items up in a row or a column and controls how they share space, align, and wrap.

CSS Grid

A CSS layout system that places items on rows and columns at once, so content lines up in both directions.

Container

A wrapper that caps content at a maximum width and centers it, so lines stay readable on wide screens and keep padding on small ones.

Card

A bordered, rounded container that groups one item’s image, title, details, and actions so it can sit in a list or grid with others.