Skip to content
Kuza Ndoto Academy

Display & Visibility

Beginner → IntermediateChapter 2 · Page 9 of 223 min readUpdated 15 Nov 2026

The display property decides how an element's box behaves in the layout: does it start a new line, sit inside a line of text, become a flex or grid container, or disappear completely? Together with visibility and overflow, it controls what is shown and how.

block, inline and inline-block

Every HTML element has a default display value. Headings, paragraphs, div, section and lists are block; span, a, strong and em are inline. (The HTML series covers this in Block & Inline Elements.) CSS lets you change the default.

ValueNew line?Width and heightVertical padding/margin
blockYes; fills the available widthRespectedPushes other elements away
inlineNo; flows within textIgnoredPadding is drawn but doesn't push lines apart; vertical margins have no effect
inline-blockNo; sits in the line like a wordRespectedPushes other elements away
block new line, full width <div> block 1 <p> block 2 inline flows with the text <a> <strong> (cont.) inline-block in the line, own size 90 × 46 90 × 46 grey bars = ordinary text in the same line
Figure 1. Block boxes stack vertically. Inline boxes wrap with the text and can even break across lines. Inline-block boxes stay in the line but keep the width and height you give them.

inline-block is handy for buttons and tags that sit in a sentence but need padding and a fixed size. For arranging several items in a row, though, Flexbox is now the better tool.

Layout display values

display: flex and display: grid turn an element into a layout container whose children are arranged by Flexbox or Grid rules. The container itself still behaves like a block on the outside. inline-flex and inline-grid do the same but the container behaves like an inline-block.

Hiding elements

There are several ways to hide something, and they are not the same:

TechniqueTakes up space?Read by screen readers?Clickable?
display: noneNo; the layout closes the gapNoNo
visibility: hiddenYes; leaves an empty gapNoNo
opacity: 0YesYesYes (invisible but still there)

The HTML hidden attribute works like display: none. Use display: none for things like a closed mobile menu, visibility: hidden when you need the space reserved, and opacity mainly for fade animations (because opacity can be transitioned smoothly).

Overflow

When content is bigger than its box (for example a fixed-height box with lots of text), overflow decides what happens:

  • visible (default): content spills out of the box.
  • hidden: extra content is clipped and can't be seen.
  • scroll: scrollbars always appear.
  • auto: scrollbars appear only when needed. This is usually what you want.

Use overflow-x and overflow-y to control one direction. overflow-x: auto on a wrapper around a wide table lets it scroll sideways on phones instead of breaking the page.

Try it

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Display</title>
  <style>
    body { font-family: system-ui, sans-serif; margin: 1.5rem; }
    .tag {
      display: inline-block;
      width: 90px;
      padding: 0.3rem;
      text-align: center;
      background: #fde68a;
      border-radius: 6px;
    }
    .gone { display: none; }
    .ghost { visibility: hidden; }
    .scroll-box {
      height: 70px;
      overflow: auto;
      border: 2px solid #1e3a8a;
      padding: 0.5rem;
    }
  </style>
</head>
<body>
  <p>Skills: <span class="tag">HTML</span> <span class="tag">CSS</span> <span class="tag">JS</span></p>
  <p>A <span class="gone">HIDDEN</span> B <span class="ghost">HIDDEN</span> C</p>
  <div class="scroll-box">
    Line one.<br>Line two.<br>Line three.<br>Line four.<br>Line five.
  </div>
</body>
</html>

The three skill tags sit in the sentence as equal-width yellow boxes. The second line reads "A B" followed by a wide empty gap and then "C": the display: none word left no trace, but the visibility: hidden word kept its space. Below, a blue-bordered box shows the first few lines with a scrollbar for the rest.

Key points

  • block starts a new line; inline flows in text and ignores width and height; inline-block mixes both.
  • display: flex and display: grid create layout containers.
  • display: none removes an element; visibility: hidden hides it but keeps its space.
  • overflow: auto adds scrollbars only when content doesn't fit.
Up next · New chapter: LayoutPositioningstatic, relative, absolute, fixed and sticky.
→

Tip: use the ← and → keys to move between pages.