13. The Display Property (block, inline, inline-block, none)

🟢 Beginner

1. Definition

The display property specifies an element's outer formatting context and inner layout rendering behavior: block, inline, inline-block, none, flex, and grid.

2. Why It Is Used

Changing display values transforms default inline text tags (like <a>) into clickable block buttons or converts multi-line divs into inline elements.

3. How It Works

Block elements take up 100% container width and force line breaks. Inline elements only take content width and ignore vertical margins/heights. inline-block combines inline line flow with custom block width and height support.

4. Syntax

.element {
  display: inline-block;
}

5. Display Values Comparison Table

Display ValueLine Flow BehaviorAccepts Custom Width/Height?Accepts Vertical Margins?
blockForces new line breakYesYes
inlineFlows side-by-side with textNoNo (Vertical ignored)
inline-blockFlows side-by-side with textYesYes
noneRemoves from DOM layout treeN/AN/A

6. Beginner Example

a.btn {
  display: inline-block;
  padding: 10px 20px;
  background-color: #2563eb;
  color: white;
}

7. Intermediate Example

.nav-menu li {
  display: inline-block;
  margin-right: 16px;
}

8. Advanced Flow Control Example

/* Hiding element visually and from accessibility tree vs visual hide */
.hidden-visually {
  display: none; /* Completely removed from DOM layout */
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0); /* Accessible screen reader text */
}

9. Line-by-Line Code Explanation

  • display: none;: Completely collapses the element space so neighboring elements fill the gap.
  • display: inline-block;: Allows setting explicit width, height, and padding on inline links.

10. Expected Terminal / Browser Output

Links formatted as button blocks aligned side-by-side on a single line.

11. Common Mistakes & Pitfalls

  • Setting width/height on display: inline elements: Inline tags ignore custom width declarations. Change to inline-block or block.

12. Important Rules & Limitations

  • display: none removes the element from rendering AND screen readers. Use visibility: hidden if you want to keep the element space intact.

13. Multilingual Explanation: English

The display property controls element layout flow: block takes full line width, inline fits content width, and inline-block supports custom sizing.

14. Multilingual Explanation: Hindi (Roman Script)

Display property decide karti hai ki element kaise dikhega: block poori line leta hai, inline text ke saath rehta hai, aur inline-block mein custom width/height de sakte hain.

15. Multilingual Explanation: Marathi (Roman Script)

Display property mule element cha flow tharto: block navin line var jato, inline text sobat rahto, ani inline-block la custom size deta yeto.

16. Practice Questions

  1. What is the difference between display: none and visibility: hidden?
  2. Why does an <a> tag ignore height: 50px until its display is changed?

17. Practical Coding Challenge

Convert 4 list items (<li>) into a horizontal navigation bar using display: inline-block with 15px padding.

18. Related Topics