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 Value | Line Flow Behavior | Accepts Custom Width/Height? | Accepts Vertical Margins? |
|---|---|---|---|
block | Forces new line break | Yes | Yes |
inline | Flows side-by-side with text | No | No (Vertical ignored) |
inline-block | Flows side-by-side with text | Yes | Yes |
none | Removes from DOM layout tree | N/A | N/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: inlineelements: Inline tags ignore custom width declarations. Change toinline-blockorblock.
12. Important Rules & Limitations
display: noneremoves the element from rendering AND screen readers. Usevisibility: hiddenif 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
- What is the difference between
display: noneandvisibility: hidden? - Why does an
<a>tag ignoreheight: 50pxuntil 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.