CSS Free, no sign-up

CSS box model visualizer

Change width, height, padding, border and margin — the diagram shows every layer of the box and the calculation below shows how wide the element really is. Ready-made CSS appears under the preview.

Result
Ready-made CSS
.box {
  box-sizing: content-box;
  width: 200px;
  height: 120px;
  padding: 20px;
  border: 5px solid;
  margin: 20px;
}

How to use it

  1. Set width and height, then switch box-sizing between content-box and border-box and watch how the rendered size changes.
  2. Adjust padding, border and margin. Untick “Link all sides” to set top, right, bottom and left separately.
  3. Read the calculation under the diagram: it shows how the final width and height are added up.
  4. Click “Copy” and paste the .box rule into your stylesheet.

How it works

Four layers of every box

Every element is a rectangle made of four layers, from the inside out: content (text and child elements), padding (space between the content and the border), border (the line around the element) and margin (transparent space between this element and its neighbors). The background is painted under the content, padding and border, but never under the margin.

content-box vs border-box

By default box-sizing: content-box applies: width and height set only the content, and padding and border are added on top. So width: 200px with padding: 20px and border: 5px gives a box 250px wide. With box-sizing: border-box the same width includes padding and border, and the content shrinks instead. That is why most projects start with this reset:

*, *::before, *::after {
  box-sizing: border-box;
}

Shorthand: 1, 2, 3 or 4 values

padding, margin and border-width accept up to four values in clockwise order: top, right, bottom, left. One value means all sides, two values mean vertical and horizontal, three values mean top, horizontal and bottom. The generator writes the shortest form of 1, 2 or 4 values.

margin: 10px 20px 30px 40px; /* top right bottom left */
margin: 10px 20px; /* vertical horizontal */

Margin collapsing

Vertical margins of neighboring blocks collapse: if one block has margin-bottom: 30px and the next has margin-top: 20px, the gap is 30px (the larger one), not 50px. A parent's margin can also merge with the margin of its first or last child when nothing separates them (no padding, border or content). Horizontal margins never collapse, and neither do the margins of flex and grid items.

.a { margin-bottom: 30px; }
.b { margin-top: 20px; }
/* gap between .a and .b = 30px */

FAQ

What is the difference between content-box and border-box?

In content-box the width is the width of the content only, so padding and border make the element wider. In border-box the width is the width of the whole box up to the outer edge of the border, so the element keeps its size however much padding you add.

Why is my element wider than the width I set?

Most likely box-sizing is content-box, so padding and border are added to the width. A block with width: 100% and padding: 20px then sticks out of its parent by 40px. Set box-sizing: border-box to fix it.

Do margins add up?

Not always. Vertical margins of adjacent blocks collapse into the larger one, while horizontal margins and the margins of flex or grid items do add up. The diagram shows the margin box of a single element, so it does not show collapsing.

Padding or margin — which one should I use?

Use padding for space inside the element: it stays under the background and inside the clickable area. Use margin for space between elements: it is always transparent. Percentage values of both are calculated from the width of the containing block.

This topic in the HTML & CSS course

More CSS tools

CSS gradient generator Linear, radial and conic gradients: pick colors, angle and stops, then copy the code. Flexbox generator and playground Try flex properties on a container and its items: direction, alignment, gaps and ready-made CSS. CSS Grid generator Set columns and rows, gaps and alignment, then copy the ready-made grid code. CSS box-shadow generator Pick offset, blur, color and layers for a shadow, then copy the ready-made box-shadow code. Cubic-bezier generator Drag the control points of an easing curve and copy the ready-made CSS transition. SVG animation generator Pick a shape and an effect — get inline SVG and CSS keyframes ready to paste. CSS specificity calculator Type selectors to see their (a,b,c) weight, a color-coded breakdown and which rule wins. CSS transform generator Move, rotate, scale and skew an element, see its original position and copy the code. CSS clamp() calculator Enter min and max font size and viewport width — get a ready clamp() for fluid typography. Color contrast checker (WCAG) Check text and background contrast against WCAG AA and AAA, and fix a failing pair of colors. CSS clip-path generator Polygons, bevels, arrows and circles: drag control points on the live canvas and copy ready-made CSS. CSS glassmorphism generator Frosted glass effect: backdrop-filter blur, saturation, border highlight and clean CSS. CSS border-radius (Blob) generator Organic shapes and 8-value border-radius: tune horizontal and vertical corner radii. CSS Stacking Context & z-index Visualizer Why z-index: 9999 fails! Explore CSS Stacking Contexts in interactive 3D exploded layers with transform, opacity and isolation triggers. CSS aspect-ratio & object-fit Generator Eliminate squished images & CLS layout jumps: live aspect-ratio proportions and interactive object-position focal point. CSS Filters Studio & Generator Photo effects without design tools: blur, contrast, hue-rotate, sepia, and trending Instagram-style presets. OKLCH Color Picker & Palette Generator Perceptually uniform brightness & vibrant Display P3 colors: OKLCH picker, design system scales, and fallbacks. CSS Scroll-driven Animations Generator Scroll-linked animations without JavaScript: animation-timeline scroll() & view(), reading progress bar & reveal on scroll. CSS Container Queries (@container) Generator Beyond viewport media queries: components adapt directly to their parent container width. Live resizer & @container rules.
WhatsApp