Table of contents
1.
Introduction
2.
What is Relative Positioning?
2.1.
Syntax
2.2.
Example
3.
What is Absolute Positioning?
3.1.
Syntax
3.2.
Example
4.
Differences Between Relative and Absolute Positioning
5.
Frequently Asked Questions
5.1.
When should I use relative positioning in CSS?
5.2.
What happens if an absolutely positioned element has no positioned ancestor?
5.3.
Can relative and absolute positioning be used together?
6.
Conclusion
Last Updated: Feb 16, 2025
Easy

Absolute vs Relative Position in CSS

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The absolute and relative position in CSS are two key positioning methods used to control the placement of elements on a web page. The relative position moves an element based on its normal position, while the absolute position places an element relative to its nearest positioned ancestor or the document itself.

Absolute vs Relative Position in CSS

In this article, you will learn the syntax, differences, and practical usage of absolute and relative positioning in CSS.

What is Relative Positioning?

Relative positioning in CSS allows an element to be positioned relative to its original position in the document flow. This means that even if the element is moved using topleftright, or bottom, it still occupies its original space in the document.

Syntax

.element {
    position: relative;
    top: 20px;
    left: 30px;
}

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Relative Positioning</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: lightblue;
            position: relative;
            top: 20px;
            left: 30px;
        }
    </style>
</head>
<body>
    <div class="box">Box</div>
</body>
</html>

 

Output

Output

The blue box moves 20px down and 30px right from its original position, but the space it initially occupied remains reserved.

What is Absolute Positioning?

Absolute positioning in CSS removes the element from the normal document flow and positions it relative to its nearest positioned ancestor. If no positioned ancestor is found, it is placed relative to the <html> element (the whole page).

Syntax

.element {
    position: absolute;
    top: 50px;
    left: 100px;
}

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Absolute Positioning</title>
    <style>
        .container {
            position: relative;
            width: 300px;
            height: 200px;
            border: 2px solid black;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: lightcoral;
            position: absolute;
            top: 50px;
            left: 100px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box">Box</div>
    </div>
</body>
</html>

 

Output

Output

The red box is placed 50px down and 100px right, but unlike relative positioning, it does not occupy its original space.

Differences Between Relative and Absolute Positioning

FeatureRelative PositioningAbsolute Positioning
Affects Document FlowYes, retains spaceNo, removed from flow
Positioned Relative ToIts original positionIts nearest positioned ancestor (or <html> if none)
Requires Parent PositioningNoYes, to prevent default positioning to <html>
Space ReservationYesNo
Best Used ForSmall adjustmentsFloating elements over others

Frequently Asked Questions

When should I use relative positioning in CSS?

Use relative positioning when you need to adjust an element’s position slightly without affecting other elements around it.

What happens if an absolutely positioned element has no positioned ancestor?

If an absolute element has no positioned ancestor, it is placed relative to the <html> element.

Can relative and absolute positioning be used together?

Yes! You can set a relative position for a parent element and use absolute positioning for a child element inside it.

Conclusion

In this article, we learned about absolute and relative positions in CSS, their differences, and when to use them. Absolute positioning places elements at fixed locations, while relative positioning moves elements based on their original position. Understanding these concepts helps in designing flexible and well-structured web pages. Using them correctly improves layout control and enhances user experience.

Live masterclass