Skip to the content

Lesson 20 of 296

display and position

What you will learn

  • The display values and what each does
  • Ways to hide elements and the differences between them
  • The five position values
  • z-index and stacking context
  • overflow
On this page

display

display decides how an element's box behaves in the layout.

ValueBehaviour
blockNew line, full width. Width, height, margin and padding all work.
inlineStays in the line of text. Width and height are ignored. Top and bottom margins do not push other lines away.
inline-blockStays in the line like inline, but width, height and all margins work.
flexMakes the element a Flexbox container (Lesson 21)
gridMakes the element a Grid container (Lesson 22)
noneRemoves the element completely
a.button {
  display: inline-block;
  padding: 10px 20px;
}

Links are inline by default. Making them inline-block lets padding and sizes work fully while they stay in the line.

Ways to hide things

MethodVisible?Takes space?Read by screen readers?Clickable?
display: noneNoNoNoNo
visibility: hiddenNoYesNoNo
opacity: 0NoYesYesYes
.sr-only (below)NoNo (tiny)YesNo
hidden HTML attributeNoNoNoNo

Hidden for eyes, available for screen readers: a common class, often called sr-only (screen reader only):

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

Use it for labels that are clear visually from icons but still need text for screen readers.

position

position changes how an element is placed. Use it together with top, right, bottom and left.

ValueWhere it is placedKeeps its space in the layout?
staticNormal place in the page. The default. top and left do nothing.Yes
relativeIts normal place, then moved by top/leftYes, the original space stays
absoluteRelative to the nearest positioned ancestorNo, removed from the flow
fixedRelative to the screen. Stays put when you scroll.No
stickyNormal place, until you scroll to a point, then it sticksYes

absolute: the most important rule

An absolutely positioned element is placed relative to the nearest ancestor whose position is not static. If there is none, it uses the page itself.

That is why you usually set position: relative on the parent:

.card {
  position: relative;
}
.card .badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

The badge sits in the top-right corner of the card. Without position: relative on .card, it would go to the top-right corner of the page.

fixed and sticky

.site-header {
  position: sticky;
  top: 0;
}

The header scrolls normally until it reaches the top of the screen, then stays there. sticky needs a top (or other side) value to work, and it stops working if a parent has overflow: hidden.

.chat-button {
  position: fixed;
  bottom: 20px;
  right: 20px;
}

The button always stays in the bottom-right corner of the screen.

Centring with absolute

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

top and left at 50% put the modal's top-left corner in the centre. The transform then moves it back by half its own width and height, so its centre is in the centre.

z-index: what goes on top

When positioned elements overlap, z-index decides which one is in front. A bigger number is closer to the user.

.dropdown { position: absolute; z-index: 10; }
.modal    { position: fixed;    z-index: 100; }

z-index only works on elements that are positioned (not static), or that are flex or grid items.

Stacking context: why z-index 9999 "does not work"

Some properties create a stacking context: a separate group of layers. Examples: a positioned element with a z-index, opacity less than 1, transform, and filter.

Elements inside a stacking context can only be ordered inside that group. If the parent is behind another element, its children are also behind it, even with z-index: 9999.

Fix: find the parent that creates the stacking context (use dev tools), and change the z-index of that parent, or move the element (for example, put modals at the end of <body>).

Tip: keep a small scale of z-index values in your project, such as 10 for dropdowns, 100 for headers, 1000 for modals, instead of random large numbers.

overflow

overflow decides what happens when content is bigger than its box.

ValueEffect
visibleContent spills out (the default)
hiddenExtra content is cut off
scrollAlways shows scroll bars
autoShows scroll bars only when needed. Usually the best choice.

overflow-x and overflow-y control each direction separately.

Common mistakes

  • Setting width on an inline element and wondering why nothing changes.
  • Forgetting position: relative on the parent of an absolute element.
  • position: sticky with no top value.
  • Using huge z-index numbers to beat a stacking context.
  • Using opacity: 0 to hide something, so invisible buttons can still be clicked.

How to say it in an interview

"Block elements take the full width, inline elements sit in the text flow and ignore width and height, and inline-block is a mix. For position, relative offsets an element but keeps its space; absolute removes it from the flow and positions it against the nearest positioned ancestor; fixed is relative to the viewport; sticky switches between normal and fixed at a scroll point. z-index only works within a stacking context, which things like transform and opacity create, so a high z-index can still lose."

Key words

WordMeaning
displayHow an element's box behaves in the layout
Normal flowThe default way elements are laid out, one after another
Positioned elementAn element with position other than static
z-indexWhich overlapping element is in front
Stacking contextA group of layers that z-index works inside
overflowWhat happens to content bigger than its box

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.