TExcelTAD
Home › CSS › CSS Display

CSS Display

display is the most important property for layout. It decides what kind of box an element generates: whether it starts on a new line, whether it accepts width and height, and how its children are laid out.

Block and inline

Every HTML element has a default display value.

Type Starts on a new line? Width by default Accepts width/height? Examples
block Yes Full width of parent Yes div, p, h1–h6, section, ul
inline No Only as wide as content No span, a, strong, em
inline-block No As wide as content Yes button, input behave like this
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .b { display: block; background: #90caf9; margin: 4px 0; }
  .i { display: inline; background: #ffcc80; width: 300px; /* ignored */ }
  .ib { display: inline-block; background: #a5d6a7; width: 110px; height: 40px; }
</style>
</head>
<body>
  <span class="b">block span</span>
  <span class="b">block span</span>
  <div class="i">inline div</div> <div class="i">inline div</div>
  <p>Text <span class="ib">inline-block</span> flows <span class="ib">with sizes</span> in the line.</p>
</body>
</html>

You can change any element's display. A common example: turning list items into a horizontal menu with display: inline or inline-block, or turning links into block so the whole row is clickable.

Hiding elements: display vs visibility

Declaration Visible? Takes up space? Read by screen readers?
display: none No No — removed from layout No
visibility: hidden No Yes — leaves a gap No
opacity: 0 No Yes Yes
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 20px; }
  .col div { width: 90px; padding: 10px; margin: 4px 0; background: #ce93d8; text-align: center; }
  .none { display: none; }
  .hidden { visibility: hidden; }
</style>
</head>
<body>
  <div class="col"><div>A</div><div class="none">B</div><div>C</div><p>display: none</p></div>
  <div class="col"><div>A</div><div class="hidden">B</div><div>C</div><p>visibility: hidden</p></div>
</body>
</html>

Tip: The HTML hidden attribute works like display: none, and is handy for toggling content with JavaScript.

Layout display values

Some display values change how the element's children are arranged. You'll study them in detail in the Layout section.

Value Children are laid out as
flex / inline-flex A flexible row or column (Flexbox)
grid / inline-grid A two-dimensional grid
table, table-row, table-cell Like an HTML table
list-item A block with a marker, like <li>
contents The element's own box disappears; children act as if they were its parent's children
flow-root A block that contains its floats
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .row { display: flex; gap: 8px; margin-bottom: 12px; }
  .grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .row div, .grid div { background: #4db6ac; color: white; padding: 12px; text-align: center; border-radius: 6px; }
</style>
</head>
<body>
  <div class="row"><div>flex</div><div>items</div><div>in a row</div></div>
  <div class="grid"><div>grid 1</div><div>grid 2</div><div>grid 3</div><div>grid 4</div><div>grid 5</div><div>grid 6</div></div>
</body>
</html>

Toggling display with :hover

A simple show/hide effect: a hidden element becomes visible when its parent is hovered.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 20px; }
  .card { width: 220px; padding: 14px; background: #fff8e1; border: 2px solid #ffb300; border-radius: 10px; }
  .more { display: none; margin-top: 8px; color: #6d4c41; }
  .card:hover .more { display: block; }
</style>
</head>
<body>
  <div class="card">
    <strong>Hover me</strong>
    <div class="more">Surprise! I was display: none until you hovered the card.</div>
  </div>
</body>
</html>

Test yourself

Which display value sits in a line of text but still accepts width and height?

  1. block
  2. inline
  3. inline-block
  4. contents

What is the difference between display: none and visibility: hidden?

  1. None; they are identical
  2. display: none removes the space; visibility: hidden keeps it
  3. visibility: hidden removes the space
  4. display: none only hides text

What is the default display of a <span>?

  1. block
  2. inline
  3. inline-block
  4. flex