Set a width on a span and nothing happens. Put two divs side by side and they stack instead. Both behaviours confuse people early on, and both come from the same source: elements have a default display behaviour, and the two main kinds behave in fundamentally different ways.
Block level elements claim a whole line
A block level element starts on a new line and takes up the full width available to it, however little content it holds. The next element goes underneath. Headings, paragraphs, divs, lists, list items, sections, articles, headers, footers and forms all behave this way by default.
<div>First</div> <div>Second</div> These stack vertically even though each contains one short word, because the box extends across the whole container regardless of its content. Block elements accept width and height, all four margins and all four padding values, and they can contain other block elements as well as inline ones.
Inline elements sit within the text
An inline element takes only the width its content needs and sits in the flow of the line, alongside the words around it. It does not force a break. Anchors, spans, strong, em, small, code, images and form inputs are inline.
<p>Read the <a href="/html/">HTML guide</a> for more.</p> The link sits inside the sentence rather than interrupting it, which is exactly what you want. Inline elements should normally contain only text or other inline elements.
The rules that catch everyone
Two properties behave differently on inline elements, and this is the source of most of the confusion:
- Width and height are ignored entirely. The box is sized by its content and nothing else.
- Top and bottom margins are ignored. Left and right margins work normally.
Padding is stranger still. It applies on all four sides and is painted, so a background colour will visibly extend above and below, but the vertical padding does not push neighbouring lines away. Instead it overlaps the lines above and below, which produces the effect of a highlighted link bleeding into the text around it.
None of this is a bug. An inline element is part of a line of text, and line height governs the spacing of lines. Allowing arbitrary vertical sizing on something in the middle of a sentence would make text layout impossible to reason about.
Inline block is the compromise
When you need something that flows with the text but still accepts dimensions, there is a third value.
.tag { display: inline-block; width: 120px; padding: 8px 12px; } An inline block element sits alongside its neighbours like an inline element, but internally behaves like a block, so width, height and vertical margins all apply. This is how buttons, badges and navigation items were built for years before Flexbox arrived, and it is still the simplest answer for a single element that needs to be sized without leaving the text flow.
Default display is only a starting point
The critical thing to understand is that these behaviours are CSS defaults applied by the browser, not properties baked into the elements themselves. Any element can be given any display value.
span { display: block; } li { display: inline; } nav ul { display: flex; } That last line is what modern layout actually looks like. Flexbox and Grid replace the default behaviour of a container entirely, and once a parent is a flex container its children stop following the block and inline rules and follow the flex rules instead. This is why a div inside a flex container no longer takes the full width and why several of them line up in a row.
Do not choose elements for their display value
Because display is trivially changed in CSS, it is a poor reason to pick an element. Choose based on what the content means. A navigation list should be a list because it is a list of links, and if you want it horizontal you change its display. Reaching for a div because you want a block, or a span because you want inline, throws away meaning for something a single CSS declaration would have given you anyway.
Diagnosing it quickly
When an element ignores a size you set, open your browser tools and look at its computed display value. Seeing inline there explains the ignored width immediately, and the fix is either a different display value or, more often, a better choice of element. It is a five second check that resolves a problem which otherwise looks like the CSS is simply not loading.