Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
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:
display: block — The full-width line-takersdisplay: inline — The sentence-friendly word-flowsdisplay: inline-block — The superhero hybrid combining the best of both worldsTo 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! | +-------------------------------------------------------------------------+
display: blockA block-level element is a structural container. By default, it behaves like a rigid brick:
width, height, padding, and margin work fully on all 4 sides (top, right, bottom, left).<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 | +-------------------------------------------------------------+
display: inlineAn inline element is designed to sit directly inside text content without disturbing the flow of the sentence:
width and height properties!padding-left and padding-right work normally, vertical padding-top, padding-bottom, margin-top, and margin-bottom do NOT push surrounding text lines away cleanly.<span>, <a>, <strong>, <em>, <code>, <label>, <small>.
display: inline-blockWhat 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:
inline element.width, height, margin, and padding on all 4 sides like a block element!+---------------------+ +---------------------+ +---------------------+ | [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!
| 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 |
Remember: HTML tags do not have permanently carved-in-stone display behaviors! You can change any element's display using CSS:
| 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;. |
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, ignores width/height and vertical margins (span, a, strong).display: inline-block: Sits side-by-side like inline, but respects width, height, and all 4-sided margins/paddings like block.display property.width: 250px; height: 60px; to a standard <span> tag, but the box size does not change. Why?display: inline-block unique compared to display: inline and display: block?<a> to stretch across the full width of a mobile sidebar menu, which CSS rule should you write?display-comparison-lab.html.block, inline, and inline-block tags on the same page:In the next lesson, we will continue your web styling journey with Display: none vs Visibility: hidden: Hiding Elements in CSS (Display and Visibility).
👉 Continue to Next Lesson: Display: none vs Visibility: hidden: Hiding Elements in CSS →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.