Sub topic 12
Choosing the right display value
This chapter assumes you know the CSS box model and can write rules with selectors. You should also know the earlier chapters' basics: block boxes stack and fill the width, inline boxes flow in a line of text, inline-block boxes flow like text but accept sizes, and display: none removes the box. Here you will turn that knowledge into a decision process.
Start with the behavior you need
Pick a display value by asking what the box must do, not by looking at the tag name. Tag names only set the default. Four questions cover most cases:
- Does the box need its own line, or the full available width?
- Does it sit inside a line of text and move with that text when it wraps?
- Does it need a set width, height, or vertical spacing?
- Should it be present on the page at all right now?
| What you need | Value | Why |
|---|---|---|
| Own line, fills the width, accepts width, height, and all margins | `block` | Block boxes stack vertically and honor the full box model. |
| Part of a sentence, wraps with the text, styled only horizontally | `inline` | The box follows line flow. Width and height are ignored. |
| Sits in a line, but needs width, height, or vertical padding and margin | `inline-block` | The box is inline-level on the outside and a block container on the inside. |
| Not rendered, takes no space | `none` | The element generates no box, and its descendants generate none either. |
Worked cases
A tag inside a paragraph: inline
A span that highlights a word should stay in the sentence. Inline is the default, so you often write nothing:
.keyword {
background: #fff3b0;
padding: 0 0.25rem;
}Horizontal padding works and the text wraps normally. If you add height or vertical margin to this rule, the browser ignores them. That is a signal you may need inline-block, not a bug.
Navigation links: inline-block
Links are inline by default, so only the text itself is clickable and vertical padding does not push neighboring content away. Making them inline-block gives each link a full padded target and keeps them in one row:
<nav class="site-nav">
<a href="/docs">Docs</a>
<a href="/blog">Blog</a>
<a href="/about">About</a>
</nav>.site-nav a {
display: inline-block;
padding: 0.5rem 1rem;
}The whitespace between the a tags still renders as a small space. For nav links this gap is usually fine. If you need exact spacing, set the parent to display: flex with a gap.
A card that is also a link: block
A link that wraps a heading and a paragraph needs to contain block content and fill its column. Set the link to display: block:
a.card {
display: block;
padding: 1rem;
border: 1px solid #ccc;
}This works because a block box has a width and height that contain its children. An inline link with block children inside is harder to predict.
A panel that opens and closes: none
When content should not exist in the layout until a user asks for it, toggle between none and the normal display value. This is a complete page you can save and open:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Toggle panel</title>
<style>
.panel { display: none; }
.panel.is-open { display: block; }
</style>
</head>
<body>
<button type="button" aria-expanded="false" aria-controls="details">Show details</button>
<div id="details" class="panel">Extra information goes here.</div>
<script>
const button = document.querySelector("button");
const panel = document.getElementById("details");
button.addEventListener("click", () => {
const open = panel.classList.toggle("is-open");
button.setAttribute("aria-expanded", String(open));
});
</script>
</body>
</html>The .is-open rule sets display: block and does not set it back to some guess. Restoring the correct value matters, as the errors below show.
Choosing how to hide
Hiding is the one case where several properties look alike. Decide by what should happen to space, clicks, and assistive technology.
| Property | Takes space? | Receives clicks and focus? | Read by screen readers? |
|---|---|---|---|
| `display: none` | No | No | No |
| `visibility: hidden` | Yes | No | No |
| `opacity: 0` | Yes | Yes | Yes |
Use display: none when the content should be completely absent, such as a closed menu or an inactive tab panel. Use visibility: hidden when the layout must not shift, for example a validation message whose space you reserve. Use opacity: 0 for fade animations, where the element must stay in the layout while it changes. After a fade-out finishes, pair it with something that stops clicks and focus, or the invisible element remains interactive.
.hidden-a { display: none; }
.hidden-b { visibility: hidden; }
.hidden-c { opacity: 0; }Content that is visually hidden but should still be read aloud needs a different technique, a "visually hidden" utility class. Do not use display: none for it, because that removes it from the accessibility tree.
Common errors and how to read them
Most display mistakes show up as "my property does nothing." Match the symptom to the cause:
| Symptom | Likely cause | Fix |
|---|---|---|
| `width` or `height` has no effect on a `span` or `a` | The box is inline | Use `inline-block`, or `block` if it needs its own line |
| Vertical margin does not move the surrounding lines | Inline boxes ignore vertical margin for line spacing | Use `inline-block` or `block` |
| Gaps appear between inline-block items | Whitespace in the HTML renders as a space | Use a flex parent, or accept the gap |
| Items in a row sit at uneven heights | Inline-block boxes align on their text baseline | Set `vertical-align: top`, or use flex |
| `li` bullets disappear | The `li` was changed from `list-item` to `inline` or `inline-block` | Keep list items as `list-item`, or style the list with flex |
| Layout breaks after showing a hidden element | The "show" rule used the wrong value, such as `block` on a flex container | Restore the element's real value |
| An element is gone and cannot be animated | `display: none` has no in-between state to transition | Animate `opacity`, then change `display` |
The baseline row is easy to miss. Inline-block boxes line up by the last line of text inside them, so cards of different heights drift vertically. This one line fixes the common case:
.badge {
display: inline-block;
vertical-align: top;
}Do not overuse inline-block
Inline-block is a common first answer for "put these boxes in a row," but it carries the whitespace gap and baseline alignment costs. If the real goal is to arrange a set of siblings, a layout mode usually fits better:
display: flexarranges items in one row or column with controlled spacing and alignment.display: gridarranges items in rows and columns together.display: flow-rootmakes a box that contains its floated children, which a plain block does not.display: contentsremoves the element's own box but keeps its children, which then take part in the parent's layout.
Keep inline-block for items that belong inside a line of text, such as badges, buttons, small icons, or links with padded click areas. Keep block and inline for ordinary document flow. Move to flex or grid when you are laying out a group of items in two dimensions or need exact spacing.
.row {
display: flex;
gap: 0;
list-style: none;
padding: 0;
}This rule turns a list into a row with no whitespace gap, which solves the inline-block spacing issue at the parent instead of the children.
Exercise
A span with class tag has width: 120px; padding: 0.5rem; margin: 1rem 0.5rem; and you want it to keep sitting inside a sentence. The width has no effect and the vertical margin does not change the line spacing. Which display value fixes this, and what new side effect can you expect between several adjacent tags?
Show answer
Set display: inline-block on .tag. The width and vertical margin then apply while the tag still flows in the line of text. Adjacent tags separated by whitespace in the HTML show a small gap between them, and tags of different heights align on their baselines, so you may need vertical-align: top.
Exercise
You are building a form with an error message under each field. When the message appears, the form below it jumps down, which annoys users. You also have a decorative panel that should take no space at all until opened. Which hiding method fits each case?
Show answer
Use visibility: hidden for the error message, because it keeps the reserved space and the layout does not jump when the message becomes visible. Use display: none for the decorative panel, because it should take no space and should not be reachable by keyboard or screen reader until it is opened.
Exercise
A fading menu uses opacity: 0 to hide itself, but keyboard users report that tabbing lands on links they cannot see. Why does this happen, and what is a fix?
Show answer
opacity: 0 hides the menu visually but leaves it in the layout, focus order, and accessibility tree, so its links are still focusable. A fix is to also set visibility: hidden once the fade finishes, or to switch to display: none. Either removes the links from focus and from assistive technology.