CSS Box Model Calculator — Visualize Margin Padding Border

Visualize and calculate the CSS box model with margin, padding, border, and content dimensions. Get accurate total width and height calculations. Runs entirely in your browser.

Box Model Calculator

Box Dimensions
Margin (px)
Border (px)
Padding (px)
CSS Output
box-sizing: border-box;
width: 200px;
height: 150px;
margin: 10px;
padding: 15px;
border: 2px solid #000;
Visual Diagram
Margin: 10px
Border: 2px
Padding: 15px
Total Dimensions
Total Width
254px
Total Height
204px
Calculation

Total Width = Content Width + Padding Left + Padding Right + Border Left + Border Right

200 + 15 + 15 + 2 + 2 = 234px (without margin)

How to Use the Box Model Calculator

  1. Set content dimensions — enter the width and height of your content area.
  2. Adjust margin — set the margin values for each side of the element.
  3. Adjust border — set the border width for each side.
  4. Adjust padding — set the padding values for each side.
  5. View diagram — see the visual representation of your box model.
  6. Check totals — see the calculated total width and height including all spacing.
  7. Copy CSS — grab the generated CSS code with one click.

Why Use This Box Model Calculator

The CSS box model is fundamental to web layout, but calculating total dimensions with margins, borders, and padding can be confusing. This calculator provides a visual diagram that makes it easy to understand how all the pieces fit together.

By visualizing the box model, you can quickly see how changes to margin, padding, and border affect the overall size of your elements. This helps prevent layout issues and ensures your components fit perfectly within their containers.

Frequently Asked Questions

The CSS box model describes the rectangular boxes generated for elements in the document tree. It consists of content, padding, border, and margin areas. Each area adds to the total dimensions of the element, affecting layout and spacing.

content-box (default) — width and height apply only to the content area; padding and border are added on top. border-box — width and height include content, padding, and border. Most developers prefer border-box for more predictable layouts.

Vertical margins between adjacent block elements collapse into a single margin (the larger of the two). Horizontal margins never collapse. Margin collapsing only happens in normal flow, not in floated, absolutely positioned, or flex/grid containers.

Use Cases

Layout Debugging

Quickly calculate total element dimensions to debug layout issues and ensure components fit within their containers.

Responsive Design

Calculate box dimensions to ensure elements scale properly across different screen sizes and breakpoints.

Component Spacing

Design consistent spacing systems by visualizing how margin and padding affect component relationships.

BorderBox Learning

Understand the difference between content-box and border-box sizing models for better CSS authoring.