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

blockinlineinline-block
New lineYesNoNo
Default widthFills containerFits contentFits content
width / heightAppliedIgnoredApplied
Vertical marginAppliedIgnoredApplied
Vertical paddingAppliedPainted, but does not push lines apartApplied
Examplesdiv, p, h1, sectionspan, a, strong, embutton, 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-align and 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

html
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 before
9 <span class="x block">block</span>
10 text after.</p>
11<p>Text before
12 <span class="x inline">inline</span>
13 text after, padding overlaps lines above and below.</p>
14<p>Text before
15 <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.

Open in study app