Sub topic 4
display: inline
This chapter covers how inline boxes behave: how they flow in lines of text, which properties apply to them, and which are ignored. It assumes you know basic HTML elements, how to write CSS rules with selectors and properties, and the box model (content, padding, border, margin). Block boxes were covered in the previous chapter. The next chapter compares width, height, margin, and padding across display types in detail.
What an inline box is
An inline box is a box that does not start a new line. It sits in the flow of text, one after another with its neighbors, and it wraps to the next line when the current line is full. The <span>, <a>, <strong>, <em>, <code>, and <img> elements are inline by default. The display value inline produces this behavior.
<div>Block one</div>
<div>Block two</div>
<span>Inline one</span>
<span>Inline two</span>The two div elements each take their own line. The two span elements share one line, separated by a single space that comes from the whitespace in the markup.
Inline is a property of the CSS, not of the tag. The same rules can make a div behave like a span:
div {
display: block;
margin: 1rem 0;
}
span {
display: inline;
}If you write div { display: inline; }, every div flows in a line. HTML semantics do not change: a div is still a generic container and a span is still a generic inline container. Only the layout box changes.
How inline boxes flow
The browser places inline boxes into line boxes. A line box is a horizontal strip, as wide as the containing block, that holds everything on one line of text. The browser fills a line box from left to right in left-to-right languages, and starts a new line box when the next piece does not fit.
Because of this, an inline box has no fixed rectangle. If an inline element contains a long phrase and the phrase wraps, the element is split into one fragment per line. Each fragment has its own rectangle. You can see this by giving a long link a background color: the color appears as separate bands on each line.
Two consequences matter in practice:
- An inline element that wraps has no single width or height, so properties that depend on a single rectangle cannot apply cleanly.
- The height of a line is decided by the line's content and its
line-height, not by the padding or borders of the inline elements inside it.
What applies and what is ignored
The rule set below gives a span explicit sizes and spacing.
span.tag {
display: inline;
width: 200px;
height: 50px;
margin: 20px;
padding: 10px;
}On an inline, non-replaced box, the browser handles each declaration differently:
| Property | Effect on an inline box |
|---|---|
| `width`, `height` | Ignored. The box is as wide as its content. |
| `margin-left`, `margin-right` | Applied. They add space beside the box. |
| `margin-top`, `margin-bottom` | Ignored. They do not change line spacing. |
| `padding-left`, `padding-right` | Applied. They push neighbors away. |
| `padding-top`, `padding-bottom` | Painted, but they do not push other lines away. |
| `border` | Painted on all four sides, with the same vertical caveat as padding. |
| `background` | Painted behind the content and padding. |
The vertical padding row surprises many people. Padding above and below an inline box is drawn, so a background grows taller and can overlap the line above or below. It does not make the line box taller. Text on neighboring lines does not move.
a.highlight {
background: gold;
padding: 1rem 0.5rem;
}If this link sits in a paragraph, the gold area extends 1rem above and below the text. It can cover parts of the lines around it, because layout still uses the normal line height.
Replaced elements are an exception
A replaced element is one whose content comes from outside the CSS, such as <img>, <video>, or <input>. Images are inline by default, but they accept width and height:
<p>
A small icon
<img src="icon.png" alt="Settings gear" width="24" height="24">
sits inside this sentence.
</p>The image has intrinsic dimensions, so the browser can size its box. Vertical margins on an inline image do affect line height. This is the main reason "inline never accepts width and height" is not quite true. The rule is about non-replaced inline boxes, such as span and a.
A related gotcha: an inline image sits on the text baseline, which leaves a few pixels of space below it for descenders (the parts of letters like "g" and "y" that hang below the line). This is why an image placed alone in a container often shows a small gap underneath. Setting display: block on the image, or vertical-align: middle, removes the gap.
Whitespace between inline boxes
Browsers collapse runs of whitespace (spaces, tabs, newlines) in inline content into a single space. Whitespace at the start and end of a line is removed.
<p>
<span>One</span>
<span>Two</span>
</p>The newline and indentation between the spans collapse to one space, so the rendered text reads "One Two". Remove all whitespace between the tags (</span><span>) and the words render as "OneTwo". This behavior matters again when you compare it to inline-block, where the same collapsed space shows up as a visible gap between boxes.
You can change how whitespace is handled with the white-space property. For example, white-space: nowrap prevents wrapping, so the inline content stays on one line and may overflow its container.
Inline boxes that contain blocks
A block-level element inside an inline element is legal in HTML5 in many cases, for example an <a> wrapping a <div>. The layout is unusual. The browser splits the inline box around the block: the part before the block, the block itself, and the part after. The block takes a full line, and the inline box's padding and borders appear only on the fragments that are actually inline.
If you want a whole card to be clickable, it is usually clearer to set display: block on the <a> itself.
Other properties that behave differently on inline boxes
transformdoes not apply to non-replaced inline boxes. Transforms need a box with a definite rectangle.line-heighton an inline element changes its contribution to the line, and the tallest contribution on a line sets the line's height.vertical-alignapplies to inline boxes and moves them relative to the baseline of the line, for examplevertical-align: super.text-alignis set on the container, not on the inline element. Inline boxes follow the alignment of the block that contains them.box-decoration-break: clonemakes padding, border, and background repeat on each fragment when an inline element wraps across lines. The default value,slice, treats the fragments as one box cut into pieces, so the left padding appears only on the first fragment and the right padding only on the last.
Inline navigation links
Links are inline, so a row of links already sits on one line. The problem appears when you want a bigger click area. Horizontal padding works, but vertical padding does not grow the layout box, so the links look padded while the clickable area and surrounding spacing behave unexpectedly. The next chapters show how inline-block fixes this:
nav a {
display: inline-block;
padding: 0.5rem 1rem;
}For now, the point is to recognize the symptom: you add padding: 1rem to a link, the background grows, but nothing around it moves.
Common errors
- Setting
widthorheighton aspanoraand seeing no change. The fix is a differentdisplayvalue, not a bigger number. - Using vertical
marginon an inline element to space lines apart. Useline-heighton the container, or change the display type. - Expecting vertical padding to push surrounding lines away. It only enlarges the painted area.
- Forgetting that a wrapped inline element is several fragments, so a single border or background can appear broken across lines.
- Seeing a gap under an inline image and trying to remove it with margin. The gap comes from baseline alignment, so use
display: blockorvertical-align.
Exercise
A <span class="label"> has width: 120px; height: 40px; margin: 10px; padding: 10px; and display: inline. Which of these declarations change layout, and what happens to the vertical padding?
Show answer
Only the horizontal margin (10px left and right) and the horizontal padding (10px left and right) affect layout. width, height, and the top and bottom margins are ignored on a non-replaced inline box. The top and bottom padding (10px) is painted, so a background or border grows taller, but it does not change the line height or push neighboring lines away, so it can overlap them.
Exercise
An <img> placed by itself inside a <div> leaves a few pixels of empty space under the image. Why does this happen, and name two ways to remove it.
Show answer
The image is an inline element, so it sits on the text baseline of a line box, and the line box reserves space below the baseline for descenders. The gap is that reserved space. You can remove it by setting display: block on the image, or by setting vertical-align: middle (or top or bottom) on the image.