In the HTML course you learned that some elements are block and some are inline. The CSS display property lets you change that behaviour. You can turn a link into a block, line a list up in a row, or hide something completely. display is also the switch that turns on Flexbox and Grid, the two main layout systems in CSS.
Because it changes how an element takes up space, display affects everything around it too. Understanding its main values explains why a width you set seems to be ignored, or why an element suddenly jumps onto its own line.
The main display values
| Value | Behaviour |
|---|---|
block | Starts on a new line, fills the full width, respects width, height and all margins |
inline | Flows with the text, ignores width, height and top and bottom margins |
inline-block | Flows with the text but respects width, height and all spacing |
none | Removed from the page completely |
flex | Becomes a Flexbox container |
grid | Becomes a Grid container |
Every element has a default display value set by the browser. <div> and <p> are block, while <a> and <span> are inline. CSS lets you override that default without changing the HTML.
inline vs inline-block
Inline elements like <a> and <span> ignore width, height and vertical margins. That's why vertical padding on a link can overlap the lines of text above and below it.
Switch the element to inline-block and it behaves like a small box that still sits in the line of text. That's ideal for buttons, tags and badges, which need padding on all sides but shouldn't force a line break.
Ways to hide an element
| CSS | Visible? | Takes up space? | Read by screen readers? |
|---|---|---|---|
display: none | No | No | No |
visibility: hidden | No | Yes | No |
opacity: 0 | No | Yes | Yes |
The HTML hidden attribute has the same effect as display: none. Choose based on what you need: display: none for content that shouldn't exist right now, visibility: hidden to keep the layout from shifting, and opacity when you plan to fade the element in with a transition.
CSS Display Syntax
Display values
a.button { display: inline-block; }
.menu { display: flex; }
.gallery { display: grid; }
.hidden { display: none; }CSS Display Summary
displaycontrols how an element takes up space on the page.blockstarts a new line;inlineflows within text.inline-blockflows within text but respects width, height and spacing.display: noneremoves an element;visibility: hiddenhides it but keeps its space.display: flexanddisplay: gridturn on the two main layout systems.
CSS Display Examples
Each example runs in the page. Edit the code and press Run to see what changes.
block, inline and inline-block
All three spans get the same width, height and margin. Only two of them listen.
<p>Before <span class="i">inline</span> after.</p>
<p>Before <span class="ib">inline-block</span> after.</p>
<p>Before <span class="b">block</span> after.</p>span {
width: 120px;
height: 40px;
margin: 10px;
padding: 4px;
background: #ffd23f;
border: 2px solid #15161b;
}
.i { display: inline; }
.ib { display: inline-block; }
.b { display: block; }Line-by-line explanation
- Lines 2 to 4Everyone gets the same size and spacing.
- Line 10Inline ignores width, height and vertical margin.
- Line 11Inline-block respects them but stays in the line.
- Line 12Block takes its own line.
Turning links into button-like tabs
<nav class="tabs">
<a href="#">Overview</a>
<a href="#" class="active">Reviews</a>
<a href="#">Specs</a>
</nav>.tabs a {
display: inline-block;
padding: 10px 18px;
margin-right: 4px;
border-radius: 10px 10px 0 0;
background: #e5e7eb;
color: #15161b;
text-decoration: none;
}
.tabs .active {
background: #4467ff;
color: white;
}Line-by-line explanation
- Line 2inline-block lets the padding and rounded corners work properly.
- Lines 11 to 14The active tab stands out.
none vs hidden
<div class="row">
<span class="box">1</span>
<span class="box gone">2</span>
<span class="box">3</span>
</div>
<div class="row">
<span class="box">1</span>
<span class="box ghost">2</span>
<span class="box">3</span>
</div>.box { display: inline-block; width: 50px; padding: 10px 0; text-align: center; background: #d6f4e4; border: 2px solid #17a865; }
.gone { display: none; }
.ghost { visibility: hidden; }Line-by-line explanation
- Line 3Box 2 is removed, so box 3 slides into its place.
- Line 4Box 2 is invisible but still holds its space.
Common CSS Display Mistakes
Setting width on an inline element
a.btn { width: 200px; height: 50px; }a.btn { display: inline-block; width: 200px; height: 50px; }Links are inline, so width and height are ignored until you change display.
Hiding content with display: none that should stay accessible
.label-text { display: none; } /* screen readers can't read it either */.sr-only {
position: absolute; width: 1px; height: 1px;
overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}To hide something visually but keep it for screen readers, use a "visually hidden" class like this one.
CSS Display in Real Projects
Display everywhere
Navigation bars (flex), photo galleries (grid), pill-shaped tags (inline-block) and mobile menus that open and close (none ↔ block) all rely on display. It's one of the most used properties in every stylesheet.
CSS Display Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Let the link accept width and padding while staying in line.
.tag {
display: ;
padding: 4px 10px;
}
inline-block sits inline but respects box sizing.Which value removes an element from the page completely?
display: none removes it from layout and from screen readers.What's the difference between display: none and visibility: hidden?
The three .chip links should sit in a row, each 100px wide with 8px padding, and the .secret paragraph should be removed from the layout. Fix the CSS.
<a class="chip" href="#">Red</a>
<a class="chip" href="#">Green</a>
<a class="chip" href="#">Blue</a>
<p class="secret">You shouldn't see me</p>
<p>Visible text</p>.chip {
display: block;
width: 100px;
padding: 8px;
background: #eee;
}
.secret {
visibility: hidden;
}- Chips sit side by side
- Chips are 100px wide (plus padding)
- The secret is removed from the layout
CSS Display Quiz
3 questions to check what you learned.
Which display value makes an element a Flexbox container?
- flex
- block
- inline
- box
Does
widthwork on an element withdisplay: inline?- Yes
- Only in percentages
- No
- Only on links
Which hidden element is still announced by screen readers?
- display: none
- visibility: hidden
- The
hiddenattribute - opacity: 0
CSS Display Challenge
Make a badge row
Without changing the HTML:
- Make every
.badgeaninline-blockwithpadding: 6px 12pxand amargin-rightof6px - Hide the
.oldbadge completely withdisplay: none
<span class="badge">New</span>
<span class="badge">Sale</span>
<span class="badge old">Last year</span>
<span class="badge">Free shipping</span>.badge { background: #fff1c2; border: 2px solid #15161b; border-radius: 999px; font: 600 14px sans-serif; }
- Badges are inline-block
- Padding 6px 12px
- 6px right margin
- Old badge is gone