On this page10 sections
Lecture 015: Display: Block, Inline & None
| Category | 4: Layout Basics |
| Lecture | 015 of 057 |
| Estimated video length | 12 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
The
displayproperty 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,inlineandinline-block - Hide elements with
display: noneandvisibility: hidden, and know the difference - Know that
display: flexanddisplay: gridstart 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:
<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
<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
<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
<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>| Property | Visible? | Takes up space? |
|---|---|---|
display: none | No | No. The others move up |
visibility: hidden | No | Yes. A gap is left |
Menus that open and close usually switch between display: none and display: block.
5. A Preview: flex and grid
.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
- Which
displayvalue makes an element start on a new line and fill the width? - Why doesn't
width: 100pxwork on a<span>? - Which value lets elements sit side by side and accept a width?
- What's the difference between
display: noneandvisibility: hidden? - Make every link inside
.sidebara full-width block. - Which two
displayvalues turn on the modern layout systems?
Practice Answers
block- A span is inline, and inline elements ignore width and height.
inline-blockdisplay: noneremoves the element and its space;visibility: hiddenhides it but keeps the space..sidebar a { display: block; }flexandgrid.
Key Takeaways
block= own line, full width;inline= inside text, no width/height.inline-block= side by side, with width, height and padding.display: noneremoves an element;visibility: hiddenleaves a gap.display: flexanddisplay: gridstart Flexbox and Grid.