Display Property — CSS

The display property decides how an element takes up space on the page. block takes a whole line, and width/height apply. inline flows inside a line of text…

You give a <span> a width: 120px, but its width doesn't change at all. A <div> with the same value becomes 120px wide right away. The cause of the difference is the display property.

display decides how an element takes up space on the page: it takes a whole line, it flows inside a line of text, or it doesn't show at all. Every tag already has a default display value. <div>, <p> and <h1> are block by default, and <li> behaves like block. <span>, <a> and <strong> are inline by default.

block, inline and inline-block

This example has three <div> elements with display: inline-block:

<div class="item">A</div>
<div class="item">B</div>
<div class="item">C</div>

<style>
.item {
  display: inline-block;
  width: 120px;
  height: 44px;
  background: lightblue;
}
</style>

The result: three blue boxes, 120 × 44px each, side by side. Compare these three display values:

That is why the <span> at the start of this lesson didn't get wider: a <span> is an inline element, so the browser ignores its width and height. To make the size apply, give the <span> display: inline-block or block.

inline-block is also how you make a menu whose items sit side by side. List items <li> also take a whole line, so they are stacked one below the other. With display: inline-block, the items sit side by side, and their padding still applies.

Hiding with display: none

This rule hides every element that has the class ad:

.ad {
  display: none;
}

display: none makes the element disappear completely, as if it were not on the page. The element below it moves up into its place right away. Screen readers don't read it either. .ad is written with a dot in front because ad is a class name.

visibility: hidden is different. The element can't be seen either, but its place is kept, so the page has an empty space the size of the element.

Ways to hide an element:

Way Result When to pick it
display: none, or the hidden attribute on the HTML tag Gone for everyone, with no space A part you really don't use, like the ads in this example
visibility: hidden Can't be seen, but keeps its space So the page layout doesn't shift
opacity: 0 See-through, but still clickable and still read by screen readers Only during a fade animation, together with pointer-events: none so it can't be clicked
the .sr-only class below Can't be seen, but still read by screen readers Text for a button that only shows an icon

The .sr-only class makes its box only 1 pixel wide and 1 pixel tall, and cuts off the text that doesn't fit. Some of its properties are covered in later lessons:

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

<button>✕ <span class="sr-only">Close menu</span></button> shows only ✕, but a screen reader reads it as "✕ Close menu".

A value for the parent: flex

This example puts display: flex on a menu:

.menu {
  display: flex;
}

The values block, inline, inline-block and none change the element they are written on. display: flex is different: you put it on the parent, and all the children of that parent are placed in one row automatically. The row can be changed into a column. The value is written flex, not flexbox. There is also display: grid, for a layout with rows and columns at the same time. Flex and grid have lessons of their own.

⚠️ Elements that won't hide, and gaps that appear by themselves

TL;DR: The display property decides how an element takes up space on the page. block takes a whole line, and width/height apply. inline flows inside a line of text and ignores width/height. inline-block sits side by side but its size still applies, which suits a menu with items side by side. none removes the element and its space, while visibility: hidden hides the element but leaves an empty space. display: flex goes on a parent and places its children in one row.

⚡ Side by side, with a size

Write:

.item {
  display: inline-block;
  width: 120px;
  height: 44px;
  background: lightblue;
}
Guess first: this rule is applied to three <div> elements. Without display: inline-block, how are the three arranged?

One below the other. The default display of a <div> is block, so each box takes a whole line.

Meaning: The display property decides how an element takes up space on the page. block takes a whole line. inline flows inside a line of text and ignores width and height: that is why a <span>, which is inline by default, can't be made wider. inline-block sits side by side like inline, but width and height still apply.

⚡ Gone completely, or a space left

Write:

.ad {
  display: none;
}
Guess first: if display: none is replaced with visibility: hidden, is the result different?

Yes. Both make the element impossible to see. With visibility: hidden, the element's place is kept, so there is an empty space the size of the element. With display: none, its place is gone too.

Meaning: display: none makes the element disappear completely, and the element below it moves up into its place.

⚡ flex goes on the parent

Meaning: display: flex is written on the parent, not on the elements you want to arrange. All the children of that parent are then placed in one row. The value is written flex, not flexbox.

What you will learn