Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - CSS Position


CSS Position

The position property controls how an element is placed on a web page. It lets you move, layer, and fix elements anywhere on the screen.


Position Values

The most common position values are:

  • static - default positioning (normal page flow)
  • relative - positioned relative to its normal place
  • absolute - positioned relative to the nearest positioned ancestor
  • fixed - positioned relative to the viewport (does not move when scrolling)
  • sticky - toggles between relative and fixed depending on scroll position

position: static

Static is the default position for all elements. They appear in the normal document flow.

Example

Static positioning:

div.static {
  position: static;
  border: 2px solid gray;
}
Try it Yourself »

position: relative

A relatively positioned element can be moved from its normal position using top, right, bottom, and left.

Example

Relative positioning:

div.relative {
  position: relative;
  left: 30px;
  border: 2px solid green;
}
Try it Yourself »

This moves the element 30px to the right, but the space it originally occupied remains.


position: absolute

An absolutely positioned element is removed from the normal flow and positioned relative to its first positioned ancestor (not static).

Example

Absolute inside a relative parent:

<div style="position: relative;">
  <div style="position: absolute; top: 50px; left: 50px;">
    Absolute Box
  </div>
</div>
Try it Yourself »

If there's no positioned ancestor, the element is placed relative to the page (the <html> element).


position: fixed

Fixed elements stay in the same place even when the page is scrolled. They are positioned relative to the viewport.

Example

Fixed position (like a sticky header):

div.fixed {
  position: fixed;
  top: 0;
  width: 100%;
  background-color: navy;
  color: white;
  padding: 10px;
}
Try it Yourself »

This is often used for navigation bars that stay at the top of the screen.


position: sticky

Sticky elements behave like relative until a specified scroll position is reached, then they "stick" like fixed.

Example

Sticky header:

h2 {
  position: sticky;
  top: 0;
  background-color: lightgray;
  padding: 5px;
}
Try it Yourself »

Note: You must specify at least one of the top, right, bottom or left properties, for sticky positioning to work.

Note: Sticky positioning works only if the parent has enough height and overflow is visible.


Z-Index (Stack Order)

The z-index property controls the stacking order of positioned elements. Elements with higher z-index appear in front of others.

Example

Stacking elements:

div.box1 { position: absolute; left: 40px; top: 40px; z-index: 1; }
div.box2 { position: absolute; left: 60px; top: 60px; z-index: 2; }
Try it Yourself »

Best Practices

  • Use relative for small offsets or containing absolutely positioned children.
  • Use absolute for tooltips, popups, and positioned elements inside containers.
  • Use fixed for sticky navigation or floating buttons.
  • Use sticky for headers that stay visible during scroll.
  • Control stacking with z-index.

Next, you'll learn about CSS Float and Clear - how to align elements side by side before we move on to Flexbox and Grid layouts.

Next » CSS Float and Clear


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.