CSS: CSS Display & Visibility

🗓 Last updated: 2026-11-30👤 Written by: Alex✓ Reviewed by: MOTO🏷 Category: CSS Tutorial

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

Properties in this section: display flex grid

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 Yourself

2. 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 Yourself

3. Element Hiding Methods Comparison

Method Occupies Space Visible Interactive Animation
display: none ❌ ❌ ❌ ❌
visibility: hidden ✅ ❌ ❌ ✅
opacity: 0 ✅ ✅ ✅ ✅
position: absolute; left: -9999px ❌ ❌ ✅ ❌

Properties in this section: display flex grid

❓ 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

📝 Exercises

  1. Create a page with block, inline, and inline-block boxes to observe arrangement and size differences.
  2. Set navigation links to inline-block with width, height, and padding to create a horizontal navbar.
  3. Demonstrate the difference between display: none and visibility: hidden.
  4. Create a "button group" using display: inline-block, handling the whitespace gap issue.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏