CSS Position Property

The position property decides how an element is placed. Together with top, right, bottom and left, you can move it from its normal place.

ValueMeaning
staticDefault. Normal flow. top, left and others have no effect
relativeMoved from its normal place. Its old space stays reserved
absoluteRemoved from the flow, placed against the nearest positioned parent
fixedPlaced against the browser window, stays while scrolling
stickyNormal until you scroll to it, then it sticks

relative

.box { position: relative; top: 10px; left: 20px; }

absolute inside a relative parent

An absolute element is placed against its nearest ancestor that has a position other than static. So make the parent position: relative.

.parent { position: relative; height: 100px; background: #eee; }
.child  { position: absolute; top: 10px; right: 10px; background: tomato; color: #fff; padding: 6px; }
Output in the browser
absolute: top 10, right 10
Parent with position: relative


fixed

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

A fixed element stays in the same place of the screen when the visitor scrolls. It is used for chat buttons and top bars.


sticky

.table-header { position: sticky; top: 0; background: #fff; }
Note: To place a box over another box, use z-index. For page layouts prefer Flexbox or Grid. Position is best for small things, like badges and overlays.

Summary

  • relative shifts an element, absolute places it in a positioned parent.
  • fixed sticks to the window and sticky sticks after a scroll point.
  • Next: CSS z-index.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Write CSS to centre a box horizontally using margin auto
Write CSS to create a horizontal navigation menu
Interview Questions & Answers
What is the use of float property in CSS?
What is the difference between display none and visibility hidden?
What is the difference between inline, block and inline-block?
What are the different values of the position property in CSS?
What is z-index in CSS?
What is clearfix in CSS?
How to hide an element in CSS?