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)
How It Works
- Enter box model values: set margin, border, padding, and content dimensions using the input fields or interactive sliders.
- 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.
- 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
- Interactive diagram: the classic nested box model view with color-coded layers (margin, border, padding, content).
- box-sizing toggle: switch between content-box and border-box to see how each mode affects total dimensions.
- Individual sides: set top/right/bottom/left values independently for margin, padding, and border.
- Total size calculation: displays the computed total width and height of the element.
- CSS output: generates ready-to-use CSS for the configured box model.
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
- CSS 1 specifies content-box, 1996. The CSS 1 recommendation (December 1996) defines the W3C box model: width applies to content only, padding and border add to total size. This becomes the standard, but Internet Explorer 4 and 5 implement an alternative model where width includes padding and border (similar to today's border-box).
- IE quirks mode and the Box Model War, 1997 to 2001. IE 4 (1997) and IE 5 ship with their non-standard box model; IE 6 (2001) introduces "standards mode" triggered by a doctype declaration that switches to W3C box model. The discrepancy creates years of cross-browser layout bugs and the "quirks mode vs standards mode" knowledge every web developer must learn through 2010.
- CSS reset and Eric Meyer's classic stylesheet, 2007. Eric Meyer publishes the canonical CSS reset (May 2007) which sets margins, paddings, and other defaults to zero across all elements. Becomes standard practice for predictable cross-browser styling. The reset doesn't address box-sizing directly but normalizes the chaos around inherited defaults.
- box-sizing property in CSS3, 2009 to 2014. CSS3 introduces
box-sizing: border-boxas an opt-in alternative to content-box. WebKit and Firefox support it from 2009; IE 8 (2009) becomes the first IE version to support it natively. Allows developers to choose the IE-style or W3C-style box model per element. - Paul Irish's universal border-box, 2012. Paul Irish (Google) publishes the blog post "* { box-sizing: border-box } FTW" (February 2012), arguing that universal border-box is sane default for new projects. The pattern spreads rapidly through framework defaults (Bootstrap, Tailwind, Material UI). By 2015, most CSS resets and frameworks ship with border-box as the default.
- Logical properties, 2018 to 2024. CSS Logical Properties and Values Module Level 1 (CR June 2018) introduces direction-agnostic alternatives:
margin-inline-start(start of text direction) instead ofmargin-left,padding-block(top + bottom) instead ofpadding-top + padding-bottom. Critical for right-to-left languages (Arabic, Hebrew) and modern internationalization. Browser support reaches 95%+ by 2022.
Real-world workflows
- Debugging layout bugs. When an element is rendering at an unexpected size, opening the browser DevTools' computed style panel reveals the four box values numerically. This visualizer is useful for working out the math beforehand: "if I set width 200, padding 16, border 2, what's the total size in each box-sizing mode?" Especially useful when teaching juniors why
width: 100%with padding overflows the parent in content-box mode. - Teaching CSS to beginners. The box model is the first abstract concept beginning web developers must internalize. A color-coded interactive visualization beats reading specs. Bootcamp instructors and self-study learners use box model visualizers as a "tactile" way to see how margin, border, and padding stack. This tool exists partly for this purpose.
- Design system component sizing. Design systems specify button heights, padding rhythms, and margin scales in tokens (e.g., spacing-1, spacing-2, spacing-3). When implementing these in code, the box model determines whether the final rendered button is the intended size. Visualize the math first to catch off-by-padding errors before they ship to the Figma-to-React handoff.
- Responsive design and percent margins. Percent values for padding, margin, width, and height are calculated relative to the parent's content width (not height, surprisingly), which can cause odd behavior on mobile. The box model visualizer helps you think through what percentages mean before writing the CSS. For example,
padding-top: 50%on a div creates a square aspect-ratio container (used for image placeholders in pre-aspect-ratio-property days). - Accessibility hit-target sizing. WCAG 2.1 SC 2.5.5 (Level AAA) recommends minimum 44x44 CSS pixel touch targets. The box model determines whether a button's combined content + padding meets this threshold. Use the visualizer to confirm that
padding: 12px 16pxaround 18px-line-height text gives a button that exceeds 44px in both dimensions, satisfying the guideline. - CSS Grid and Flexbox alignment. Modern layout tools (CSS Grid, Flexbox) treat each grid/flex item as a box subject to the box model. Understanding how padding and margin interact with
gap,justify-content, andalign-itemsrequires box-model fluency. Margin collapsing is suppressed inside flex/grid containers, which often surprises CSS developers used to traditional block layout.
Common pitfalls and what they mean
- Margin collapsing surprises. When two block elements have margins touching vertically, they collapse into the larger of the two, not the sum. A 20px bottom margin meeting a 30px top margin produces 30px of space, not 50px. Parent and first/last child margins also collapse if the parent has no padding, border, or overflow set. Inside flex or grid containers, margins do not collapse. This rule has caught every CSS developer at some point.
- content-box vs border-box width math. Setting
width: 100%; padding: 20pxon a content-box element makes it 40px wider than the parent (overflow). With border-box, the same declaration sits inside the parent correctly. This is the single most common box-model gotcha; the universal* { box-sizing: border-box }rule fixes most of it preemptively. Always know which mode you're working in. - Percent padding/margin is relative to width.
padding-top: 50%andmargin-bottom: 50%calculate the percentage based on the parent's content width, not height. This is non-obvious and is the basis of the classic "aspect ratio container" hack (a div withpadding-top: 56.25%creates a 16:9 box). Now CSS has theaspect-ratioproperty, but legacy code and modern Tailwind both rely on the percent-padding trick. - Inline element baseline gap. An
<img>inside a containing div has a mysterious 4 to 6 pixel gap below it. This is because inline elements align to the text baseline, leaving room for descenders. Fix withimg { display: block },img { vertical-align: bottom }, or settingfont-size: 0on the parent. This is one of the oldest box-model bugs in CSS, dating to 1996. - Negative margins for layout tricks. Margins accept negative values, which pulls elements toward each other and is occasionally useful (e.g., expanding a container "outside" its parent's padding). However, negative margins are a frequent source of confusing overlap bugs. Use them deliberately, comment your code, and prefer CSS Grid/Flexbox positioning when possible. Negative padding is invalid CSS and ignored by browsers.
- Padding + border + content > container. When the sum of content + padding + border exceeds the parent's available width, the element overflows. Common fixes:
box-sizing: border-box, reducing the width value, usingoverflow: hiddenon the parent (clip the overflow), ormin-width: 0on flex children to override the implicit min-width: auto behavior. Many "layout breaks at mobile" bugs are overflow caused by box-model math.
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
- Browser DevTools for live debugging. Chrome and Firefox DevTools include a box model panel that displays the four box values for any selected element on a live page. Edit the values inline and see the page update. For debugging actual page layouts (not abstract box-model exploration), DevTools beats this tool because it works on your real code with real CSS cascades and inheritance.
- Figma for design-spec output. Designers use Figma, Sketch, or Adobe XD to lay out components with exact pixel measurements. The design tool's inspect panel exports margin and padding values that developers translate into CSS. For design-to-development handoff, the design tool is the source of truth; this visualizer is for understanding CSS behavior, not generating production component specs.
- CSS-in-JS for component encapsulation. When building component libraries with styled-components, Emotion, vanilla-extract, or similar CSS-in-JS solutions, the box model is encapsulated within each component. Margins on the outermost wrapper, padding inside. This visualizer helps reason about an individual component's geometry; the library handles the cross-component layout coordination.
- CSS Grid/Flexbox tools for layout. For multi-element layouts (page grids, card galleries, navigation bars), CSS Grid and Flexbox are the right abstractions, not the box model alone. Our Flexbox Generator and CSS Grid Generator tools help with those layouts directly. The box model is necessary but not sufficient for modern layout; use the right tool for the right scale.
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.