Sub topic 6
display: inline-block
This chapter assumes you can write CSS rules with selectors and properties, and that you know the box model (content, padding, border, margin). It also assumes you have read the chapters on display: block and display: inline. Here you will see how inline-block combines parts of both, where it helps, and where it causes trouble.
What inline-block means
Every element generates a box with two sides: how the box behaves toward its siblings (outer behavior) and how it lays out its own children (inner behavior). display: inline-block sets them as follows:
- Outer behavior is inline-level. The box sits in a line of text next to words and other inline content. It does not start a new line.
- Inner behavior is a block container. The box lays out its contents the way a block does. It accepts
width,height, and all four margins and paddings.
The browser treats an inline-block as one unit, like a single large character in the line. Specifications call this an atomic inline-level box.
| Property | `block` | `inline` | `inline-block` |
|---|---|---|---|
| Starts a new line | Yes | No | No |
| Default width | Fills the container | Content only | Content only (shrink-to-fit) |
| `width` and `height` | Applied | Ignored | Applied |
| Vertical margin | Applied | No effect on line spacing | Applied |
| Horizontal margin and padding | Applied | Applied | Applied |
| Can break across lines | Not applicable | Yes | No |
| Sits beside siblings | Only with floats or other layout modes | Yes | Yes |
Sizing: the badge example
An inline element ignores width and height. An inline-block respects them:
.badge {
display: inline-block;
width: 6rem;
padding: 0.5rem;
margin: 0.5rem;
text-align: center;
}Here is the markup and a style block that make it visible and runnable:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Badges</title>
<style>
.badge {
display: inline-block;
width: 6rem;
padding: 0.5rem;
margin: 0.5rem;
text-align: center;
background: #e0ecff;
border: 1px solid #5b8def;
}
</style>
</head>
<body>
<p>
Status:
<span class="badge">Draft</span>
<span class="badge">Review</span>
<span class="badge">Published</span>
</p>
</body>
</html>Three things happen:
- The badges sit in the same line as the word "Status:".
- Each badge is exactly
6remwide for its content, plus padding and border, becausebox-sizingis the defaultcontent-box. Addbox-sizing: border-boxif you wantwidthto include padding and border. - The vertical margin of
0.5rempushes neighboring lines away. On a plainspanit would not.
If you leave width as auto, the browser uses a shrink-to-fit width. It takes the content's preferred width (all text on one line), limited by the space available in the container, but never narrower than the content's smallest possible width (the longest word). A block fills its container by default. An inline-block hugs its content.
Atomic behavior and line wrapping
A long inline element can break across lines, so one span can occupy the end of one line and the start of the next. An inline-block cannot be split. If it does not fit on the current line, the whole box moves to the next line.
<style>
.chip {
display: inline-block;
padding: 0.25rem 0.75rem;
margin: 0.25rem;
background: #eee;
border-radius: 1rem;
}
</style>
<div style="width: 18rem">
<span class="chip">CSS display</span>
<span class="chip">inline-block</span>
<span class="chip">line wrapping</span>
<span class="chip">atomic boxes</span>
</div>Each chip wraps as a whole. With display: inline, a chip near the edge would split in half, and its padding and background would be cut at the break.
A practical use: navigation links
Links are inline by default. Padding on an inline a extends its background, but vertical padding does not affect line spacing, so neighboring lines or elements can overlap. Making the links inline-block gives each one a full padded click area, and they stay in one row:
nav a {
display: inline-block;
padding: 0.5rem 1rem;
}You do not need floats and you do not need to turn each link into a block. The padding is part of the box's real height, so the line grows to fit it.
It contains its content
An inline-block creates a new block formatting context. This is a layout region where its children are isolated from the outside. Two consequences matter in practice:
- Floats stay inside. If a child is floated, the inline-block still grows to enclose it. A plain block does not.
- Margins do not collapse through it. A child's top margin stays inside the inline-block and does not escape to the parent. Vertical margins of adjacent inline-blocks never collapse together either, unlike adjacent blocks.
.card {
display: inline-block;
width: 12rem;
margin: 1rem;
vertical-align: top;
}Note vertical-align: top here. It is explained in the next section's warning and in the baseline chapter.
Common errors
Misaligned rows
Inline-blocks line up on their baseline by default. For a box with text, the baseline is the baseline of its last line of text. If the box has overflow set to anything other than visible, or has no in-flow text, the baseline becomes the bottom margin edge. Two cards of different heights, or one card with text and another that is empty, can therefore sit at different vertical positions.
Gaps between items
Browsers treat whitespace between inline-level boxes as a space character. If the markup puts your inline-blocks on separate lines, a gap about the width of one space appears between them. The next chapter covers fixes. One option is to make the parent a flex container, which ignores that whitespace:
.row {
display: flex;
gap: 0;
list-style: none;
padding: 0;
}That gap also breaks width math. Four inline-blocks at width: 25% do not fit on one line, because the three spaces add extra width. The fourth wraps to the next line.
Alignment through the parent
Because inline-blocks are inline-level, the parent's text-align controls their horizontal position. text-align: center on the container centers a row of inline-blocks. Setting margin: 0 auto on an inline-block does nothing, since auto margins center only blocks with a set width.
<style>
.toolbar { text-align: center; }
.toolbar button { display: inline-block; width: 7rem; }
</style>
<div class="toolbar">
<button>Save</button>
<button>Cancel</button>
</div>Line height and font size
An inline-block lives in a line box, so its height influences the line. A tall inline-block makes its line taller. The parent's line-height and font-size also leak into the inline-block's own text unless you reset them. The small space under an inline-block image is often the result: the line box reserves room for descenders below the baseline. Setting vertical-align: middle on the image, or display: block, removes it.
When to choose inline-block
Use inline-block when:
- A small component sits inside running text or a row and needs real width, height, or vertical spacing: badges, chips, icon buttons, tags.
- Links or buttons need a larger click area but should not each take a full line.
- You need an inline element that wraps as one unit.
Prefer another value when:
- You are building a full row or grid of components.
display: flexanddisplay: gridhandle spacing, alignment, and wrapping without whitespace or baseline problems. - The element should take its own line. Use
block. - You only need to style a few words of text. Use plain
inline.
Exercises
Exercise
A span has display: inline, width: 200px, height: 50px, and margin: 20px. Which of these values take effect, and what single change makes all of them take effect without moving the span to its own line?
Show answer
Only the horizontal margins (left and right 20px) take effect. The width and height are ignored, and the vertical margins do not change line spacing. Change the display to inline-block. The span stays in the line, and width, height, and vertical margins are applied.
Exercise
Four div elements are styled display: inline-block; width: 25%; inside a container, with each div on its own line in the HTML source. The fourth one wraps to a second row. Why does it wrap, and name one way to fix it.
Show answer
The whitespace between the elements in the markup is rendered as a space between each pair, which adds width beyond 100%. The fix is to make the container display: flex (the whitespace is ignored), or to remove the whitespace from the markup, or to reduce the widths slightly.
Exercise
Two inline-block cards sit in one row. One has several lines of text and the other has a single line. The shorter card appears lower than the taller one. What causes this and what property fixes it?
Show answer
Inline-blocks align on their baselines by default. The baseline of each card is the baseline of its last line of text, so cards of different heights line up at different tops. Setting vertical-align: top on the cards aligns their tops.