Relative and absolute positioning in CSS


absolute

CSS positioning of elements is really sometimes confusing but is very easy to understand. Lets start with static position.
Static position is default for any element on a Web page. If position of element is not defined , it will be static and would display inside the normal flow of the page.

If you try to apply rules like top , bottom , left or right to static element , will be ignored.

Absolute Positioning

This is the easiest property to learn and understand. When absolute position is applied to an element it is taken out from the normal flow of document and is positioned to defined top ,left, bottom or right of the body.

Consider the following HTML code:

1
2
3
4
5
6
<div class="container">
	<div class="normal">This is normal text block </div>
	<div class="normal">This is normal text block </div>
	<div class="normal">This is normal text block </div>
	<div class="demo-position">This is an absolute block </div>
</div>

and its CSS as:

1
2
3
.container{margin:120px;}
.normal, .demo-position{padding:10px;border:1px solid #000;background:#ccc}
.demo-position{position:absolute;top:0;left:0;background:#eee;}

Here is the output:

absolute

Relative positioning
Relative positioning uses the same four positioning properties as absolute positioning. But instead , it starts from where the normal flow of the element.

Most important thing is to note that if absolute element is inside relative element, it consider relative element as its parent element and will positioned accordingly.

Lets make little addition in above CSS. We will simply add “position:relative” to div container

1
2
3
.container{margin:120px;position:relative}
.normal, .demo-position{padding:10px;border:1px solid #000;background:#ccc}
.demo-position{position:absolute;top:0;left:0;background:#eee;}

and find out the change as :
relative

You will find absolute positioned div is now inside div container which was positioned relative.

isn’t it was easy?

That’s it!!
Cheers!

Recently Published

»

The Most Common Issues Faced By UX Designers

Google’s mobile-friendly updates compel companies to adopt ...

»

7 Advantages Of Choosing WordPress For E-Commerce Websites

E-commerce has certainly grown in the past few years and today, it ...

»

Top 6 Beautiful Food Oriented WordPress Themes

In recent times, Food Blogging has become a favourite pastime for ...

»

Reasons to Choose Custom Web Design Over Website Template

When going to design a website, you will usually find yourself with ...

»

5 Important Techniques To Make Your E-commerce Store SEO-Friendly

Ecommerce owners are engaged in turning their stores into dynamic one ...

»

Get Content Marketing Into Your System

The days of marketers hoisting up a brand campaign once every few ...

»

Combine HTML5 & Bootstrap As Key Ingredients For Rebooting Your Business

HTML5 has achieved the tremendous heights in the recent years owing ...

»

10 Tips On Social Media That Will Land Your Dream Job

A job search trends survey conducted by Jobvite showed that 48% of ...

»

10 Best Node.js Frameworks For Web And Apps Development

Node.js is counted among one of the most renowned JavaScript runtime ...