Relative and absolute positioning in CSS


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:

<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>

and its CSS as:

.normal, .demo-position{padding:10px;border:1px solid #000;background:#ccc}

Here is the output:


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

.normal, .demo-position{padding:10px;border:1px solid #000;background:#ccc}

and find out the change as :

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

isn't it was easy?

That's it!!

Recently Published


4 flawless WordPress Plugins for Employee Management

Keeping your employee management in check is an important part of ...


Kubernetes: Why Today’s Programmers Need to Understand

Launched by three Google engineers, Kubernetes is a container ...


Do I Need Endpoint detection and response tools ?

Endpoint detection and response (EDR) tools are designed to add an ...


How to Build a Killer Marketing Strategy for your WordPress Blog?

Running a blog over WordPress is seemingly an easy thing to do. The ...


5 Signs You Need a New Hosting Company

Web hosting has become cheaper and easier to attain than ever before. ...


5 Things to Consider When Designing A Logo For Your Startup

Designing a Logo is essential because it communicates your business. ...


What Is Access Control?

Access Control, When running a business, there are dozens of things ...


Process Management Solution For a Business

In order to be successful, a company needs to be on the same page and ...


5 Metrics You Should Track on Every Blog Post

As a blogger, you may have developed your blog with a variety of blog ...