liveBook · Manning

Inline-block

As explained in CSS in Depth, Second Edition

Inline-block is a CSS display value that makes an element participate as an inline-level box while retaining block-container behavior.

inline-block makes an element participate in inline flow while retaining block-container behavior, so padding and other dimensions can affect its layout differently from a purely inline element. section 3.2

Use inline-block when the layout needs both inline placement and box-like behavior:

  • Let an element’s padding contribute appropriately to its rendered height and surrounding layout. section 3.2
  • Prevent margins from collapsing through the outside of a container. section 3.5.3
  • Choose whole-element wrapping when an inline item’s contents should not split across lines. section 12.1.3

Tags and wrapping

A tag list can use inline items for natural whitespace, but inline-block changes the trade-off: each tag remains a single wrapping unit, and the line grows to accommodate its padding. Add horizontal and vertical margins when spacing must be explicit. section 12.1.3

Listing 12.5 Styling the tags
@layer modules {
  .tag-list {
    list-style: none;               #1
    padding-inline-start: unset;    #1
  }
  .tag-list > li {
    display: inline;
    padding: 0.3rem 0.5rem;
    font-size: 0.8rem;
    border-radius: 0.2rem;
    background-color: var(--light-gray);
    line-height: 2.6;        #2
  }
}

Compare the tag-list listing’s display: inline rule with inline-block behavior: inline content can split when wrapping, while an inline-block remains intact and can enlarge the line. section 12.1.3

How CSS in Depth, Second Edition covers it

How deep it goes
Explains padding, line growth, wrapping, and margin collapsing, but does not develop a complete implementation pattern.
Examples
Uses padded tag lists and container margin-collapsing cases to contrast inline, inline-block, and flex layouts.
What it assumes
Assumes familiarity with CSS display, padding, margins, line-height, and basic document flow.
What you can do after
Apply inline-block when elements must wrap as units, grow their line, or prevent outside margin collapse.
How it connects
Connects inline-block with the box model, inline elements, line-height, flexbox, and margin collapsing.

Also explained in

Compare how 4 books explain inline-block →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.