CSS Box Model Calculator

Ad Placeholder - Top (728×90)

Configure Parameters

Box Model Visualization

Margin
Border
Padding
200 × 100

Calculated Results

Total Width
264
px
Total Height
164
px
Content Width
200
px
Content Height
100
px
Tip: Currently in border-box mode: width/height includes padding and border. The element's actual footprint equals the set value plus margin.

Generated CSS Code

.box { width: 200px; height: 100px; padding: 20px; border: 2px solid #333; margin: 10px; box-sizing: border-box; }
Ad Placeholder - Middle (728×90)

Understanding the CSS Box Model

What Is the CSS Box Model?

The CSS box model is the cornerstone of web layout. When a browser renders any HTML element, it treats it as a rectangular box composed of four nested layers. From the outside in, these are: Margin (space outside the element), Border (the visible edge), Padding (space between content and border), and Content (the actual content area). Understanding how these layers interact is essential for every frontend developer who wants precise control over element sizing and spacing.

In the standard box model (content-box), when you write width: 300px, that 300 pixels applies only to the content area. If you then add padding: 20px and border: 2px, the element's actual footprint becomes 300 + 20×2 + 2×2 = 344px. This additive behavior often makes layout calculations tedious and error-prone, which is why many developers prefer border-box.

This tool provides an interactive visualization of these four layers, calculates total dimensions in real time, and generates copy-ready CSS code so you can understand the differences between box-sizing modes at a glance.

content-box vs border-box

content-box (default): width and height apply only to the content area. Padding and border are added on top of these values, increasing the total size. This is the W3C standard model, but it makes practical development more complex because you must mentally account for padding and border in every dimension calculation.

border-box (recommended): width and height include the content, padding, and border combined. When you write width: 300px, the element takes up exactly 300px (not counting margin). Padding and border are "absorbed" from the inside, so the element never exceeds its declared size. This is widely regarded as the best practice for modern web development.

Nearly all modern CSS frameworks — Bootstrap, Tailwind CSS, Bulma, and many more — set box-sizing: border-box globally. Google's own style guides and MDN documentation also recommend this approach for predictable layouts.

How to Use This Tool

Step 1: Set the base dimensions. Enter the desired width and height in the Content Width and Content Height fields. If you're using border-box, these will typically be the final displayed dimensions (excluding margin).

Step 2: Adjust padding and border. Set the top, right, bottom, and left values for padding and border. The visualization updates instantly so you can see exactly how padding and border affect the layout.

Step 3: Set margins. Enter margin values for all four directions. Note that margin is shown with a dashed outline in the visualization since it does not render the element's background color, but rather represents the gap between this element and neighboring elements.

Step 4: Switch box-sizing modes. Use the dropdown to toggle between content-box and border-box. Observe how the total width and height change. In border-box mode, adding padding and border does not increase the element's footprint beyond the declared dimensions.

Step 5: Copy the CSS code. When you're satisfied with the configuration, click the "Copy CSS Code" button to copy the generated styles directly into your stylesheet.

Real-World Use Cases

Responsive Layout Debugging: When building responsive websites, precise control over card, button, and form component sizes is critical. Use this tool to calculate how different padding and border combinations affect the actual space an element occupies, preventing overflow and layout shifts.

Framework Customization: CSS frameworks come with preset padding and border values. This tool helps you visualize how those presets affect total dimensions, making it easier to override and customize styles without breaking the layout grid.

Teaching and Learning: For beginners, the box model is the gateway to understanding web layout. The interactive visualization makes abstract concepts tangible, helping learners build correct spatial intuition from day one.

Design Handoff: Frontend engineers often need to translate Figma or Sketch designs into pixel-perfect HTML/CSS. This tool helps you quickly derive the correct width, height, padding, and margin values to match the designer's intent exactly.

Advanced Concepts

Margin Collapse: When two block-level elements meet vertically, their margins do not add up. Instead, the larger margin wins. For example, if one element has margin-bottom: 20px and the next has margin-top: 30px, the gap between them is 30px, not 50px. Margin collapse only happens in the vertical direction; horizontal margins never collapse.

Global border-box Reset: The industry-standard approach is to set *, *::before, *::after { box-sizing: border-box; } in your CSS. This ensures every element uses the border-box model, eliminating inconsistencies between components that might otherwise use different sizing modes.

CSS Logical Properties: Modern CSS introduces logical properties like inline-start/end and block-start/end to replace physical directions (top, right, bottom, left). These properties are especially valuable for multilingual layouts with right-to-left (RTL) scripts such as Arabic and Hebrew, allowing the same CSS to work across languages without directional overrides.

What is the CSS box model?

The CSS box model is the foundational layout concept in web design. Every HTML element is treated as a rectangular box consisting of four layers from outside to inside: Margin (space outside the element), Border (the edge of the element), Padding (space between content and border), and Content (the actual content area). The total width and height calculation depends on the box-sizing property.

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

content-box is the CSS default where width and height apply only to the content area, and padding and border are added on top. border-box includes padding and border within the specified width and height, so the element's actual footprint matches the declared dimensions. This makes border-box more intuitive and is the preferred choice for modern web development.

Why should I use border-box?

You should use border-box for three key reasons: 1) When you set width: 300px, the element actually takes up 300px, not more due to padding and border. 2) It simplifies responsive layout calculations because element sizes stay predictable. 3) It prevents accidental layout breaks when padding or border values change. Most CSS frameworks like Bootstrap and Tailwind use border-box globally.

Does margin affect the element's background color?

No. Margin is the transparent space outside the element and is not part of the element itself. Only the content, padding, and border areas display the element's background color. Margin exists solely to create spacing between adjacent elements in the document flow.

How do I calculate total size in content-box mode?

In content-box mode, total width equals width + padding-left + padding-right + border-left-width + border-right-width. Total height equals height + padding-top + padding-bottom + border-top-width + border-bottom-width. Margin is not counted as part of the element itself, but it does affect the element's position in the document flow.

Can padding be negative?

No. Padding values must be zero or positive. Unlike margin, which can be negative (useful for overlapping elements or pulling items closer together), padding cannot be negative. Browsers will ignore or treat negative padding values as invalid.

Does border width affect the clickable area?

Yes. The border area is part of the element and responds to mouse events such as click and hover. If you want to increase the clickable area without showing a visible border, you can use a transparent border (e.g., border: 10px solid transparent) or rely on padding instead.

Do inline and block elements use the same box model?

Not exactly. Block-level elements (like div and p) fully support width, height, padding, border, and margin, making the complete box model applicable. Inline elements (like span and a) ignore width and height (size is determined by content), and while vertical margin and padding can be set, they do not push surrounding elements away in the layout flow.

Ad Placeholder - Bottom (728×90)