Block vs Inline vs Inline-Block: Mastering CSS Display
Block vs Inline vs Inline-Block: Mastering CSS Display
Welcome to Chapter 9! 🚦
Have you ever wondered why an <h1> heading or <p> paragraph always starts on a fresh new line, while a <span>, <strong>, or <a> link happily sits right inside the same sentence without breaking to the next line?
The secret behind this behavior is the CSS display property!
The display property is the ultimate traffic police of the web. It dictates how an element renders in relation to its neighbors, whether it stretches across the entire screen, and whether you are allowed to change its width, height, and margins.
In this lesson, you will master:
- 1Normal Document Flow in HTML
- 2
display: block— The full-width line-takers - 3
display: inline— The sentence-friendly word-flows - 4
display: inline-block— The superhero hybrid combining the best of both worlds
The School Classroom Analogy 🏫
To clearly grasp the difference, look around your School Classroom:
+-------------------------------------------------------------------------+ | THE 3 DISPLAY TYPES IN A CLASSROOM | +-------------------------------------------------------------------------+ | 1. BLOCK (The Teacher's Blackboard / Wooden Benches) | | - Takes up the entire width of the wall from left to right. | | - Nothing else can sit beside it on the same line. | | - You can measure its exact width and height in meters. | | | | 2. INLINE (Words Written in Your Notebook) | | - Words flow one after another across the ruled notebook line. | | - Takes only the exact space needed for its letters. | | - You cannot set an artificial 200px width on a single word! | | | | 3. INLINE-BLOCK (Pencil Boxes Lined Up on a Bench) | | - Several pencil boxes sit side-by-side on the same bench. | | - BUT each pencil box has a rigid width, height, and depth! | +-------------------------------------------------------------------------+
1. display: block
A block-level element is a structural container. By default, it behaves like a rigid brick:
- Always starts on a new line (creates a line break before and after).
- Stretches 100% full width of its parent container unless you explicitly restrict its width.
- Respects all box model properties:
width,height,padding, andmarginwork fully on all 4 sides (top, right, bottom, left).
Common Default Block Elements in HTML:
<div>, <p>, <h1> through <h6>, <ul>, <ol>, <li>, <header>, <footer>, <section>, <article>.
+-------------------------------------------------------------+ | BLOCK ELEMENT (div, p, h1) | | Stretches full 100% width. Pushes next element to new line! | +-------------------------------------------------------------+ +-------------------------------------------------------------+ | NEXT BLOCK ELEMENT | +-------------------------------------------------------------+
2. display: inline
An inline element is designed to sit directly inside text content without disturbing the flow of the sentence:
- Does NOT start on a new line. It sits side-by-side with adjacent text.
- Takes only as much width as its content: Its width is determined solely by the text or image inside.
- ⚠️ Crucial Limitation: Inline elements IGNORE
widthandheightproperties! - Vertical Spacing Quirk: While
padding-leftandpadding-rightwork normally, verticalpadding-top,padding-bottom,margin-top, andmargin-bottomdo NOT push surrounding text lines away cleanly.
Common Default Inline Elements in HTML:
<span>, <a>, <strong>, <em>, <code>, <label>, <small>.
3. display: inline-block
What if you want elements to sit side-by-side on the same horizontal line (like buttons or cards), but you also want full control over their width, height, padding, and margin?
That is exactly why inline-block exists! It is the perfect marriage of both worlds:
- Sits side-by-side on the same line like an
inlineelement. - Respects
width,height,margin, andpaddingon all 4 sides like ablockelement!
+---------------------+ +---------------------+ +---------------------+ | [btn] Save Draft | | [btn] Submit Exam | | [btn] Cancel | | (custom width: 140) | | (custom width: 140) | | (custom width: 140) | +---------------------+ +---------------------+ +---------------------+ All sitting on the same line with exact widths and heights!
Side-by-Side Comparison Table 📊
| Feature | display: block | display: inline | display: inline-block |
|---|---|---|---|
| Starts on new line? | ✅ Yes | ❌ No | ❌ No (sits side-by-side) |
| Default Width | 100% of parent | Content width only | Content width only |
Respects width & height? | ✅ Yes | ❌ No (ignored) | ✅ Yes |
Respects margin & padding? | ✅ All 4 sides | ⚠️ Left/Right only | ✅ All 4 sides |
| Primary Use Cases | Page sections, cards, headers | Text highlights, bold words | Buttons, badges, grid items |
Changing Default Display Values
Remember: HTML tags do not have permanently carved-in-stone display behaviors! You can change any element's display using CSS:
Common Mistakes Beginners Make ❌
| Bad Practice ❌ | Why It Fails ⚠️ | Better Approach ✅ |
|---|---|---|
Setting width: 200px; on a <span> or <a> | Pure inline elements ignore width and height properties completely. | Change to display: inline-block; or display: block;. |
Adding top/bottom margins to <a> links | Vertical margins do not push adjacent text lines in inline elements. | Set display: inline-block; so vertical margins work properly. |
Forgetting that HTML spaces between inline-block elements create a 4px gap | Web browsers render HTML code line-breaks and tabs as a single whitespace character. | Use modern Flexbox (display: flex; gap: 12px;) or eliminate HTML spaces. |
Using display: block on items meant to be side-by-side | Block elements force a new line, stacking everything vertically. | Use display: inline-block or display: flex;. |
Summary Cheat Sheet 📌
display: block: Starts on a new line, fills 100% width, honors all dimensions (div,p,h1).display: inline: Flows inline with text, width hugs content, ignoreswidth/heightand vertical margins (span,a,strong).display: inline-block: Sits side-by-side like inline, but respectswidth,height, and all 4-sided margins/paddings like block.- CSS Overrides: Any HTML tag's default display can be changed at will using the
displayproperty.
Multiple Choice Questions
1. Which CSS display value forces an element to begin on a brand-new line and take up 100% of its parent's width by default?
A. display: inline; B. display: block; C. display: inline-block; D. display: content; Answer: B Explanation: display: block creates a block box that starts on a new line and stretches across the full width of its containing parent.
2. A student applies width: 250px; height: 60px; to a standard <span> tag, but the box size does not change. Why?
A. Because <span> tags only support percentages B. Because <span> is an inline element by default, which ignores width and height properties C. Because <span> tags require an external JavaScript library D. Because the browser has a bug Answer: B Explanation: Pure inline elements (display: inline) cannot have explicit width or height values applied. Setting display: inline-block or display: block is required.
3. What makes display: inline-block unique compared to display: inline and display: block?
A. It makes elements float to the top of the browser window B. It allows elements to sit side-by-side on the same line while still respecting custom width, height, and all 4 margins C. It hides elements from view on mobile devices D. It only works on tablet devices Answer: B Explanation: display: inline-block combines the horizontal flow of inline elements with the complete box-model sizing capabilities of block elements.
4. Which of the following HTML elements is a block-level element by default?
A. <strong> B. <span> C. <div> D. <a> Answer: C Explanation: <div> (along with <p>, <h1>, <ul>, etc.) is a block-level element by default. <span>, <a>, and <strong> are inline elements.
5. If you want a navigation hyperlink <a> to stretch across the full width of a mobile sidebar menu, which CSS rule should you write?
A. a { display: block; width: 100%; } B. a { display: inline; width: 100%; } C. a { flow: horizontal; } D. a { text-width: full; } Answer: A Explanation: Setting display: block; width: 100%; converts the naturally inline <a> tag into a full-width block container.
Practice Challenge (Try It Yourself)
- 1Create an HTML file named
display-comparison-lab.html. - 2Build an interactive demonstration comparing
block,inline, andinline-blocktags on the same page:
- 1Load the page in your browser and resize your window to observe how the three display modes behave! 🎯
Display: none vs Visibility: hidden: Hiding Elements in CSS
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.