CSS: CSS Display & Visibility

Last updated: 2026-10-13

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 How to remove gaps between inline-block elements?
A The 4px gap between inline-block elements comes from HTML whitespace (line breaks and spaces). Solutions: set font-size: 0 on parent then reset on children, or write tags on the same line without breaks.
Q opacity: 0 vs visibility: hidden — which performs better?
A opacity: 0 elements can still trigger click events, visibility: hidden cannot. Performance is similar, but opacity supports transition animations for smooth show/hide, visibility cannot transition.

📖 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.

100%

🙏 帮我们做得更好

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

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