Free CSS Box Model Visualizer

Adjust margin, border, padding, and content dimensions and see the CSS box model live.

Dimensions

Padding (px)

Border (px)

Margin (px)

margin
border
padding
200 × 120

  

How It Works

  1. Enter box model values: set margin, border, padding, and content dimensions using the input fields or interactive sliders.
  2. Explore the visualization: the classic nested box diagram updates in real time, margin (orange), border (yellow), padding (green), and content (blue) are all clearly labeled.
  3. Copy the CSS: click Copy CSS to get the complete box model properties for the configured element.

Why Use CSS Box Model Visualizer?

The CSS box model is one of the most fundamental concepts in web layout, and one of the most common sources of layout bugs, especially when mixing box-sizing values, margin collapsing, and border sizing. This interactive visualizer makes the box model concrete and tangible: you adjust values and immediately see how margin, border, and padding stack around the content area. Essential for learning, debugging, and teaching CSS.

Features

Frequently Asked Questions

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

With content-box (the default), width and height apply only to the content area, padding and border add to the total element size. With border-box, width and height include padding and border, making it easier to predict element sizes. Most modern CSS resets apply box-sizing: border-box to all elements.

What is margin collapsing?

When two vertical margins touch (between adjacent block elements or parent and first/last child), they collapse into a single margin equal to the larger of the two values. This does not happen with flex or grid children, or with elements that have padding or borders.

How do I remove extra spacing below inline elements?

Inline elements (like <img>) have a small gap below them caused by line-height baseline alignment. Fix it by setting display: block on the element, or vertical-align: bottom, or font-size: 0 on the parent.

What the CSS box model actually defines

The CSS box model is the rectangle representation of every element on a web page. Each element has four concentric boxes: the content box (where text, images, child elements live), the padding box (space between content and border, inside the visible boundary), the border box (the visible outline drawn around the padding), and the margin box (transparent space outside the border that separates this element from its neighbors). The total visible size of an element is content + padding + border; margin is invisible but pushes other elements away.

The critical concept is box-sizing. By default (box-sizing: content-box, the CSS 1 specification value), the width and height properties apply only to the content box. If you set width: 200px; padding: 20px; border: 2px solid, the total rendered width is 200 + 40 + 4 = 244 pixels. This is unintuitive: a "200px wide" element actually takes 244 pixels of horizontal space. The box-sizing: border-box alternative (added in CSS 3) reverses this: the width property is the total visible size including padding and border, so the content area shrinks to fit. border-box is what most developers expect; it became the de facto standard via Paul Irish's 2012 "* { box-sizing: border-box }" CSS reset.

Understanding the box model matters because layout bugs almost always trace to confusion about which value applies to which box. "Why is my element wider than I set it?" is content-box surprise. "Why doesn't my margin push siblings down?" is usually margin collapsing. "Why does padding-top: 50% behave weirdly?" is because percentage padding is calculated relative to the parent's width, not height. Every CSS developer encounters these confusions; this visualizer makes them concrete by letting you adjust values and see the resulting box geometry in real time.

How this tool works under the hood

The interactive diagram is a series of nested colored divs corresponding to each box (margin in orange, border in yellow, padding in green, content in blue). As you change the input values, JavaScript updates the inline style attributes of the diagram divs so they reflect the values numerically and visually. The proportions are scaled to fit the preview area; absolute pixel values would push the diagram off-screen for typical inputs.

Total size calculation depends on the box-sizing mode. For content-box: total width = content width + padding-left + padding-right + border-left + border-right. For border-box: total width = the width value (padding and border are subtracted from the content area). The tool computes both modes simultaneously when you toggle box-sizing, so you can compare directly.

The generated CSS output uses your inputs as the property values: box-sizing: border-box; width: 200px; padding: 10px 20px; border: 2px solid; margin: 1rem auto;. Copy this directly into your stylesheet and apply the matching class. Nothing leaves your browser; everything runs locally in JavaScript. No analytics, no backend, no account. Refresh and the state is gone.

Brief history of the CSS box model

Real-world workflows

Common pitfalls and what they mean

Privacy: everything runs in your browser

CSS learning and visualization tools come in two flavors: simple HTML pages running client-side JavaScript (private, fast, no setup) and cloud editors with saved projects (collaborative but with privacy trade-offs). This tool is the first kind. Your entered values, your box-sizing toggle state, your generated CSS: all stay in your browser session. Refresh the page and the state is gone unless you copied the CSS first.

The privacy stakes are low here because box model values rarely contain sensitive information. Still, the lack of analytics matters: you can experiment freely without your trial-and-error process being recorded. Particularly useful in classroom or corporate training settings where having students or trainees register accounts on third-party platforms is a friction point. This is a single static page with no backend, usable in any browser, including locked-down enterprise machines.

When another tool is the right pick

Other frequently asked questions

Should I use universal * { box-sizing: border-box }?

For most projects, yes. Paul Irish's 2012 proposal is now the industry default and aligns with how most developers expect width and height to work. Frameworks like Bootstrap, Tailwind, Bulma, and Material UI ship with border-box as the default. Modern projects starting in 2026 should set *, *::before, *::after { box-sizing: border-box; } at the top of their reset, and rarely need to think about box-sizing again. Specific overrides to content-box are possible for individual elements where border-box would mess things up.

Why does my margin push the parent down instead of pushing siblings?

This is margin collapsing. When a child's top margin meets the parent's top edge with no padding/border between them, the child's margin "escapes" and applies to the parent instead. Fixes: add padding-top: 1px to the parent (any value works), add border: 1px solid transparent, set the parent to a flex or grid container (collapsing is suppressed in those modes), or use display: flow-root on the parent (a modern explicit "establish a new block formatting context" rule). All of these block the margin from collapsing through.

What is the difference between margin: auto and margin: 0?

margin: 0 means no margin; the element sits flush against its neighbors. margin: auto means the browser computes the margin to distribute remaining horizontal space, typically used to center a block element: margin-left: auto; margin-right: auto; equally splits the remaining space. margin: auto auto auto auto (or shorthand margin: auto) centers both horizontally and vertically inside a flex container. Outside flex/grid, only horizontal auto-margins work.

What does outline differ from border?

Outline draws around an element but is outside the box model: it doesn't add to the element's size and doesn't push neighbors away. Border is part of the box model and adds to the rendered size. Outline is typically used for focus rings (:focus-visible outlines) where you want a visible indicator without shifting layout. Border is used when the line should be part of the element's structure. Don't remove outline: none without providing an alternative focus indicator; doing so breaks keyboard accessibility.

Why use logical properties like margin-inline-start?

Logical properties (margin-block-start, padding-inline, etc.) adapt to the document's writing direction. In English (left-to-right), margin-inline-start is the same as margin-left. In Arabic or Hebrew (right-to-left), it becomes margin-right automatically. For sites that support multiple languages, logical properties make CSS internationalization automatic. For English-only sites, they are equivalent in effect but signal intent more clearly: "this margin is at the start of the text flow" rather than "this margin is on the left side."

How do I create a fixed-aspect-ratio box?

The modern way is the aspect-ratio CSS property: aspect-ratio: 16/9 on a div makes its height automatically 9/16 of its width. Supported in all modern browsers (2021+). The legacy technique uses padding-top as a percentage: a div with padding-top: 56.25% creates a 16:9 ratio (9/16 = 56.25%). The padding-top trick still works but the property syntax is cleaner. Use aspect-ratio for new code; the legacy hack remains in Tailwind and other tools that need broad browser support.

Related Tools

CSS Columns CSS Grid Generator Flexbox Generator Border Radius