Box Model Visualizer
Interactive CSS box model visualizer with real-time editing of margin, border, padding, and content dimensions
Updated
What is the Box Model Visualizer?
The Box Model Visualizer is an interactive tool for understanding and debugging the CSS box model. Every HTML element is rendered as a rectangular box composed of four layers: content, padding, border, and margin. This tool provides real-time visualization of how these layers interact and affect the final dimensions of elements on your webpage.
Why Use This Tool?
- Learn CSS fundamentals — See exactly how padding, borders, and margins contribute to element sizing
- Debug layout issues — Identify why elements are larger or positioned differently than expected
- Compare box-sizing modes — Instantly switch between
content-boxandborder-boxto understand the difference - Generate CSS code — Copy ready-to-use CSS with your configured values
Key Features
- Live visual preview with color-coded layers (orange for margin, yellow for border, green for padding, blue for content)
- Individual side controls for margin and padding (top, right, bottom, left)
- Box-sizing toggle to compare standard vs. alternative box models
- Computed dimensions display showing total size, content size, and used space
- CSS code export with one-click copying
Whether you are a beginner learning CSS or an experienced developer troubleshooting complex layouts, this tool makes the invisible box model visible and intuitive.
How it works
The CSS box model describes how the browser calculates the space an element occupies. Every element consists of four nested boxes:
| Layer | Description | Visual Color |
|---|---|---|
| Content | The actual content area where text and images appear | Blue |
| Padding | Transparent space between content and border | Green |
| Border | A visible line surrounding the padding (if set) | Yellow |
| Margin | Transparent outer space separating the element from others | Orange |
Box-Sizing Modes
Content-Box (Default)
- The
widthandheightproperties apply only to the content area - Padding and border are added outside, increasing the total size
- Total width = width + padding-left + padding-right + border-left + border-right
- Total height = height + padding-top + padding-bottom + border-top + border-bottom
Border-Box
- The
widthandheightproperties apply to the content + padding + border - Padding and border are subtracted from inside, keeping the declared dimensions
- Content area shrinks to accommodate padding and border
- Total size equals the declared width/height (excluding margin)
Using the Visualizer
- Set dimensions — Enter width and height for your element
- Adjust spacing — Modify margin, border, and padding values
- Toggle box-sizing — Switch between content-box and border-box modes
- Observe changes — Watch the visual diagram and computed values update instantly
- Copy CSS — Export your configuration as ready-to-use CSS code
The visualizer displays both the nested box diagram (showing each layer) and computed statistics (total size, content size, spacing breakdown) to give you complete visibility into how your element will render.
Examples
Standard Content Box
Default box-sizing with 200x150 content, 20px padding, 5px border, and 30px margin
Border Box Layout
Border-box sizing with fixed total dimensions including padding and border
Asymmetric Spacing
Different values for each side of the box model
Frequently asked questions
What is the CSS box model?
What is the CSS box model?
The CSS box model is a fundamental concept that defines how HTML elements are rendered as rectangular boxes. Every element consists of four concentric layers: the content (where text and images appear), padding (space between content and border), border (the visible edge), and margin (space outside the border that separates elements). Understanding how these layers interact is essential for creating predictable layouts.
What is the difference between content-box and border-box?
What is the difference between content-box and border-box?
Content-box (the default) means width and height apply only to the content area—padding and border add to the total size. For example, a 200px width with 20px padding and 5px border renders as 250px total. Border-box means width and height include content, padding, and border—the content area shrinks to accommodate the extra space. That same 200px width with border-box stays 200px total, with content reduced to 150px. Border-box is generally preferred for easier layout calculations.
Why does my element take up more space than I specified?
Why does my element take up more space than I specified?
This typically happens with content-box sizing (the browser default). If you set width: 300px but also add padding: 20px and border: 5px, the element actually occupies 350px horizontally (300 + 20 + 20 + 5 + 5). Switch to box-sizing: border-box to make the declared dimensions include padding and border, or reduce your content dimensions to account for the extra space.
How do margins affect the total size of an element?
How do margins affect the total size of an element?
Margins are not included in an element's declared dimensions with either box-sizing mode. However, margins do contribute to the space the element occupies in the document flow and affect positioning. Collapsing margins (where adjacent vertical margins combine rather than add) can also cause unexpected spacing. The visualizer shows both the element box dimensions and the margin-included outer dimensions separately.
Can I set different values for each side of the box?
Can I set different values for each side of the box?
Yes. The visualizer supports individual side controls for margin and padding (top, right, bottom, left). This allows you to create asymmetric spacing, such as larger bottom margins for vertical rhythm or different horizontal padding for visual balance. Border width can also be set per-side, though the tool defaults to uniform borders for clarity.
When should I use border-box instead of content-box?
When should I use border-box instead of content-box?
Use border-box when you want predictable sizing, especially in responsive layouts. It simplifies mental math—you declare 300px width and the element stays 300px regardless of padding changes. Content-box is useful when you need the content area to remain fixed (like an image container) while padding adjusts around it. Most modern CSS frameworks default to border-box for this reason.
Does padding affect background color or images?
Does padding affect background color or images?
Yes. The padding area extends the background of an element. If you set a background-color or background-image, it fills both the content box and padding box. The border sits outside this background, and the margin is always transparent. This is why increasing padding makes colored backgrounds appear larger without changing the content dimensions.
How can I see the box model in browser DevTools?
How can I see the box model in browser DevTools?
Most browsers show the box model in the Elements panel of DevTools. In Chrome/Edge, select an element and look for the "Computed" tab or the box model diagram in the "Styles" sidebar. Firefox has a similar visualization in the "Layout" panel. These tools show the numeric values for each layer, but our visualizer lets you experiment with values interactively and see changes in real-time before applying them to your code.
Why do margins sometimes collapse?
Why do margins sometimes collapse?
Margin collapsing occurs when two adjacent block-level elements have vertical margins that touch. Instead of adding (20px bottom + 30px top = 50px), the larger value wins (30px). This also happens between a parent and child if there is no padding or border separating them. Collapsing does not occur for horizontal margins, floated elements, absolutely positioned elements, or with flex/grid items.
Can I export the CSS from this tool?
Can I export the CSS from this tool?
Yes. The visualizer generates ready-to-use CSS based on your configuration. Click the Copy CSS button to copy the complete box model declaration to your clipboard, including width, height, box-sizing, margin, padding, and border properties formatted with proper syntax. Paste directly into your stylesheet or inline styles.
Related tools
More utilities you might find handy.
CSS Specificity Calculator
Instantly calculate the specificity score of any CSS selector, understand its impact on styling, and resolve cascade conflicts with ease.
Flexbox Visual Playground
Interactive CSS flexbox playground with live preview, visual guides, and preset layouts for learning and prototyping
CSS Animation Generator
Generate CSS keyframe animation code