CSS positioning

position property

  • static
  • relative
  • absolute
  • fixed
  • sticky

position: static

  • default position

position: relative

position: relative;
top: 20px;
left: 20px;
  • relative to the original position
  • the original space is retained

position: absolute

  • relative to the element with other than static position above
  • taken out from the document flow

position: fixed

  • relative to the viewport
  • not affected by scrolling
  • used for floating menu

position: sticky

z-index

  • defines priority
  • works only on positioned elements
  • stacking context
.above {
    z-index: 50;
}

.below {
    z-index: 2;
}

Playground

lorem ipsum

CSS positioning

By Martin Nuc

CSS positioning

  • 321