CSS Inline-block
display: inline-block combines the best of two worlds. Like inline, the element sits inside a line of text next to other content. Like block, it respects width, height, and vertical padding and margin.
Comparing inline, inline-block and block
inline |
inline-block |
block |
|
|---|---|---|---|
| Sits next to other content | Yes | Yes | No |
width / height respected |
No | Yes | Yes |
| Vertical padding/margin pushes lines | No | Yes | Yes |
| Default width | Content | Content | Full container |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; line-height: 1.6; max-width: 420px; }
span { width: 90px; height: 40px; padding: 10px; margin: 6px; color: white; }
.i { display: inline; background: #ef5350; }
.ib { display: inline-block; background: #42a5f5; }
.b { display: block; background: #66bb6a; }
</style>
</head>
<body>
<p>Text <span class="i">inline</span> text — width and height are ignored, padding overlaps lines.</p>
<p>Text <span class="ib">inline-block</span> text — sized, and the line makes room.</p>
<p>Text <span class="b">block</span> text — on its own line.</p>
</body>
</html>Use case: horizontal navigation
Before Flexbox, inline-block was the standard way to put list items side by side. It's still handy for small inline groups such as tags, pills and buttons.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
ul { list-style: none; padding: 0; background: #37474f; margin: 0 0 16px; }
li { display: inline-block; }
li a { display: inline-block; padding: 14px 16px; color: white; text-decoration: none; }
li a:hover { background: #546e7a; }
.tag { display: inline-block; padding: 4px 10px; margin: 3px; border-radius: 999px; background: #e1bee7; color: #4a148c; font-size: 14px; }
</style>
</head>
<body>
<ul><li><a href="#">Home</a></li><li><a href="#">Shop</a></li><li><a href="#">Contact</a></li></ul>
<span class="tag">#css</span><span class="tag">#layout</span><span class="tag">#inline-block</span><span class="tag">#web</span>
</body>
</html>Use case: a grid of boxes
Inline-block boxes wrap naturally to the next line when the row is full. That makes a simple, responsive tile layout.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.card { display: inline-block; width: 110px; height: 80px; margin: 4px; padding: 8px; vertical-align: top;
border-radius: 8px; background: #fff3e0; border: 2px solid #ffa726; box-sizing: border-box; }
.tall { height: 110px; }
</style>
</head>
<body>
<div class="card">Card 1</div><div class="card tall">Card 2 (taller)</div><div class="card">Card 3</div>
<div class="card">Card 4</div><div class="card">Card 5</div>
</body>
</html>Tip: Inline-block elements align on the text baseline by default, which can make boxes of different heights look uneven. Add
vertical-align: top(ormiddle) to fix it — try removing it above.
The whitespace gap
Because inline-block elements are treated like words, the spaces and line breaks between them in your HTML show up as small gaps (about 4px). Ways to deal with it:
- Accept it, and size with that in mind.
- Remove whitespace between tags in the HTML.
- Set
font-size: 0on the parent and restore it on the children. - Use Flexbox with
gapinstead (usually the best answer).
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; }
.row div { display: inline-block; width: 60px; height: 40px; background: #26a69a; }
.row div:nth-child(even) { background: #80cbc4; }
.nogap { font-size: 0; }
.flex { display: flex; }
.flex div { width: 60px; height: 40px; background: #7e57c2; }
.flex div:nth-child(even) { background: #b39ddb; }
</style>
</head>
<body>
<p>Gaps from whitespace:</p>
<div class="row"><div></div> <div></div> <div></div> <div></div></div>
<p>font-size: 0 trick:</p>
<div class="row nogap"><div></div> <div></div> <div></div> <div></div></div>
<p>Flexbox — no gaps:</p>
<div class="flex"><div></div> <div></div> <div></div> <div></div></div>
</body>
</html>Test yourself
Which display value lets an element sit in a line of text AND have a set width and height?
- inline
- inline-block
- block
- none
Why do small gaps appear between inline-block elements?
- Default margins
- Whitespace in the HTML is rendered like spaces between words
- A browser bug with padding
- border-collapse
Inline-block boxes of different heights don't line up at the top. What fixes it?
- float: top
- vertical-align: top
- text-align: top
- align-items: top