Sub topic 12
Choosing the right display value
This chapter assumes you know basic HTML, how to write CSS rules with selectors and properties, and the box model (content, padding, border, margin). You have seen how block, inline, inline-block, and none behave. Here you will turn that into a decision process.
Start with the question you are asking
Pick a value by asking what the element must do. Three questions cover most cases:
- Does the element need its own line, or should it sit inside a line of text?
- Does it need
width,height, or vertical margin? - Should it take up space at all?
The answers map to a value.
| Need | Value |
|---|---|
| Own line, full width, sizeable | `block` |
| Part of a text run, no sizing needed | `inline` |
| Sits in a line, but needs width, height, or vertical spacing | `inline-block` |
| No box, no space, no rendering | `none` |
Worked cases
A navigation link with a large click area
Links are inline by default, so vertical padding does not push neighbors away and the clickable area can overlap the lines around it. Use inline-block so the padding counts in layout and the links still sit in one row.
nav a {
display: inline-block;
padding: 0.5rem 1rem;
}A fixed-size badge inside a sentence
A badge needs a width and vertical margin, but it must stay in the text flow. This is the standard inline-block case.
.badge {
display: inline-block;
width: 6rem;
padding: 0.5rem;
margin: 0.5rem;
text-align: center;
}Emphasis inside a paragraph
A highlighted word needs no size. Leave it inline. Making it inline-block adds nothing, and it would stop the text from wrapping across lines as one box.
A section that should stack
A card or section should fill the available width and start on its own line. Leave it block and set margin and width as needed.
Hiding: pick by what must remain
For none, the question is whether the space and the accessibility tree should remain.
.hidden-a { display: none; }
.hidden-b { visibility: hidden; }
.hidden-c { opacity: 0; }- Use
display: noneto remove the element entirely, for example a closed menu or an inactive tab panel. It also leaves the accessibility tree and the tab order. - Use
visibility: hiddenwhen the layout must not shift but the element should be invisible and not focusable. - Use
opacity: 0for fade effects. The element still takes space and can still receive clicks and focus, so it is rarely right for content that is meant to be gone.
Common mistakes
- Using
inline-blockfor a row of items and then fighting the whitespace gap. If the items form a row layout,display: flexon the container is simpler. - Setting
widthorheighton an inline element and wondering why nothing changes. Switch toinline-blockorblock. - Changing
displayto fix semantics.displayonly changes layout. Adivstyled asinlineis still adivto the browser's parser and to HTML validation rules. - Using
opacity: 0to hide content from screen reader users. It does not hide it.
Quick checklist
- Default first: check whether the element's default
displayalready does what you need. - Change it only for a reason you can state, such as "this needs vertical padding but must stay in the line."
- Test with a different amount of text, since inline boxes wrap and block boxes do not.
Exercise
A set of tag labels sits inside a paragraph. Each label needs a fixed width of 5rem and 0.5rem of vertical margin, and the paragraph's text must keep flowing around them on the same line. Which display value do you set on the labels, and why not inline or block?
Show answer
Use display: inline-block. Plain inline ignores width and vertical margin, and block would put each label on its own line and break the flow of the paragraph.
Exercise
A tab panel must be invisible when inactive, must not leave a blank gap, and must not be reachable by keyboard or screen reader. Which of display: none, visibility: hidden, and opacity: 0 fits?
Show answer
display: none. It removes the element from layout, so there is no gap, and it also removes the element from the accessibility tree and tab order. visibility: hidden leaves the gap, and opacity: 0 leaves both the gap and the focusability.