CSS Positioning Elements (original) (raw)

Last Updated : 19 Jan, 2026

CSS positioning is used to control the placement of elements on a web page. It allows elements to be positioned relative to the normal document flow, the browser window, or other elements.

Given below the different types of Positioning:

1. Static Positioning

Static is the default position of an element. It does not accept properties like top, left, right, or bottom.

HTML `

<style>
    div {
        border: 1px solid black;
        padding: 10px;
        margin: 10px;
    }
</style>
Box 1
Box 2

`

2. Relative Positioning

Relative positioning places an element relative to its normal position. You can move it using top, left, right, or bottom.

HTML `

Link CSS Example
Box 1
Box 2

CSS

div { border: 1px solid black; padding: 10px; margin: 10px; } .relative { position: relative; top: 20px; left: 30px; }

`

Box 2 is shifted 20px down and 30px to the right from its normal position.

3. Absolute Positioning

Absolute positioning removes the element from the document flow and places it relative to the nearest ancestor with a positioning context (relative, absolute, or fixed).

HTML `

<style>
    .container {
        position: relative;
        width: 300px;
        height: 200px;
        border: 2px solid black;
        margin: 20px auto;
    }
    .absolute {
        position: absolute;
        top: 50px;
        left: 50px;
        background-color: pink;
        padding: 10px;
    }
</style>

Container with Relative Positioning

Absolutely Positioned Element

`

4. Fixed Positioning

Fixed positioning removes the element from the flow and positions it relative to the viewport. It remains in place even when the page scrolls.

HTML `

<style>
    .fixed {
        position: fixed;
        top: 10px;
        right: 10px;
        background-color: lightgreen;
        padding: 20px;
        border: 2px solid black;
    }
    .content {
        height: 1200px;
        padding: 10px;
    }
</style>

Fixed Positioning Example

Fixed Box

Scroll down to see that the fixed box stays in place.

This content simulates a long page.

`

The fixed element stays at the top-right corner of the viewport even as the user scrolls.

5. Sticky Positioning

Sticky positioning switches between relative and fixed based on the scroll position.

HTML `

<style>
    .sticky {
        position: sticky;
        top: 0;
        background-color: yellow;
        padding: 10px;
    }
</style>
I am sticky

Scroll down to see the effect.

`

The sticky element stays at the top of the viewport as you scroll but only within its containing element.