Skip to content

CSS Positioning

Overview

CSS positioning allows you to precisely control the placement of elements on the page.

Position Values

Static Positioning

css
.static-element {
    position: static;  /* Default positioning */
    /* Element follows normal document flow */
}

Relative Positioning

css
.relative-element {
    position: relative;
    top: 20px;
    left: 30px;
    /* Positioned relative to its normal position */
}

Absolute Positioning

css
.absolute-element {
    position: absolute;
    top: 50px;
    left: 100px;
    /* Positioned relative to nearest positioned ancestor */
}

Fixed Positioning

css
.fixed-element {
    position: fixed;
    top: 0;
    right: 0;
    /* Positioned relative to viewport */
    z-index: 1000;
}

Sticky Positioning

css
.sticky-element {
    position: sticky;
    top: 10px;
    /* Sticks to viewport when scrolling */
}

Z-index and Stacking Context

css
.higher-element {
    position: relative;
    z-index: 10;
}

.lower-element {
    position: relative;
    z-index: 5;
}

Content is for learning and research only.