On this page10 sections
  1. Learning Objectives
  2. 1. Block and Inline (a Recap)
  3. 2. inline Ignores Width and Height
  4. 3. inline-block: The Best of Both
  5. 4. Hiding Elements
  6. 5. A Preview: flex and grid
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 015: Display: Block, Inline & None

Category4: Layout Basics
Lecture015 of 057
Estimated video length12 min
TargetCSS3 · any modern browser
LevelBeginner

The display property decides how an element takes up space: a full-width block, a word inside a line, a mix of both, or not at all. It's also the switch that turns on Flexbox and Grid in the next lectures.


Learning Objectives

  • Change how elements flow with display: block, inline and inline-block
  • Hide elements with display: none and visibility: hidden, and know the difference
  • Know that display: flex and display: grid start the modern layout systems

1. Block and Inline (a Recap)

From HTML Lecture 016:

  • block elements (div, p, h1) start on a new line and fill the width
  • inline elements (span, a, strong) sit inside a line of text

CSS can change either one into the other.

Example: links turned into full-width blocks make a simple vertical menu:

html
<style>
  .menu a { display: block; padding: 10px; border-bottom: 1px solid #ddd; text-decoration: none; }
  .menu a:hover { background: #f1f3f5; }
</style>

<nav class="menu">
  <a href="#">Home</a>
  <a href="#">Courses</a>
  <a href="#">Contact</a>
</nav>

2. inline Ignores Width and Height

Inline elements ignore width, height and vertical margins. They only grow with their text.

Example

html
<style>
  span { background: #ffd43b; width: 200px; height: 60px; }
</style>

<p>This <span>span</span> ignores its width and height.</p>

3. inline-block: The Best of Both

inline-block elements sit side by side like words, but do accept width, height, padding and margin. They're perfect for buttons and badges.

Example

html
<style>
  .tag { display: inline-block; width: 90px; padding: 8px; margin: 4px; text-align: center;
         background: #e7f5ff; border: 1px solid #74c0fc; border-radius: 6px; }
</style>

<span class="tag">HTML</span>
<span class="tag">CSS</span>
<span class="tag">JavaScript</span>

4. Hiding Elements

Example

html
<style>
  .box { display: inline-block; width: 70px; padding: 14px 0; text-align: center; background: #b2f2bb; margin: 2px; }
  .gone      { display: none; }
  .invisible { visibility: hidden; }
</style>

<div class="box">1</div><div class="box gone">2</div><div class="box">3</div>
<br>
<div class="box">1</div><div class="box invisible">2</div><div class="box">3</div>
PropertyVisible?Takes up space?
display: noneNoNo. The others move up
visibility: hiddenNoYes. A gap is left

Menus that open and close usually switch between display: none and display: block.


5. A Preview: flex and grid

css
.row  { display: flex; }   /* children line up in a row: Lecture 049 */
.page { display: grid; }   /* children go into rows and columns: Lecture 051 */

These two values are how modern layouts are built.


Common Beginner Mistakes

1. Setting the width of a <span> or <a>. It's ignored while they're inline. Use inline-block or block.

2. Gaps between inline-block items. The spaces between tags in your HTML show up as small gaps. Flexbox (next lectures) avoids this.

3. Hiding important content with display: none. Search engines and screen readers may skip it. Hide only what's truly not needed at that moment.


Practice Questions

  1. Which display value makes an element start on a new line and fill the width?
  2. Why doesn't width: 100px work on a <span>?
  3. Which value lets elements sit side by side and accept a width?
  4. What's the difference between display: none and visibility: hidden?
  5. Make every link inside .sidebar a full-width block.
  6. Which two display values turn on the modern layout systems?

Practice Answers

  1. block
  2. A span is inline, and inline elements ignore width and height.
  3. inline-block
  4. display: none removes the element and its space; visibility: hidden hides it but keeps the space.
  5. .sidebar a { display: block; }
  6. flex and grid.

Key Takeaways

  • block = own line, full width; inline = inside text, no width/height.
  • inline-block = side by side, with width, height and padding.
  • display: none removes an element; visibility: hidden leaves a gap.
  • display: flex and display: grid start Flexbox and Grid.