Sub topic 6
display: inline-block
This chapter assumes you know basic HTML, how to write CSS rules with selectors and properties, and the box model (content, padding, border, margin). You should also know how block and inline boxes behave.
What inline-block is
An inline-block element is a hybrid. On the outside, it behaves like an inline box. It sits in a line of text, shares the line with its neighbors, and wraps with the text. On the inside, it behaves like a block box. It accepts width, height, and vertical margin and padding, and those values affect the layout.
.badge {
display: inline-block;
width: 6rem;
padding: 0.5rem;
margin: 0.5rem;
text-align: center;
}Three .badge elements in a row sit side by side on one line. Each is 6rem wide plus padding. If the line runs out of room, the next badge wraps to a new line, as a word would.
Where it fits
A common use is navigation links. Plain inline links have a click area only as tall as the text. Making them inline-block gives each link a full padded target without forcing it onto its own line.
nav a {
display: inline-block;
padding: 0.5rem 1rem;
}The whitespace gap
Inline-level boxes are separated by whitespace in the HTML, the same way words are. A line break between two <li> elements renders as a space character, so you see a small gap between items. Consider this markup, where the li elements are inline-block:
<ul class="row">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>The gap is about the width of one space in the current font. You can't remove it with margin: 0, because it is not a margin. It is a text character.
One common fix is to make the container a flex container. Flex items are not inline-level, so no whitespace is rendered between them.
.row {
display: flex;
gap: 0;
list-style: none;
padding: 0;
}Other fixes exist. You can set font-size: 0 on the parent and restore the size on the children, or remove the whitespace in the HTML. Flexbox is usually the cleanest, and it also removes the need for inline-block in this case.
Baseline alignment
An inline-block box sits on the line's baseline by default. For a box with text inside, the baseline is the baseline of its last line of text. If the box has no in-flow line boxes, or has overflow other than visible, the baseline becomes the bottom margin edge.
In practice, this means two inline-blocks with different heights or content can look misaligned, and an empty or clipped box can sit higher or lower than expected. Set vertical-align: top (or middle) on the boxes to align them differently.
.badge {
vertical-align: top;
}Common errors
- Expecting
widthto work on aninlineelement and being surprised when nothing changes. Switch toinline-block. - Trying to close the gap between items with negative margins. This is fragile because the gap depends on the font. Use flexbox or
font-size: 0. - Forgetting that the box wraps. If a row of inline-blocks is wider than its container, the last items drop to a new line.
- Ignoring
vertical-alignwhen boxes of different heights look uneven.
Exercises
Exercise
You set display: inline-block on three li elements and see a small gap between them, even with margin: 0. What causes the gap, and what is one way to remove it?
Show answer
The gap comes from the whitespace (line breaks and indentation) between the li tags in the HTML, which renders as a space character between inline-level boxes. One fix is to make the parent ul a flex container with display: flex, since flex items do not render whitespace between them. Setting font-size: 0 on the parent also works.
Exercise
A span has width: 200px; height: 50px; padding: 10px; but its size does not change. Which single declaration makes the width and height apply while keeping the span in the line of text?
Show answer
display: inline-block;