Tricks behind CSS Position Property values Absolute and Fixed

The time we are designing a HTML page for more complex layout we do refer position using CSS. Basically this is used to position the HTML elements. There are 6 possible values for position property in CSS. By default position is static. According to our requirements we can apply absolute, relative, fixed, initial & inherit values to the position property in CSS.

Cascading Style Sheets (CSS) is a fundamental technology used to control the layout and presentation of web pages. Among its many properties, the `position` property is one of the most powerful yet often misunderstood. It determines how an element is positioned within a document, influencing its relationship with other elements and the viewport. The three most commonly used values—`absolute`, `relative`, and `fixed`—each serve distinct purposes in web design. Understanding these values is crucial for creating flexible and responsive layouts.

Position: static;

This is the default value for position property. By declaring position static it will render the HTML elements in order to the document flow.

Position: absolute;

Position absolute is the most trickiest positioning value. Let us take example to understand position absolute. In below example I am using position absolute for a div inside the div which is position relative.

The ‘position: absolute’ value removes an element from the normal document flow, allowing it to be placed anywhere within its closest positioned ancestor (an element with a `position` value other than ‘static’).

Key Characteristics of ‘position: absolute’:

1. Breaks Document Flow – The element no longer affects the position of other elements, which may overlap or collapse into the space it once occupied.

2. Uses Nearest Positioned Parent – If a parent element has `relative`, `absolute`, or `fixed` positioning, the child element is positioned relative to that parent. Otherwise, it uses the document body.

<!DOCTYPE html>
<html>
<head>
<title>Using Position Absolute</title>
<style type="text/css">
.posRelative { position: relative; top: 100px; left: 100px; }
.posAbsolute { position: absolute; top: 100px; left: 100px; }
</style>
</head>
<body>
<div class="posRelative">
<div class="posAbsolute">
This is the div where we applied position absolute.
</div>
</div>
</body>
</html>

This will give us first div in 100px left & 100px from top position. The second div is applied with position absolute. In real-time the second div will appear 100px left & top from the parent div.

If i am declaring the parent div position static then child div with position absolute will show in 100px top & left from the top left corner of the browser window. Try with the example below.

<!DOCTYPE html>
<html>
<head>
<title>Using Position Absolute</title>
<style type="text/css">
.posStatic { position: static; top: 100px; left: 100px; }
.posAbsolute { position: absolute; top: 100px; left: 100px; }
</style>
</head>
<body>
<div class="posStatic">
<div class="posAbsolute">
This is the div where we applied position absolute.
</div>
</div>
</body>
</html>

Position: fixed;

Using position fixed we can set position of our HTML element from the top left corner of browser window.

The ‘position: fixed’ value anchors an element to the viewport, meaning it remains in the same position even when the page is scrolled. This is commonly used for navigation bars, headers, or sticky elements.

Key Characteristics of ‘position: fixed’:

1. Viewport-Relative – The element is positioned relative to the browser window, not any parent container.

2. Stays in Place During Scrolling – Unlike ‘absolute’ positioning, a fixed element does not move when the user scrolls.

3. Ignores Parent Elements – Even if a parent has ‘relative’ or ‘absolute’ positioning, the fixed element remains tied to the viewport.

Position: relative;

Using position relative the position is relative to its normal position. If I am declaring top:100px it will take the element to 100px down.

The ‘position: relative’ value positions an element relative to its normal position in the document flow. Unlike ‘static’ positioning, which does not allow adjustments via properties like ‘top’, ‘right’, ‘bottom’, or ‘left’, ‘relative’ positioning enables fine-tuning an element’s placement.

Key Characteristics of ‘position: relative’:

1. Maintains Document Flow – The element remains in the normal flow, and surrounding elements behave as if it were in its original position.

2. Offset Adjustments – Using ‘top’, ‘bottom’, ‘left’, or ‘right’ shifts the element from its default position.

3. Reference Point – The element’s new position is calculated based on where it would have been in the normal flow.

Position: initial;

Initial reset the position of an element to default value.

Position: inherit;

Using position inherit we can inherits this property from parent element. You can access CSS position property using JavaScript. The syntax we need to use is object.style.position=”fixed”.

Common Mistakes

1. Overusing Absolute Positioning – Excessive use can lead to layout instability, especially on responsive designs.

2. Forgetting a Positioned Parent – An `absolute` element without a positioned ancestor may appear in unexpected locations.

3. Ignoring Z-Index – Positioned elements can overlap; using `z-index` ensures proper layering.

Conclusion

Understanding the CSS ‘position’ property is essential for mastering web layout design. By carefully selecting the appropriate positioning method and avoiding common pitfalls, designers can build more flexible and maintainable web pages. Whether aligning elements within a container or keeping a navigation bar visible at all times, mastering these techniques is a cornerstone of effective CSS development.