Sub topic 4
display: inline
This chapter assumes you know basic HTML elements, how to write CSS rules with selectors and properties, and the box model (content, padding, border, margin). It covers how inline boxes flow in a line, which box properties they honor, and the errors that follow from that.
What an inline box is
An inline box does not start a new line. It sits in the flow of text, next to the content before and after it, and it wraps to the next line when the current line is full. The default display value of span, a, em, strong, and code is inline.
<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 and span the available width. The two span elements share one line. The value comes from the CSS, not from the tag name. The next rules restate the defaults:
div {
display: block;
margin: 1rem 0;
}
span {
display: inline;
}Because the value is just a property, you can write div { display: inline; } and the div will flow like text. Its HTML meaning does not change.
Line flow and wrapping
An inline box has no fixed shape. Its width is the width of its content. If the text inside wraps across two lines, the box is split into fragments, one per line. Each fragment gets the inline box's horizontal padding and border only at the start of the first fragment and the end of the last one.
The height of the line is set by the line box, which comes from line-height and the font. It is not set by the padding or margin you put on the inline element.
What inline boxes ignore
Inline boxes ignore width and height. They also ignore vertical margin: it does not change the spacing between lines. Horizontal margin and padding work. Vertical padding and borders are drawn, but they do not push neighboring lines away, so they can overlap the text above and below.
span.tag {
display: inline;
width: 200px;
height: 50px;
margin: 20px;
padding: 10px;
}With this rule, the 200px width and 50px height have no effect. The left and right margin of 20px and the left and right padding of 10px take effect. The top and bottom margin of 20px does nothing to line spacing. The top and bottom padding of 10px paints a larger background area, but the surrounding lines do not move.
| Property | Effect on an inline box |
|---|---|
| `width`, `height` | Ignored |
| Horizontal margin | Applied |
| Vertical margin | Does not affect line spacing |
| Horizontal padding | Applied |
| Vertical padding | Painted, but does not affect line spacing |
Common errors
- Setting
widthorheighton aspanoraand seeing no change. Fix: change the display value toinline-blockorblock, or size the parent. - Adding
margin-topto an inline element to create space. Fix: useline-height, or change the display type. - Expecting a padded link to push the lines around it apart. Vertical padding on an inline
aoverlaps nearby lines. Fix: useinline-blockfor a padded click area. - Putting a block element, such as a
div, inside an inline element for layout. Browsers handle it, but the result is hard to predict. Prefer a block parent.
Whitespace between inline boxes
Inline boxes are laid out like words, so the whitespace in your HTML between two inline elements renders as a single space. Line breaks and indentation in the source collapse into one space character. This is why two span elements on separate lines in the markup still have a small gap between them on the page.
Exercise
A span has display: inline; width: 120px; height: 40px; margin: 10px; padding: 5px;. Which of these values affect the layout of the surrounding lines?
Show answer
Only the horizontal margin (10px left and right) and horizontal padding (5px left and right) affect the layout. The width and height are ignored, and the vertical margin does not change line spacing. The vertical padding is painted but does not push the lines above and below.
Exercise
You want a link in a nav bar to have a padded, clickable area of 0.5rem above and below, and the links must stay on one line. Which display value should you use instead of inline?
Show answer
Use display: inline-block. It keeps the link in the line of text, and it makes the vertical padding count in the layout.