Block vs inline vs inline-block?
Block elements start on a new line and stretch to fill their container's width, honoring width, height, margin and padding. Inline elements flow inside text, take only the width of their content, and ignore width, height and vertical margins. Inline-block elements flow inline like text but behave like blocks inside, respecting width, height and all spacing.
Comparison
| block | inline | inline-block | |
|---|---|---|---|
| New line | Yes | No | No |
| Default width | Fills container | Fits content | Fits content |
width / height | Applied | Ignored | Applied |
| Vertical margin | Applied | Ignored | Applied |
| Vertical padding | Applied | Painted, but does not push lines apart | Applied |
| Examples | div, p, h1, section | span, a, strong, em | button, input, img (replaced elements behave similarly) |
Practical uses
- inline-block: badges, buttons and nav links that need padding and a fixed height while sitting in a line of text.
- Inline-block elements are aligned with
vertical-alignand are separated by whitespace in the HTML source, which creates small, sometimes unwanted gaps.
Modern perspective
For laying out a row of items, Flexbox with gap replaced most inline-block layouts: no whitespace gaps, easy alignment and wrapping. Inline-block is still handy for small inline components.
Interview tip
A classic gotcha: setting width on a <span> or <a> does nothing until you change its display.
Code example
1<style>2 body { font-family: system-ui; }3 .x { background: #dbeafe; border: 2px solid #0b1020; padding: 8px; margin: 12px; width: 140px; height: 40px; }4 .block { display: block; }5 .inline { display: inline; } /* width/height/vertical margin ignored */6 .inline-block { display: inline-block; }7</style>8<p>Text before9 <span class="x block">block</span>10 text after.</p>11<p>Text before12 <span class="x inline">inline</span>13 text after, padding overlaps lines above and below.</p>14<p>Text before15 <span class="x inline-block">inline-block</span>16 text after, sits in the line and keeps its size.</p>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.