Sub topic 8
How display type changes box behaviour
This chapter assumes you know how content, padding, border, and margin combine, and that you can write CSS rules with selectors and declarations. It covers how the display property changes which of those layers an element honours.
Block boxes
A block-level box starts on a new line and, with width: auto, stretches to fill its container. It accepts width, height, and margin and padding on all four sides. Elements such as div, p, h1, and section are block by default.
Inline boxes
An inline box flows within a line of text and does not start a new line. Elements such as span, a, and strong are inline by default. Inline boxes treat the box model differently:
widthandheighthave no effect.- Horizontal margin and padding push neighbouring content aside.
- Vertical margin has no effect.
- Vertical padding and borders are drawn, but they do not change the line's height. They can overlap the lines above and below.
Inline-block boxes
display: inline-block gives you an element that sits in a line like text but behaves like a block inside. It accepts width, height, and vertical margins, and those values do change the line's height.
Comparing the three
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Display types</title>
<style>
.demo {
width: 120px;
height: 40px;
margin: 20px;
padding: 10px;
background: #cde;
border: 2px solid #345;
}
.a { display: block; }
.b { display: inline; }
.c { display: inline-block; }
</style>
</head>
<body>
<span class="demo a">block</span>
<span class="demo b">inline</span>
<span class="demo c">inline-block</span>
</body>
</html>All three elements are span tags with identical rules. Only display differs, and that changes the result:
| Display | New line | Width and height | Vertical margin | Vertical padding affects layout |
|---|---|---|---|---|
| `block` | Yes | Applied | Applied | Yes |
| `inline` | No | Ignored | Ignored | No |
| `inline-block` | No | Applied | Applied | Yes |
Common errors
A frequent mistake is setting width on a span or a and seeing nothing happen. The element is inline, so the width is ignored. Change its display to inline-block or block.
Another is giving a link margin-top to push it down. Inline boxes ignore vertical margin, so the link does not move. Use inline-block, or put the spacing on a block parent.
A third is the small gap between adjacent inline-block elements. Whitespace between the tags in your HTML becomes a space character in the line. You can remove it by changing the markup, or by using flexbox on the parent instead.
Changing the display type
Setting display on an element overrides its default. This is common for navigation links:
nav a {
display: inline-block;
padding: 0.5rem 1rem;
margin-block: 0.25rem;
}With inline-block, the padding and vertical margin both enlarge the clickable area and affect the layout. With plain inline, the vertical padding would paint outside the line and overlap nearby content.
Exercise
A link (<a>) has width: 200px; margin-top: 30px; and neither value seems to work. What is the cause, and what is one fix?
Show answer
Links are inline by default, and inline boxes ignore width and vertical margin. Set display: inline-block (or block) on the link so both properties apply.
Exercise
Which display types start a new line before the element?
Show answer
Only block does. Both inline and inline-block stay in the surrounding line.