Sub topic 8
How display type changes box behaviour
This chapter assumes you know how content, padding, border, and margin stack in a box, and how box-sizing changes what width means. It also assumes you can read basic HTML and write CSS rules. Here you will see that the same box properties do not behave the same way for every element. The display property decides which ones apply.
Two questions the display value answers
Every element has a display value. It answers two questions:
- Outer display type: how does the box take part in its parent's layout? It is either block-level or inline-level.
- Inner display type: how are the box's children laid out? Examples are normal flow,
flex, andgrid.
The values in this chapter are block, inline, and inline-block. Each combines an outer type with normal-flow layout inside. display: flex and display: grid are covered separately. They have the same block-level outer type, but they lay out their children differently.
| Value | Outer type | Inner layout | Typical elements |
|---|---|---|---|
| `block` | block-level | normal flow | `div`, `p`, `h1`, `ul` |
| `inline` | inline-level | inline content | `span`, `a`, `em`, `strong` |
| `inline-block` | inline-level | normal flow (acts like a block inside) | `button`, `input` by default in most browsers |
| `none` | no box generated | none | `[hidden]` elements |
Block boxes
A block-level box starts on a new line and, by default, stretches to fill the available inline space of its container. With width: auto, the box's margin box fills the container exactly. The content width is whatever remains after margins, borders, and padding are subtracted.
All four sides of every box property apply to a block box:
widthandheightare honoured.- Margins and padding apply on all four sides.
- Vertical margins can collapse with neighbouring vertical margins (see the margin collapsing chapter).
- Setting
margin-inline: autowith a fixed width centres the box.
.panel {
display: block;
width: 20rem;
padding: 1rem;
margin: 2rem auto;
background: #eef;
}If width is auto, the browser solves for it. If you set width: 20rem and the container is wider, the leftover space goes to the auto margins. If the container is narrower, the box overflows. It does not shrink.
Inline boxes
An inline box flows within a line of text, like a word. It does not start a new line, and its width is the width of its content. Its rules are the ones that surprise people most:
widthandheighthave no effect.- Horizontal margin and padding work and push neighbouring content aside.
- Vertical margin has no effect.
- Vertical padding and borders are painted but do not change the height of the line. They can overlap the lines above and below.
- If the text wraps across lines, one inline box is split into fragments. The box is broken at the wrap point.
The following page demonstrates the vertical padding rule:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Inline box demo</title>
<style>
body { font: 16px/1.5 sans-serif; width: 20rem; }
mark {
width: 400px; /* ignored */
height: 100px; /* ignored */
margin: 40px 10px; /* vertical part ignored, horizontal applies */
padding: 12px 8px; /* painted, but line spacing does not change */
border: 2px solid #c60;
background: #ffd;
}
</style>
</head>
<body>
<p>Some text with a <mark>highlighted phrase</mark> in the middle of a
paragraph, followed by more text to show the line spacing.</p>
</body>
</html>Open this and look at the highlighted phrase. The yellow background extends above and below the text and may cover parts of neighbouring lines. The lines themselves stay evenly spaced. The width, height, and vertical margin declarations change nothing.
The height of a line is controlled by line-height, not by the padding or border of inline boxes inside it. To get a box that really takes up vertical space, you need a different display type.
Replaced elements are an exception
A replaced element is one whose content comes from outside CSS, such as img, video, and canvas. Replaced elements are inline by default, but they accept width and height. Their vertical margins also affect layout. They behave a lot like inline-block in this respect.
Inline-block boxes
inline-block gives you a hybrid. The element sits in a line of text like an inline box. Internally, it is a full block box, so every box property applies.
| Behaviour | `block` | `inline` | `inline-block` |
|---|---|---|---|
| Starts on a new line | yes | no | no |
| Default width | fills container | content width | shrink to fit content |
| `width` / `height` honoured | yes | no | yes |
| Vertical margin affects layout | yes (may collapse) | no | yes (never collapses with siblings) |
| Vertical padding affects layout | yes | no (painted only) | yes |
| Sits within a text line | no | yes | yes |
"Shrink to fit" means the box takes the content's natural width but never exceeds the available space, unless a single unbreakable item is wider.
This makes inline-block useful for small components that sit next to text, such as badges or buttons:
.badge {
display: inline-block;
min-width: 2rem;
padding: 0.25rem 0.5rem;
margin-block: 0.5rem;
border-radius: 1rem;
background: #060;
color: #fff;
text-align: center;
}Because .badge is inline-block, min-width, vertical padding, and vertical margin all take effect. If it were inline, min-width and the vertical margin would be ignored.
Gotchas with inline-level boxes
Whitespace gaps
Inline-level boxes are separated by whitespace in the HTML source. A line break between two inline-block elements becomes a space character, typically a few pixels wide.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Whitespace gap</title>
<style>
.tile {
display: inline-block;
width: 50%;
box-sizing: border-box;
background: #9cf;
}
</style>
</head>
<body>
<div class="tile">A</div>
<div class="tile">B</div>
</body>
</html>Two tiles at 50% each cannot fit on one line, because the space between them adds extra width. The second tile wraps to a new line. Common fixes are to use flexbox or grid for the layout, or to set font-size: 0 on the parent. Flexbox is the better choice, because the space between flex items is not rendered.
Baseline alignment
Inline-level boxes align on the text baseline by default (vertical-align: baseline). An inline-block with its own text sits on the baseline of its last line of text. A box with overflow other than visible, or one with no in-flow line boxes, uses its bottom margin edge as the baseline. That alignment can leave an odd gap below an inline-block or an img inside a container.
To remove the gap, set vertical-align: top (or middle) on the box. For images, display: block is another common fix.
img {
display: block;
max-inline-size: 100%;
}`vertical-align` only applies to inline-level boxes
vertical-align does nothing on a block-level element. It does apply to inline, inline-block, and table cells.
Changing the display type
You can override the default for any element. A link turned into a block fills the row, which makes the whole row clickable:
nav a {
display: block;
padding: 0.75rem 1rem;
}Without display: block, the vertical padding on a would paint over neighbouring lines but not enlarge the clickable region in the layout.
Blockification is related. When an element becomes a flex or grid item, or is floated or absolutely positioned, the browser computes its display as the block-level equivalent. A span that is a flex item behaves as a block, so width and height apply, even though you never wrote display: block.
`display: none` versus other ways to hide
| Technique | Box generated | Takes up space | Accessible to assistive tech |
|---|---|---|---|
| `display: none` | no | no | no |
| `visibility: hidden` | yes | yes | no |
| `opacity: 0` | yes | yes | yes |
display: none removes the element and all its descendants from the box tree. You cannot override it on a child. Use it when the element should not exist in the layout. Use visibility: hidden to keep its space reserved.
Diagnosing a "my width doesn't work" bug
When a box property seems to be ignored, work through these checks:
- Open developer tools and check the element's computed
displayvalue. Do not rely on the value you think it has. - If it is
inline,width,height, and vertical margin are ignored by design. Switch toinline-blockorblock. - If it is
inline-blockand the layout breaks, look for whitespace gaps and baseline gaps. - If it is a flex or grid item, remember it is blockified, and sizing follows the flex or grid algorithm.
Exercise
A <span class="tag"> has width: 120px; height: 40px; margin: 20px 10px; padding: 10px; and the default display. Which of these declarations take effect on layout, and what single change makes all of them take effect?
Show answer
With display: inline, width, height, and the 20px vertical margins are ignored. The 10px horizontal margins and the 10px horizontal padding take effect. The 10px vertical padding is painted but does not change line spacing. Setting display: inline-block makes every declaration take effect while keeping the span in the text line. display: block also works, but it moves the span onto its own line.
Exercise
Two inline-block divs with width: 50% and box-sizing: border-box are separated by a line break in the HTML. Why does the second one wrap, and name one reliable fix?
Show answer
The line break between them becomes a space character in the line. That space adds a few pixels, so the total is slightly more than 100% and the second box moves to the next line. A reliable fix is to make the parent display: flex, which does not render whitespace between flex items.