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) and can write rules with selectors. It also assumes you have read the previous chapters on display: block and display: inline. Here you will see how the same box-model properties behave differently depending on the display type.
Block boxes use every property
A block box takes part in the box model in full:
- Width:
width: autostretches the box to fill its container. Settingwidthoverrides that. - Height:
height: autogrows with the content. Settingheightfixes it. - Margin and padding: all four sides work, and they push neighbouring boxes away.
Vertical margins between adjacent block siblings collapse. If one block has margin-bottom: 20px and the next has margin-top: 30px, the gap is 30px, not 50px.
Inline boxes ignore some properties
An inline box flows inside a line of text. Its size comes from its content, so the browser skips several properties. Try this rule on a span:
span.tag {
display: inline;
width: 200px;
height: 50px;
margin: 20px;
padding: 10px;
}The result:
widthandheightare ignored.- Left and right margin work. They add space beside the box.
- Top and bottom margin do nothing. Line spacing stays the same.
- Padding applies on all four sides, but vertical padding only changes where the background and border are painted. It does not change the line height, so it can overlap the lines above and below.
| Property | Block | Inline |
|---|---|---|
| `width`, `height` | Applied | Ignored |
| Left and right margin | Applied | Applied |
| Top and bottom margin | Applied (adjacent blocks collapse) | Ignored |
| Left and right padding | Applied | Applied |
| Top and bottom padding | Applied, affects layout | Painted, but does not affect layout |
A runnable comparison
Save this as one HTML file and open it in a browser. The block gets its size. The inline span shows the same size rules being ignored.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Block vs inline sizing</title>
<style>
.box {
width: 200px;
height: 50px;
margin: 20px;
padding: 10px;
background: lightblue;
}
div.box { display: block; }
span.box { display: inline; }
</style>
</head>
<body>
<p>Text before.</p>
<div class="box">Block</div>
<p>Text before the span <span class="box">Inline</span> and text after.</p>
</body>
</html>The div is 200px wide and 50px tall, plus padding. The span is only as wide as the word "Inline" plus 10px of padding on each side and 20px of margin left and right.
Common errors
- Setting
widthon aspanoraand seeing nothing happen. The element is still inline. Change itsdisplay, or wrap it differently. - Adding
margin-topto an inline element to space lines apart. Useline-heighton the parent, or change the display type. - Expecting vertical padding on an inline link to push other content away. It only paints outward and can overlap neighbours.
- Adding two block margins and expecting the sum. Adjacent vertical margins collapse to the larger one.
The fix for the first three is usually display: inline-block, which the next chapter covers.
Exercise
A span has display: inline, width: 120px, margin: 15px, and padding: 5px. Which of these values change its layout: width, left and right margin, top and bottom margin, left and right padding?
Show answer
Left and right margin and left and right padding change the layout. The width is ignored on an inline box, and the top and bottom margin has no effect. Top and bottom padding is also painted, but it does not change line spacing.
Exercise
Two adjacent div elements are blocks. The first has margin-bottom: 20px and the second has margin-top: 30px. How large is the gap between them, and why?
Show answer
The gap is 30px. Adjacent vertical margins between block siblings collapse to the larger of the two values instead of adding.