CSSinline-block Deep Dive

inline-block

display: inline-block is a hybrid between block and inline. Elements flow horizontally with siblings (like inline), but respect width, height, and all margins like block elements. This is useful for buttons, badges, side-by-side boxes, and other components that need to flow inline while being sizeable.

inline-block behaviour

Property

Inline

Inline-block

Block

Width

Ignored

Works

Works

Height

Ignored

Works

Works

Margin all sides

Top/bottom ignored

All work

All work

Padding all sides

Top/bottom outside box

Works normally

Works normally

Stacking

Horizontal flow

Horizontal flow

Vertical stack

Use case

Text elements

Buttons, badges

Page structure

CSS
/* inline-block — flows inline but respects sizing */
.inline-block-element {
  display: inline-block;
  width: 150px;       /* works */
  height: 40px;       /* works */
  margin: 10px;       /* all margins work */
  padding: 10px;      /* all padding works */
  /* Flows horizontally with siblings, but sizeable like a block */
}

/* Example */
<div class="inline-block-element">Item 1</div>
<div class="inline-block-element">Item 2</div>
<div class="inline-block-element">Item 3</div>

/* Renders as */
[Item 1] [Item 2] [Item 3]

/* All on one line, each 150px wide */
Common uses for inline-block

CSS
/* Buttons in a row */
.button {
  display: inline-block;
  padding: 10px 20px;
  margin: 5px;
  background: #0066cc;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  /* Buttons flow horizontally, each with sizing */
}

<button class="button">Cancel</button>
<button class="button">Save</button>
<button class="button">Delete</button>

/* Renders as */
[Cancel] [Save] [Delete]

/* Badges or tags */
.badge {
  display: inline-block;
  padding: 4px 8px;
  background: #f44336;
  color: white;
  border-radius: 12px;
  font-size: 12px;
  margin-right: 5px;
}

<span class="badge">New</span>
<span class="badge">Featured</span>

/* Renders as */
[New] [Featured]

/* Grid of boxes without CSS Grid */
.card {
  display: inline-block;
  width: 300px;
  margin: 10px;
  padding: 20px;
  background: white;
  border: 1px solid #cccccc;
  border-radius: 8px;
  vertical-align: top;
}

/* Cards flow horizontally, wrap to next line if needed */
/* vertical-align: top prevents baseline misalignment */
Spacing and alignment

Inline-block elements have one quirk: whitespace between HTML tags becomes a space character in the layout. This can affect horizontal spacing.

CSS
/* HTML whitespace becomes a space */
<div class="inline-block">Box 1</div>
<div class="inline-block">Box 2</div>

/* The newline/space between </div> and <div> becomes a space in layout */
[Box 1] [Box 2]

/* Extra space appears between boxes */

/* Solutions */

/* Solution 1: No whitespace in HTML */
<div class="inline-block">Box 1</div><div class="inline-block">Box 2</div>

/* Solution 2: Comment out whitespace */
<div class="inline-block">Box 1</div><!--
--><div class="inline-block">Box 2</div>

/* Solution 3: Negative margin to remove space */
.inline-block {
  display: inline-block;
  margin-right: -4px;  /* remove space character */
}

/* Solution 4: Use flexbox instead (modern, recommended) */
.container {
  display: flex;
  gap: 10px;  /* explicit spacing */
}

.item {
  display: block;
  flex: 1;
}
Vertical alignment with inline-block

Inline-block elements align to the baseline by default (like inline text). Use vertical-align to change alignment.

CSS
/* Baseline alignment (default) */
.inline-block {
  display: inline-block;
  /* aligns to baseline of surrounding text */
}

<span>Text</span>
<div class="inline-block">Box</div>

/* Box bottom aligns with text baseline */

/* Vertical alignment options */
.inline-block {
  display: inline-block;
  vertical-align: top;    /* align to top of line */
  vertical-align: middle; /* align to middle of line */
  vertical-align: bottom; /* align to bottom of line */
}

/* Example: icons aligned with text */
.icon-button {
  display: inline-block;
  width: 24px;
  height: 24px;
  vertical-align: middle;
  /* Icon now centers with adjacent text */
}

<button>
  <img src="icon.svg" alt="icon" class="icon-button">
  Button text
</button>

/* Icon and text align nicely */
inline-block vs flexbox

Feature

Inline-block

Flexbox

Elements flow horizontally

Yes

Yes

Gap between items

Whitespace (tricky)

Gap property (clean)

Alignment

vertical-align (complex)

align-items (simple)

Distribution

Manual margins/padding

justify-content (automatic)

Wrapping

Natural text wrapping

flex-wrap property

Modern browsers

Supported

Better support for alignment

CSS
/* Same layout with inline-block */
.container {
  width: 100%;
}

.item {
  display: inline-block;
  width: 30%;
  margin-right: 3%;
  vertical-align: top;
}

/* Same layout with flexbox (modern, cleaner) */
.container {
  display: flex;
  gap: 20px;
}

.item {
  flex: 1;
}

/* Flexbox is cleaner and more predictable */
When to use inline-block vs alternatives

CSS
/* Use inline-block for: */
/* - Components that need to flow inline and be sized */
/* - Simple layouts without complex alignment needs */

.pill-button {
  display: inline-block;
  padding: 8px 16px;
  margin: 2px;
  border-radius: 20px;
}

/* Use flexbox for: */
/* - Components that need automatic spacing/alignment */
/* - Flexible layouts that adapt to content */

.flex-navigation {
  display: flex;
  justify-content: space-between;  /* automatic distribution */
  align-items: center;              /* automatic vertical centering */
}

/* Use grid for: */
/* - Two-dimensional layouts */
/* - Complex grid structures */

.dashboard {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

/* Modern recommendation: Use flexbox or grid instead of inline-block */
/* Inline-block is useful for backwards compatibility with older browsers */
Note
Modern CSS has better alternatives: Use `flexbox` for simple one-dimensional layouts with automatic spacing, and `grid` for two-dimensional layouts. Inline-block is still useful for legacy browsers, but flexbox is the modern choice for most inline-block use cases.
Whitespace matters with inline-block
HTML whitespace between inline-block elements becomes a space character in layout. This can cause alignment issues. Either remove whitespace from HTML, use comments, apply negative margins, or (better) switch to flexbox which handles spacing cleanly with the `gap` property.
Next
Understanding how to hide elements: [display: none vs visibility: hidden](/css/display-none-vs-visibility).