Reading history

CSS display and visibility: block, inline, none

7 min read 0 views

Learn CSS display values block, inline and inline-block, and compare display: none, visibility: hidden, opacity: 0 and the hidden attribute.

Every element on a page has a display type that decides how it takes up space. Some start on a new line and fill the width, some sit inside a line of text, and some are not drawn at all. Knowing the difference explains many layout puzzles, such as why width does nothing on a <span>. This lesson covers block, inline and inline-block, and then compares the four ways to hide an element.

In this lesson you will learn

  • How block, inline and inline-block elements behave
  • Which elements are block or inline by default
  • Where flex, grid and none fit in
  • The difference between display: none, visibility: hidden, opacity: 0 and hidden
  • Hiding content visually while keeping it for screen readers

You should know the CSS box model.

Block, Inline and Inline-Block

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>display values</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 14px; }
    .box { background: #bbdefb; border: 2px solid #1565c0; padding: 6px; margin: 4px;
           width: 120px; height: 30px; }
    .block  { display: block; }
    .inline { display: inline; }          /* width, height and top/bottom margin are ignored */
    .ib     { display: inline-block; }     /* sits in the line but keeps its size */
    h3 { font-size: 13px; margin: 10px 0 2px; color: #455a64; }
  </style>
</head>
<body>
  <h3>display: block (each on its own line, full control of size)</h3>
  <div class="box block">A</div><div class="box block">B</div>

  <h3>display: inline (flows with the text, width and height ignored)</h3>
  <p>Text before <span class="box inline">C</span> <span class="box inline">D</span> text after.</p>

  <h3>display: inline-block (in the line, but sized like a block)</h3>
  <p>Text before <span class="box ib">E</span><span class="box ib">F</span> text after.</p>
</body>
</html>

Result

  • A block element starts on a new line and, unless you set a width, stretches across its container. Width, height, padding and margin all work as you expect. <div>, <p>, <h1> to <h6>, <ul> and <section> are block by default.
  • An inline element flows inside a line of text, like a word. Its width and height are ignored, and top and bottom margins do not push other lines away. <span>, <a>, <strong> and <em> are inline by default.
  • inline-block mixes the two: the element sits in the line, but it keeps its width, height and vertical spacing. It is handy for buttons and badges inside text.
  • The display type changes how an element is laid out, not what it means. A link styled with display: block is still a link.

Four Ways to Hide an Element

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Hiding elements</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 14px; }
    .row { display: flex; gap: 6px; margin-bottom: 10px; align-items: center; }
    .row span { background: #c8e6c9; border: 1px solid #2e7d32; padding: 8px 12px; }
    .row b { width: 170px; font-weight: normal; color: #455a64; }
    .none   { display: none; }        /* removed from the layout */
    .hidden { visibility: hidden; }   /* invisible, but still takes up space */
    .clear  { opacity: 0; }           /* invisible, takes space, still clickable */
  </style>
</head>
<body>
  <div class="row"><b>normal</b><span>1</span><span>2</span><span>3</span></div>
  <div class="row"><b>display: none</b><span>1</span><span class="none">2</span><span>3</span></div>
  <div class="row"><b>visibility: hidden</b><span>1</span><span class="hidden">2</span><span>3</span></div>
  <div class="row"><b>opacity: 0</b><span>1</span><span class="clear">2</span><span>3</span></div>
  <div class="row"><b>hidden attribute</b><span>1</span><span hidden>2</span><span>3</span></div>
</body>
</html>

Result

  • display: none removes the element from the layout, so box 3 moves up to fill the gap. Screen readers also skip it.
  • visibility: hidden makes the element invisible but keeps its space, leaving a hole. Screen readers skip it too.
  • opacity: 0 makes it fully transparent. It keeps its space, can still be clicked, and is still read aloud, so use it mainly as the start or end of a fade animation.
  • The HTML hidden attribute works like display: none. It is a good choice when content is not relevant yet, such as the next step of a form. A CSS rule that sets display on the element can override it.

display Values at a Glance

ValueNew line?Width and height work?Typical use
blockYesYesSections, paragraphs, cards
inlineNoNoWords, links, emphasis
inline-blockNoYesButtons and badges in text
flexYesYesRows or columns of items (Flexbox)
gridYesYesTwo-dimensional layouts (Grid)
inline-flexNoYesAn icon and label inside a button
noneNot drawnn/aHiding content completely

Hiding Visually but Not from Screen Readers

Sometimes text should be read aloud but not shown, such as a label for an icon-only button. None of the four methods above does this. Use a “visually hidden” class instead:

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

The text is still on the page for assistive technology, but it occupies one invisible pixel. See Web Accessibility Basics for more.

Common Mistakes

  1. Setting width or vertical margin on an inline element and wondering why nothing changes.
  2. Hiding a menu with opacity: 0, which leaves invisible links that keyboard users can still tab to.
  3. Using display: none on text meant for screen readers, which hides it from them too.
  4. Gaps between inline-block items caused by the spaces in your HTML. Flexbox with gap avoids them.

Practice Exercises

Exercise 1. Make links in a navigation bar sit side by side, each 100px wide with 8px of vertical padding.

Show answer
nav a {
  display: inline-block;
  width: 100px;
  padding: 8px 0;
  text-align: center;
}

Exercise 2. A “Loading…” message should disappear completely once data has arrived, and the content below should move up. Which method fits?

Show answer

display: none or the hidden attribute, because both remove the message from the layout. visibility: hidden would leave an empty gap.

Frequently Asked Questions

Can I animate display: none?

Traditionally no: an element jumps between shown and hidden. A common workaround is to animate opacity and switch visibility at the end. Newer browsers can also transition display with transition-behavior: allow-discrete.

Is an image inline or block?

<img> is inline by default, which is why a small gap often appears under images: it sits on the text baseline. Setting display: block on the image removes the gap.

Summary

  • Block elements start new lines; inline elements flow in text and ignore width and height.
  • inline-block sits in a line but keeps its size.
  • display: none and hidden remove an element; visibility: hidden keeps its space.
  • Use a visually-hidden class for text that only screen readers should read.

Next: CSS Overflow and Text Wrapping.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *