Display & Visibility
The display property decides HOW an element participates in the page's layout, and it's more fundamental than most beginners realize. block elements (like <div>, <p>, <h1>) always start on a new line and stretch to fill their container's full width by default stacking vertically like paragraphs in a book. inline elements (like <span>, <a>, <strong>) flow within a line of text, sitting side by side, and importantly, width/height/margin-top/margin-bottom have NO effect on them. inline-block is a hybrid: it flows inline like text, but DOES respect width, height, and full margin/padding extremely useful for things like navigation links that need padding without breaking onto their own line. display: none completely removes an element from the page it takes up zero space, as if it was never in the HTML at all. visibility: hidden is different and often confused with it: the element becomes invisible, but it STILL takes up its normal space in the layout, leaving an empty gap where it would have been.
Why this matters
- ·inline-block gives you the best of both worlds: inline flow with block-level sizing control
- ·display: none is great for toggling menus or tabs completely out of the layout
- ·Understanding block vs inline explains why some elements stack and others sit side by side
Common mistakes
- ·Setting width/height on an inline element (without inline-block) silently does nothing
- ·Confusing display: none with visibility: hidden leads to unexpected empty gaps in a layout
- ·Overusing display: none for content that should be accessible can hide it from screen readers too
Frequently asked questions
<span> is inline by default, and inline elements ignore width/height entirely. Change it to display: inline-block or display: block to fix this.
Practice questions
Question 1
Change a <span> to inline-block and give it a width of 120px.
Question 2
Hide one of three boxes using display: none and observe the gap disappearing.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 10
