HTML CSS SQL Meta tag Generator ScrollBar design Generator Encode Decode images

CSS Basics

Introduction Syntax Selectors Attribute Selectors Specificity Combinators !important Comments Colors

CSS Need to Know

Box Model Pseudo-class Pseudo-element Inline-block Math Functions Max and Min Opacity Outline Overflow Z-index Float

Most used

Align Backgrounds Borders Text Units Padding Margins Position Tables Display Fonts Lists Height and Width

CSS Reference

CSS Properties reference CSS Pseudo-Classes reference CSS Pseudo-Elements reference CSS Selector reference

CSS position

The CSS position property specifies how an element is positioned in a document. There are five different position values in CSS: static, relative, absolute, fixed, and sticky.

Static Position

The default positioning method is static. An element with a static position is positioned according to the normal flow of the document. This means that it will appear in the order it appears in the HTML code.

Result:

    .static-example {
      position: static;
    }
  
This is a static element.

Relative Position

An element with a relative position is positioned relative to its normal position. This means that you can move it from its original position without affecting the layout of other elements.

Result:

    .relative-example {
      position: relative;
      top: 20px;
      left: 30px;
    }
  
This is a relative element.

Absolute Position

An element with an absolute position is positioned relative to the nearest positioned ancestor. If there is no positioned ancestor, it is positioned relative to the initial containing block, which is usually the <body> element.

Result:

    .absolute-example {
      position: absolute;
      top: 50px;
      right: 0;
    }
  
This is an absolute element.

Fixed Position

An element with a fixed position is positioned relative to the viewport, which means it always stays in the same place even when the page is scrolled.

Result:

    .fixed-example {
      position: fixed;
      bottom: 0;
      right: 0;
    }
  
This is a fixed element.

Sticky Position

An element with a sticky position is positioned based on the user's scroll position. It behaves like relative positioning until a specified scroll point is reached, then it "sticks" in place.

Result:

    .sticky-example {
      position: sticky;
      top: 0;
    }
  
This is a sticky element.
Scroll down to see the sticky effect.