CSS Positioning

1. Static Positioning (Default)

By default, all elements have position: static. This means they follow the normal flow of the document without any special positioning. Static positioning doesn’t allow you to move elements from their default positions on the page. It simply follows the natural flow of the content.

CSS




Tutorials dojo strip

2. Relative Positioning

When you use position: relative, the element stays in the normal document flow, but you can move it relative to its original position using the top, bottom, left, or right properties.

CSS




3. Absolute Positioning

With position: absolute, the element is removed from the document flow and positioned relative to its nearest positioned ancestor (an element with relative, absolute, or fixed positioning).

CSS




4. Fixed Positioning

An element with position: fixed is positioned relative to the viewport (browser window), meaning it stays in the same place even when you scroll.

CSS




5. Z-Index and Stacking Order

The z-index property controls the stacking order of elements, allowing you to determine which element should appear on top when elements overlap. The higher the z-index value, the higher the element’s position in the stack.

CSS




CSS Positioning Example Code

Explanation of Code:

  • Static: Follows normal document flow.
  • Relative: Moves relative to its original position but keeps its space in the document.
  • Absolute: Positioned based on its nearest positioned ancestor.
  • Fixed: Stays in place even while scrolling.
  • Z-Index: Controls which element appears on top when they overlap.
CSS




CSS Labs

Tutorials dojo strip