All three words have the same width: 140px and a dashed outline. The block one breaks the sentence
apart and takes the width. The inline one ignores the width and hugs the word. The inline-block one keeps the
sentence together and still gets its 140px. Most CSS layout questions come back to which of these three a box is.
block, inline and inline-block
| Value | New line? | width and height | Top and bottom margin | Starts as |
|---|---|---|---|---|
block | Yes, before and after | Work. Width fills the parent by default | Work | div, p, h1, ul |
inline | No | Ignored | Ignored | span, a, strong |
inline-block | No | Work | Work | button, input, select |
display: block
A block box takes the full width of its parent unless you give it a width, and the next thing always starts below it. Links are inline by default, so making one a block turns it into a box you can pad and size, with the whole area clickable:
Without display: block the two links would run together on one line, and the width and top/bottom
margin would be ignored. Delete it and see.
display: inline
Inline boxes are part of the text. They wrap across lines like words do, and they only grow as wide as their content. Give one a size and a margin and see which parts it actually takes:
The span asks for 200 by 60 pixels and a 30px margin all round. It gets none of the size, the 30px only on the left
and right, and its padding paints the yellow a little above and below the text without making its line any
taller. The left and right margins are the only thing that moves the words around it. If a span or
link needs real size, make it inline-block or block.
display: inline-block
Inline-block boxes line up like words but behave like little blocks inside: width, height, padding and margin all work. Tags, badges and small buttons are typical:
Each tag is exactly 80px wide, and they still flow onto the next line when there’s no room. One odd detail: the
gap between the tags isn’t a margin. It’s the line break between the <span> tags in the HTML,
shown as a space, about 4px at this font size. Put the tags on one line with no spaces and they touch. For rows
where the gap should be exact, a flex container with gap is easier (below).
What each tag starts as
Every element has a display value before you write any CSS. A button sitting next to a link but taking a width when the link won’t is down to this table. These are the defaults in Chrome, Edge and Firefox:
| Default display | Tags |
|---|---|
block | div, p, h1-h6, ul, ol, section, header, footer, form |
inline | span, a, strong, em, label, img |
inline-block | button, input, select, textarea |
list-item | li, summary |
table, table-cell… | table, td, th, tr |
img being inline is why images sit on the text baseline and leave a few pixels of space under them.
display: block on the image removes that gap. The div and span
tutorial has more on block and inline tags.
none vs visibility: hidden vs opacity: 0
Three ways to make something disappear, and they don’t mean the same thing. Each row below hides its middle button, Latte, a different way:
Row one closes up as if Latte was never there. Rows two and three keep an empty space. The real difference between the last two is something you can’t see: click the empty spot in row three, or press Tab through the buttons, and you’ll find the invisible Latte is still there.
| Hidden with | Keeps its space | Can be clicked | Reached with Tab | Read by screen readers |
|---|---|---|---|---|
display: none | No | No | No | No |
visibility: hidden | Yes | No | No | No |
opacity: 0 | Yes | Yes | Yes | Yes |
- Use
display: nonefor things that are closed or not relevant right now: a menu that isn’t open, a tab panel that isn’t selected. - Use
visibility: hiddenwhen the space must stay reserved, so nothing jumps when it shows up. A child can even setvisibility: visibleand appear inside a hidden parent, whichdisplay: nonenever allows. -
opacity: 0on its own is rarely what you want, since people can still click and Tab into something they can’t see. It’s useful in the middle of a fade, paired withvisibility(see fading in and out).
The hidden attribute can lose
HTML’s hidden attribute works by giving the element display: none from the browser’s own
stylesheet. Any display you set in your CSS is stronger, so this row is supposed to be hidden and isn’t:
.row { display: flex } overrides the attribute. Many stylesheets add this one rule near the
top so hidden always wins:
Hidden on screen, still read aloud
Sometimes you want the opposite: text that isn’t visible but that screen readers still announce, like a label for an icon-only button. None of the three above do that. The usual answer is a small class that shrinks the text to a 1-pixel box and clips it:
.sr-only {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
} <button>
<span aria-hidden="true">🛒</span>
<span class="sr-only">View your order</span>
</button>
Sighted visitors see a cart; a screen reader says “View your order, button”. The emoji gets
aria-hidden="true" so it isn’t read out as well.
display: flex and display: grid
These two values change how the element lays out its children. The element itself still acts like a block on the outside. Here three links become a nav bar with one line of CSS:
Inside a flex or grid container, the children’s own display mostly stops mattering: a span or
a there is treated as a block, so it can take a width without inline-block. If you want
the container itself to sit inline in a sentence, use inline-flex or inline-grid. Newer
CSS also lets you write those as two words, display: inline flex, which current browsers understand.
Flex and grid each have a tutorial of their own: flexbox for rows and columns of things, grid for two-dimensional layouts.
display: contents
display: contents makes the element’s own box disappear while its children stay. They act as if they
belonged to the element’s parent. That helps when a wrapper gets in the way of a grid:
The <section> around Latte and Mocha would normally be one grid cell holding both. With
display: contents, Latte and Mocha become grid items themselves and all four sit in a row of three
plus one. Remove the .drinks rule to compare. The wrapper’s own background, border and padding
vanish along with its box.
There’s an accessibility history here. For years, browsers also dropped the element from the accessibility tree,
so a <ul> or <table> with display: contents stopped being
announced as a list or a table. Most of those bugs have since been fixed, and in Chromium a list and a table with
display: contents kept their roles when this page was written. Buttons are another story: in our test
a <button> with display: contents could no longer be reached with Tab. Use it on
plain wrappers like div and section, and keep it off buttons, links and form fields.
display: list-item and lost bullets
The bullet next to a list item comes from display: list-item, not from the <li> tag.
Give a div that value and it gets a bullet; give an li a different one and its bullet goes
away:
The two divs show bullets (they need the left margin, because the bullet sits outside the box). The Tea item has
display: flex, so it lost its bullet while Juice kept one. If you want flex inside a list item and a
bullet too, put the flex on an element inside the li instead. Screen readers still treat the Tea item
as part of the list; only the marker is gone.
What display doesn’t change
display changes how a box looks and lays out. It doesn’t change what the HTML is allowed to contain,
because the browser builds the page from the HTML before any CSS is applied. The common case is a block tag
inside a paragraph:
Both lines try to say “Hello Sam and welcome back” in red. In the first, the browser closes the paragraph as soon
as it meets the <div>, so “Sam” and the rest end up outside it and lose the red color (and a
stray empty paragraph appears). The second uses a <span> with display: block, which
is allowed inside a paragraph, so all of it stays red. When you need a block inside text, make an inline tag a
block rather than the other way round.
Display mix-ups
- Width on a span or link does nothing. It’s inline. Use
inline-blockorblock, or put it in a flex container. - Animating
display: none. By default it switches at once, so a fade never plays. Fadeopacityand flipvisibilityinstead, or, in newer browsers, addallow-discreteand@starting-styleas shown in the transitions tutorial. - Hiding with
display: noneand expecting a screen reader to find it. It can’t. Use the visually hidden class for text that should only be heard. - Setting
display: blockon a flex item to “make it a block”. It already is one, and the line does nothing. Look at the container’sflex-directionor the item’sflexinstead. - A typo.
display: inline-blocksordisplay: flexboxisn’t a value, so the browser keeps the default. The CSS Validator flags it.
Questions people ask
What is the difference between block, inline and inline-block?
A block element starts on a new line, fills the width of its parent and accepts width, height and margins. An inline element sits inside a line of text and ignores width, height and top and bottom margins. inline-block stays in the line like inline, but takes width, height and margins like a block, which suits tags, badges and small buttons.
What is the difference between display: none and visibility: hidden?
display: none removes the element from the layout, so the things around it close up. visibility: hidden hides it but keeps its space, so nothing moves. In both cases the element can't be clicked, reached with Tab or read by a screen reader. A child can set visibility: visible to show inside a hidden parent; with display: none nothing inside can show.
How do I put two divs side by side?
A div is a block, so each one starts on a new line. Put display: flex on their parent and they sit in a row: .row { display: flex; gap: 16px; }. Setting both divs to display: inline-block also works, but you get a small gap from the whitespace between them and they line up on the text baseline, so flex is usually easier.
Why does width not work on my span?
A span is inline by default, and inline boxes ignore width and height. Give it display: inline-block to keep it in the line with a size, or display: block to put it on its own line. Inside a flex or grid container it is treated as a block automatically, so width works there too.
Why is the hidden attribute not hiding my element?
The hidden attribute only sets display: none in the browser's own stylesheet, and any display value in your CSS beats it. A rule like .row { display: flex; } makes a hidden row show again. Add [hidden] { display: none !important; } to your stylesheet so the attribute always wins.
What does display: contents do?
It removes the element's own box but keeps its children, which then act as children of the element's parent. It helps when a wrapper div gets in the way of a flex or grid layout. The wrapper's background, border and padding disappear with it. Avoid it on buttons, links and form fields: in Chromium a button with display: contents can no longer be reached with Tab.
Why did my list lose its bullets after adding display: flex?
The bullet comes from display: list-item, which li elements have by default. Setting display: flex on the li replaces it, so the marker goes away. Put the flex layout on an element inside the li instead, or add your own marker with ::before.
How do I hide text visually but keep it for screen readers?
Use a visually hidden class: position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap;. The text is clipped down to a single pixel, but screen readers still read it, which is useful for labels on icon-only buttons. display: none and visibility: hidden would hide it from screen readers too.