Display & Visibility
The display property decides how an element's box behaves in the layout: does it start a new line, sit inside a line of text, become a flex or grid container, or disappear completely? Together with visibility and overflow, it controls what is shown and how.
block, inline and inline-block
Every HTML element has a default display value. Headings, paragraphs, div, section and lists are block; span, a, strong and em are inline. (The HTML series covers this in Block & Inline Elements.) CSS lets you change the default.
| Value | New line? | Width and height | Vertical padding/margin |
|---|---|---|---|
block | Yes; fills the available width | Respected | Pushes other elements away |
inline | No; flows within text | Ignored | Padding is drawn but doesn't push lines apart; vertical margins have no effect |
inline-block | No; sits in the line like a word | Respected | Pushes other elements away |
inline-block is handy for buttons and tags that sit in a sentence but need padding and a fixed size. For arranging several items in a row, though, Flexbox is now the better tool.
Layout display values
display: flex and display: grid turn an element into a layout container whose children are arranged by Flexbox or Grid rules. The container itself still behaves like a block on the outside. inline-flex and inline-grid do the same but the container behaves like an inline-block.
Hiding elements
There are several ways to hide something, and they are not the same:
| Technique | Takes up space? | Read by screen readers? | Clickable? |
|---|---|---|---|
display: none | No; the layout closes the gap | No | No |
visibility: hidden | Yes; leaves an empty gap | No | No |
opacity: 0 | Yes | Yes | Yes (invisible but still there) |
The HTML hidden attribute works like display: none. Use display: none for things like a closed mobile menu, visibility: hidden when you need the space reserved, and opacity mainly for fade animations (because opacity can be transitioned smoothly).
Overflow
When content is bigger than its box (for example a fixed-height box with lots of text), overflow decides what happens:
visible(default): content spills out of the box.hidden: extra content is clipped and can't be seen.scroll: scrollbars always appear.auto: scrollbars appear only when needed. This is usually what you want.
Use overflow-x and overflow-y to control one direction. overflow-x: auto on a wrapper around a wide table lets it scroll sideways on phones instead of breaking the page.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Display</title>
<style>
body { font-family: system-ui, sans-serif; margin: 1.5rem; }
.tag {
display: inline-block;
width: 90px;
padding: 0.3rem;
text-align: center;
background: #fde68a;
border-radius: 6px;
}
.gone { display: none; }
.ghost { visibility: hidden; }
.scroll-box {
height: 70px;
overflow: auto;
border: 2px solid #1e3a8a;
padding: 0.5rem;
}
</style>
</head>
<body>
<p>Skills: <span class="tag">HTML</span> <span class="tag">CSS</span> <span class="tag">JS</span></p>
<p>A <span class="gone">HIDDEN</span> B <span class="ghost">HIDDEN</span> C</p>
<div class="scroll-box">
Line one.<br>Line two.<br>Line three.<br>Line four.<br>Line five.
</div>
</body>
</html>
The three skill tags sit in the sentence as equal-width yellow boxes. The second line reads "A B" followed by a wide empty gap and then "C": the display: none word left no trace, but the visibility: hidden word kept its space. Below, a blue-bordered box shows the first few lines with a scrollbar for the rest.
Key points
blockstarts a new line;inlineflows in text and ignores width and height;inline-blockmixes both.display: flexanddisplay: gridcreate layout containers.display: noneremoves an element;visibility: hiddenhides it but keeps its space.overflow: autoadds scrollbars only when content doesn't fit.
Tip: use the ← and → keys to move between pages.
