CSS Display
The display property decides how an element takes part in layout: whether it starts on a new line, whether width and height apply to it, and whether its children are laid out with a special system such as flexbox or grid. Changing display is often the first step in fixing a layout problem, because the default value of an element determines most of its behaviour.
Block, inline and inline-block side by side
A block element, such as div, p or section, starts on a new line and stretches across the full width of its container. Width, height, margin and padding all work as you would expect.
An inline element, such as span, a or strong, flows inside a line of text. It ignores width and height, and vertical margins do not push other lines away.
Inline-block is a mix. The element sits in the line like a word, yet it accepts width, height and vertical spacing like a block. It is useful for small badges or buttons placed in a row.
.tag {
display: inline-block;
padding: 4px 10px;
margin: 0 6px 6px 0;
border-radius: 999px;
background: #e8eefc;
}
.nav-links a {
display: block; /* whole row becomes clickable */
padding: 12px 16px;
}
Turning on flex and grid
Setting display: flex or display: grid on a container changes how its direct children are arranged, while the container itself still behaves as a block. Flexbox arranges children in a single row or column and is ideal for toolbars, cards in a row and centring. Grid arranges children in rows and columns at the same time and suits page layouts and galleries. Their inline versions, inline-flex and inline-grid, make the container sit inside a line of text instead.
Hiding elements: none versus visibility
display: none removes the element from layout entirely. It takes up no space, and screen readers skip it. In contrast, visibility: hidden makes the element invisible but leaves an empty gap where it was. Pick display: none for things like closed menus, and visibility or opacity when you need the space reserved.
| Value | New line? | Width/height work? |
|---|---|---|
block | Yes | Yes |
inline | No | No |
inline-block | No | Yes |
none | Not rendered | Not applicable |
Mistakes to watch for
Many beginners set a width on a link or span and wonder why nothing changes. The element is still inline, so the width is ignored until you switch it to inline-block or block.
Another surprise comes with inline-block rows: a small gap appears between items. That gap is the whitespace between the tags in your HTML, rendered like a space character. Using a flex container with gap avoids the problem and gives you precise spacing.
Finally, remember that display: none cannot be animated with a transition. If a dropdown needs to fade, animate opacity and toggle visibility instead.
Takeaway
Check an element's current display value in your browser's developer tools whenever sizing seems ignored. Most surprises come from an element being inline when you expected block, or the reverse.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.