Learn Position in CSS

The position property decides how an element is placed. static (the default) follows the normal flow: elements one after another, from top to bottom. relative…

Normally, the elements on a page are placed one after another, from top to bottom, like people standing in a queue. That arrangement is called the normal flow. The position property changes how an element is placed: it is moved a little from its place, taken out of the normal flow, or fixed to the screen.

Nudge a box with relative

This example has one box and the CSS rule that moves it:

<div class="box"></div>

<style>
.box {
  position: relative;
  top: 20px;
  left: 40px;
}
</style>

To remember it, imagine you push the box from the side you name. top pushes from above, so the box goes down. left pushes from the left, so the box goes right. So in the exercise, left: 75px moves the box to the right, not to the left. To move a box left, use right. To move it up, use bottom.

The box's own spot is kept for it, so no other element moves. In the exercise, write each line like top: 50px;: one colon, and a number with the unit px.

The five position values

An absolute example: a label in the top right corner of a card. The parent, the card, gets .card { position: relative; }. The label gets .label { position: absolute; top: 0; right: 0; }, so the label touches the top and right sides of the card.

⚠️ Boxes that won't move, or drag their neighbours along

TL;DR: The position property decides how an element is placed. static (the default) follows the normal flow: elements one after another, from top to bottom. relative moves the element from its own spot: top pushes it down, left pushes it right. absolute takes the element out of the normal flow. Its position is measured from the nearest parent whose position is not static, or from the page if there is no such parent. fixed is measured from the screen, so the element doesn't move when the page scrolls. sticky follows the normal flow first, then sticks to the screen when the page scrolls.

⚡ Nudge it from its own spot

Write:

.box {
  position: relative;
  top: 20px;
  left: 40px;
}
Guess first: does left: 40px move the box left or right?

Right. left pushes the box from its left side, so the box moves away from the left. To move it left, use right.

Meaning: position: relative moves the box from its own spot: top: 20px pushes it down 20px, and left: 40px pushes it right 40px. That spot is kept for it, so no other element moves. Without position, top and left do nothing.

⚡ absolute is measured from its parent

Write:

.card { position: relative; }
.label { position: absolute; top: 0; right: 0; }

Meaning: The label is inside the card, so the card is its parent. absolute takes the label out of the normal flow (elements one after another, from top to bottom). The label's position is then measured from the nearest parent whose position is not static (the default). Here that parent is the card, so top: 0 and right: 0 put the label in the top right corner of the card. If there is no such parent, the position is measured from the page. That is why the parent usually gets position: relative.

⚡ Pinned to the screen

Meaning: The position of a fixed element is measured from the screen, so it stays in the same place while the page scrolls. A sticky element moves up as the page scrolls, then sticks when its distance from the top of the screen is equal to its top value.

What you will learn