Sub topic 5
Width, height, margin, and padding on block and inline boxes
This chapter assumes you know the CSS box model (content, padding, border, margin), can write CSS rules with selectors and properties, and know basic HTML elements. It covers which box-model properties take effect on which kind of box. Most "my width is ignored" bugs come from not knowing the display type.
The core rule
Every element generates a box, and the box's display type decides which box-model properties the browser applies. The two types you meet first behave very differently:
- A block-level box starts on a new line, stretches across its container by default, and honors all box-model properties.
- An inline box flows inside a line of text, wraps across lines like words, and ignores some properties completely.
The table summarizes how each property behaves. The inline-block column is covered in the next chapter, and it is included here so you can compare.
| Property | Block | Inline | Inline-block |
|---|---|---|---|
| `width`, `height` | Applied | Ignored | Applied |
| Horizontal margin (left, right) | Applied | Applied | Applied |
| Vertical margin (top, bottom) | Applied | Has no effect on layout | Applied |
| Horizontal padding | Applied | Applied | Applied |
| Vertical padding | Applied | Painted, but does not push other content | Applied |
| Default width | Fills the container | Shrinks to its content | Shrinks to its content |
Block boxes in detail
A block box takes its width from its container unless you set one. If width is auto, the content box grows to fill whatever space remains after padding, border, and margin are subtracted.
<div class="card">Block one</div>
<div class="card">Block two</div>.card {
width: 300px;
height: 80px;
padding: 1rem;
margin: 1rem auto;
border: 2px solid #345;
}Three details are worth knowing.
widthsets the content box by default. With the defaultbox-sizing: content-box, the.cardabove is 300px of content plus 32px padding on each side plus 4px of border on each side, so the visible box is 372px wide. Settingbox-sizing: border-boxmakeswidthinclude padding and border.margin: autocenters a block horizontally only when the block has a set width smaller than its container. Withwidth: autothe block already fills the container, so there is nothing to center.- Vertical margins collapse. When two block boxes sit one above the other, the gap between them is the larger of the two margins, not the sum. Here the gap between the boxes is 1rem, not 2rem.
div {
display: block;
margin: 1rem 0;
}With this rule, a stack of div elements has 1rem between each pair, because the bottom margin of one and the top margin of the next collapse into a single margin.
Inline boxes in detail
An inline box sits within a line box. Its size comes entirely from its content, such as the text and any nested inline elements. The browser does not let you resize it with width or height.
span.tag {
display: inline;
width: 200px;
height: 50px;
margin: 20px;
padding: 10px;
}For this rule, the browser applies the properties as follows:
width: 200pxandheight: 50pxare ignored. The box is only as wide and tall as its text.margin: 20pxworks horizontally. It adds 20px of space before and after the box. The top and bottom margins do nothing, so the line spacing stays the same.padding: 10pxworks on all four sides, but the vertical part behaves differently from horizontal padding. The padding is painted, so a background color extends 10px above and below the text. It does not change the height of the line, so it can overlap the lines above and below.
<p>
Some text with a <span class="tag">highlighted tag</span> in the middle of
a paragraph that is long enough to wrap onto more than one line.
</p>If you add a background color to .tag, you will see the tinted area grow upward and downward past the text while the surrounding lines do not move.
Inline boxes that wrap
An inline box can break across lines. When it does, it becomes several line fragments. Horizontal padding, margin, and border apply only at the start of the first fragment and the end of the last fragment, not at every line break. This is the default behavior of box-decoration-break: slice.
mark.note {
background: #ffe9a8;
padding: 0 0.5rem;
margin: 0 0.25rem;
}If mark.note wraps, the left padding appears only on the first line and the right padding only on the last line. Setting box-decoration-break: clone makes each fragment get its own padding and border.
Replaced inline elements are different
Elements such as img, video, and input are called replaced elements, because their content comes from outside the CSS formatting model. They are inline by default, yet they accept width and height. So the rule "inline boxes ignore width and height" applies to non-replaced inline boxes, such as span, a, em, and strong.
img {
width: 120px;
height: 80px;
}This is why an img can be sized without changing its display. Because it is inline, it also sits on the text baseline, which can leave a few pixels of extra space under it. That baseline issue is covered in a later chapter.
Why the browser ignores vertical sizing on inline boxes
The line box height is computed from the line-height of the text inside it, not from the inline box's own dimensions. An inline box has a content area based on the font, and the line height controls how tall the line is. If inline boxes could change the line height with height or vertical margin, one span could push apart lines of a paragraph, which would conflict with how text is laid out.
The diagram below shows how the browser decides which properties to apply.
Common errors
Setting width on a span or a link
A span or a with a width shows no change. The fix is to change its display type, either to block if it should sit on its own line, or to inline-block if it should stay in the line.
nav a {
display: inline-block;
width: 8rem;
padding: 0.5rem 1rem;
text-align: center;
}Using vertical margin to space inline elements
Top and bottom margin on inline elements has no effect on layout. To add space between lines, use line-height on the parent. To add space around an element, make it inline-block or block.
Expecting vertical padding to make room
Vertical padding on an inline element makes it look bigger, but the neighboring lines do not move. A button-like link with padding: 1rem and no display change can overlap the text above it. Use inline-block to have the padding take up space.
Forgetting that block width is automatic
Setting width: 100% on a block that also has padding and a border, with content-box sizing, makes the box wider than its container. The fix is either to leave width as auto, which fills the container and keeps padding inside, or to use box-sizing: border-box.
.panel {
width: 100%;
padding: 1rem;
border: 1px solid #999;
box-sizing: border-box;
}Quick checks with a test page
You can see these rules for yourself with a single file. Save it as box-test.html and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Box model by display type</title>
<style>
body { font: 16px/1.5 sans-serif; max-width: 40rem; margin: 2rem auto; }
.block-test {
background: #cde;
width: 50%;
padding: 1rem;
margin: 1rem 0;
}
.inline-test {
background: #fdc;
width: 400px;
height: 100px;
padding: 1rem;
margin: 1rem;
}
</style>
</head>
<body>
<div class="block-test">A block with width 50%.</div>
<p>
Text before
<span class="inline-test">an inline span</span>
and text after, which stays on the same line.
</p>
</body>
</html>In the result, the blue block is half the container's width and has space above and below it. The pink span is only as wide as its text, with 1rem of space to its left and right. The 400px width, 100px height, and vertical margin make no difference, and the vertical padding tints the area around the text without moving the lines.
Summary
- Block boxes honor
width,height, margin, and padding on all sides. - Non-replaced inline boxes ignore
widthandheight, ignore vertical margin for layout, and apply horizontal margin and padding. - Vertical padding on an inline box is painted but does not change line height.
- Replaced elements such as
imgare inline but acceptwidthandheight. - When you need sizing and vertical spacing on something that stays in the text line, use
inline-block.
Exercise
A link has the rule a.btn { width: 10rem; margin-top: 2rem; padding: 1rem; background: gold; } and sits inside a paragraph. The width has no effect, the paragraph's lines do not move, and the gold background overlaps the line above. Explain why, and give a one-line fix that keeps the link in the line of text.
Show answer
The a element is a non-replaced inline box. Inline boxes ignore width and vertical margin, and their vertical padding is painted but does not change line height, which is why the gold background overlaps the neighboring line. Adding display: inline-block; to a.btn keeps it in the line and makes the width, top margin, and vertical padding all take effect.
Exercise
A div has width: 100%, padding: 20px, and border: 5px solid with the default box sizing. It is inside a 600px container. How wide is its rendered box, and what two changes could stop it overflowing?
Show answer
The rendered width is 650px: 600px of content plus 40px of horizontal padding plus 10px of horizontal border. It overflows by 50px. You can set box-sizing: border-box so the 100% width includes padding and border, or remove the width declaration so the block's automatic width fills the container and keeps padding and border inside it.
Exercise
Two sibling block elements have margin-bottom: 30px on the first and margin-top: 20px on the second. How large is the gap between them, and why?
Show answer
The gap is 30px. Adjacent vertical margins of block boxes collapse into a single margin equal to the larger of the two values, not their sum.