UtilityKit

500+ fast, free tools. Most run in your browser only; Image & PDF tools upload files to the backend when you run them.

CSS Box Model Visualizer

Visualize the CSS box model interactively — adjust margin, border, padding, and content size to understand element dimensions.

About CSS Box Model Visualizer

The CSS Box Model Visualizer provides an interactive diagram of the CSS box model — the layered system of content, padding, border, and margin that determines how every HTML element is sized and spaced. Adjust each dimension using sliders or number inputs and see the box update visually in real time. The tool labels each layer with its current size and calculates the total element dimensions (width and height including padding and border, accounting for box-sizing: border-box vs content-box). This is an essential learning tool for CSS beginners and a useful sanity check for developers debugging layout issues.

Why use CSS Box Model Visualizer

  • Visual diagram shows each layer (margin/border/padding/content) in proportion.
  • Supports both content-box and border-box sizing models.
  • Calculates total element dimensions including all layers.
  • Generates a CSS snippet you can copy directly into your stylesheet.
  • Visual diagram shows each layer (margin/border/padding/content) in proportion, not just numbers.
  • Supports both content-box and border-box sizing models and shows the difference in total size.

How to use CSS Box Model Visualizer

  1. Use the sliders or number inputs to set margin, border, padding, and content size values.
  2. The box model diagram updates in real time showing each layer to scale.
  3. Toggle between box-sizing: content-box and border-box to see how total dimensions change.
  4. Copy the generated CSS code snippet for the configured box.
  5. Set asymmetric values (different top/right/bottom/left for margin or padding) to mirror real components.
  6. Hover any layer to see its specific contribution to total width and height.

When to use CSS Box Model Visualizer

  • Learning the CSS box model as a beginner frontend developer.
  • Debugging why an element's total size differs from expected.
  • Experimenting with box-sizing: border-box vs content-box behavior.
  • Visualizing how adding padding or border affects overall element dimensions.
  • Debugging why an element's total size differs from expected — usually padding under content-box.
  • Experimenting with box-sizing: border-box vs content-box behavior to understand the cascade impact.

Examples

content-box vs border-box at the same width

Input: width: 200px, padding: 20px, border: 5px

Output: content-box total: 250px (200 + 40 + 10). border-box total: 200px (padding/border absorbed into width).

Asymmetric padding for a card

Input: padding: 16px 24px 12px 24px

Output: .card { padding-top: 16px; padding-right: 24px; padding-bottom: 12px; padding-left: 24px; }

Hidden margin collapse trap

Input: Two siblings: margin-bottom: 20px and margin-top: 30px

Output: Visible gap is 30px (the larger), not 50px — vertical margins collapse between siblings.

Border-box snippet

Input: Width 320, padding 16, border 1, content-aware

Output: .btn { box-sizing: border-box; width: 320px; padding: 16px; border: 1px solid #333; }

Tips

  • Use box-sizing: border-box on every element (* { box-sizing: border-box; }) to make widths and heights predictable — most modern projects do this.
  • Margin can be negative; padding cannot. Negative margins are useful for overlapping elements but often signal a layout shortcut.
  • Vertical margins between adjacent block elements collapse — only the larger margin is applied. The visualizer shows individual margins, not collapsed values.
  • Use padding instead of margin for spacing inside a clickable element so the entire visual area receives clicks.
  • outline draws outside the border without affecting layout — perfect for focus rings without shifting content.
  • Inspect element in DevTools shows the computed box. Use that alongside this tool when diagnosing real-world layout bugs.

Frequently Asked Questions

What is the CSS box model?
Every HTML element is a rectangular box composed of four areas from inside out: content, padding, border, and margin. The box model defines how these layers combine to determine element size and spacing.
What is box-sizing: border-box?
With border-box, the width and height properties include padding and border — so a 100px wide element stays 100px regardless of padding. With the default content-box, width applies only to the content and padding/border is added on top.
Does margin affect element width?
No. Margin is outside the border and creates space between elements, but is not included in the element's rendered dimensions.
Can I set different values for each side?
Yes. You can set top, right, bottom, and left independently for margin, border, and padding.
What is outline vs border?
Outline is drawn outside the border and does not affect box model dimensions or layout. Border is part of the box model and affects element size under content-box sizing.
Why do my margins seem to combine incorrectly?
Adjacent vertical margins collapse — only the larger one is applied. This is by design for typographic flow but surprises newcomers. Use padding or display: flex/grid to avoid collapse.
Should I always use border-box?
Most modern projects apply * { box-sizing: border-box; } as a CSS reset because it makes layout math predictable. Only use content-box when integrating with legacy code that expects it.

Explore the category

Glossary

Box model
CSS specification defining how every element is rendered as a rectangular box composed of content, padding, border, and margin layers.
Content area
The innermost box, containing the element's text, image, or child elements. Width and height (in content-box) refer to this area only.
Padding
Transparent space between content and border, inside the element. Affects clickable area and visual spacing.
Border
Drawn line surrounding the padding. Has its own width, style, and color. Counts toward total element size.
Margin
Transparent space outside the border, separating one element from its siblings. Does not affect element size but does affect layout.
box-sizing: content-box
Default browser behavior. Width and height apply only to the content area; padding and border are added on top, growing total size.
box-sizing: border-box
Width and height include padding and border. Total visible size equals the declared width — far more predictable, almost universally preferred today.
Margin collapse
When two adjacent vertical margins meet (between siblings or parent/child), only the larger margin renders, not the sum.