CSS: CSS Display & Visibility
The display property is one of the most important CSS properties, controlling how elements are rendered and laid out on the page.
Display Property Quick Reference
| Value | Behavior | Width/Height | Typical Elements |
|---|---|---|---|
block |
New line, full width | Accepts | div, p, h1-h6 |
inline |
Same line, content width | Ignores | span, a, em |
inline-block |
Same line, accepts dimensions | Accepts | button, input |
flex |
Flex container | — | Layout containers |
grid |
Grid container | — | Layout containers |
none |
Completely hidden | — | Any element |
1. Display Values
(1) block
Block elements occupy a full row, stacking vertically:
▶ Example
▶ Try it Yourself(2) inline
Inline elements stay on the same line, only taking up content width:
▶ Example
▶ Try it Yourself(3) inline-block
Combines inline positioning with block-level dimensions:
▶ Example
▶ Try it Yourself2. Visibility & Opacity
(1) visibility: hidden vs display: none
▶ Example
▶ Try it Yourself(2) opacity
Controls element transparency (0-1), affecting all content including children:
▶ Example
▶ Try it Yourself3. Element Hiding Methods Comparison
| Method | Occupies Space | Visible | Interactive | Animation |
|---|---|---|---|---|
display: none |
❌ | ❌ | ❌ | ❌ |
visibility: hidden |
✅ | ❌ | ❌ | ✅ |
opacity: 0 |
✅ | ✅ | ✅ | ✅ |
position: absolute; left: -9999px |
❌ | ❌ | ✅ | ❌ |
❓ FAQ
Q What's the difference between display: none and visibility: hidden?
A display: none removes the element completely — no space occupied, surrounding elements fill the gap. visibility: hidden only hides the visual — element still occupies space. For animation transitions, opacity: 0 with transition is smoother.
Q What are the ways to hide an element, and when does each fit?
A Four main options: display: none removes it from layout entirely; visibility: hidden keeps the space but hides visuals; opacity: 0 keeps space, stays clickable, and animates; the HTML
hidden attribute behaves like display: none with clearer semantics. To vanish from layout use display or hidden; to "fade but keep position" use opacity.Q Can elements with display: none transition? Why is there no animation on toggle?
A Not directly — none and block have no intermediate state, so the browser jumps. Change partners instead: transition with visibility + opacity (both are animatable), then switch display after the animation ends. Modern browsers also support
transition-behavior: allow-discrete with @starting-style to animate display itself; for older codebases stick with visibility + opacity.Q What's the actual difference between block and inline?
A block takes a full line, accepts width/height, defaults to filling its parent; inline flows within a line, ignores width/height, ignores vertical margins, and sizes to its content. div/p/h1 are block; span/a/strong are inline. When width/height seem ignored in your CSS, first check whether the element's display is inline.
Q Why don't width, height, or margin-top work on inline elements?
A Inline boxes have a "partial" box model: size comes from content, so width/height are ignored; vertical margins don't apply — only horizontal ones do. When you need same-line flow with real dimensions, use
display: inline-block. img is the exception — a replaced element that renders inline yet accepts width/height.Q How to remove gaps between inline-block elements?
A The 4px gap between inline-block elements comes from HTML whitespace (line breaks and spaces render as one space). Solutions: set
font-size: 0 on parent then reset on children, or write tags on the same line without breaks. These days flex layout is the better fix — no gap problem to begin with.Q Both opacity: 0 and visibility: hidden hide things — which is better?
A Depends on the need: opacity: 0 elements still fire click events — beware "invisible but clickable" when building hover overlays; visibility: hidden is non-interactive. Both participate in transition, so for fade in/out use
visibility + opacity with a delay on visibility so hiding waits for the animation.Q How do I convert between block and inline, and what changes after?
A One
display property does it: inline becomes display: block (own line, settable width/height), block becomes display: inline (same line, width/height lost). inline-block is the middle ground with both perks. Conversion only changes layout behavior, not HTML semantics — an a tag styled block is still recognized as a link by search engines.Q HTML hidden attribute vs CSS display: none — what's the difference, which to use?
A hidden is native HTML semantics behaving like display: none, but with low priority — any CSS rule setting display overrides it (the most common "hidden not working" cause). Use hidden for static content — it reads clearer. For JS-driven toggles, control a class via CSS instead of mutating inline styles.
📖 Summary
displayis the core layout property- Understanding
block,inline,inline-blockdifferences helps control element arrangement display: noneandvisibility: hiddenboth hide elements, but one occupies space and the other doesn't — different use cases- In modern layouts,
display: flexanddisplay: gridare the most powerful layout tools
📝 Exercises
- Create a page with
block,inline, andinline-blockboxes to observe arrangement and size differences. - Set navigation links to
inline-blockwith width, height, and padding to create a horizontal navbar. - Demonstrate the difference between
display: noneandvisibility: hidden. - Create a "button group" using
display: inline-block, handling the whitespace gap issue.