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

HTML
<style>
.block {
  display: block;
  width: 200px;
  background: #e3f2fd;
  padding: 10px;
  margin: 5px 0;
}
</style>
<div class="block">Block element 1</div>
<div class="block">Block element 2</div>
▶ Try it Yourself

(2) inline

Inline elements stay on the same line, only taking up content width:

▶ Example

HTML
<style>
.inline {
  display: inline;
  background: #fce4ec;
  padding: 5px;
}
</style>
<span class="inline">Inline 1</span>
<span class="inline">Inline 2</span>
<span class="inline">Inline 3</span>
▶ Try it Yourself

(3) inline-block

Combines inline positioning with block-level dimensions:

▶ Example

HTML
<style>
.inline-block {
  display: inline-block;
  width: 150px;
  height: 80px;
  background: #e8f5e9;
  margin: 5px;
  text-align: center;
  line-height: 80px;
}
</style>
<div class="inline-block">Box 1</div>
<div class="inline-block">Box 2</div>
<div class="inline-block">Box 3</div>
▶ Try it Yourself

2. Visibility & Opacity

(1) visibility: hidden vs display: none

▶ Example

HTML
<style>
.hidden {
  visibility: hidden;
  background: #ffebee;
  padding: 10px;
}
.none {
  display: none;
}
.visible {
  background: #e8f5e9;
  padding: 10px;
}
</style>
<div class="visible">Visible element</div>
<div class="hidden">Hidden but still occupies space</div>
<div class="visible">This element stays in place</div>
▶ Try it Yourself

(2) opacity

Controls element transparency (0-1), affecting all content including children:

▶ Example

HTML
<style>
.semi-transparent {
  opacity: 0.5;
  background: #2196f3;
  color: white;
  padding: 20px;
}
</style>
<div class="semi-transparent">50% transparent (all content affected)</div>
▶ Try it Yourself

3. 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 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%

🙏 帮我们做得更好

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

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