CSS Positions
Written By: Avinash Malhotra
Updated on
CSS Position Property
The CSS position property specifies how an element is positioned in the document. By default, elements are positioned statically (except the dialog element). Static elements follow normal document flow. Other position values let you move elements relative to their normal position or place them at a specific location on the page (for example, using relative, absolute, fixed or sticky).
Type of CSS positions
Position Properties
These 6 position properties are applicable to non static positions only. Except position static, any position can use them.
| Property | Units | Role |
|---|---|---|
| Left | px, %, em, rem | To move element from left |
| Right | px, %, em, rem | To move element from right |
| Top | px, %, em, rem | To move element from top |
| bottom | px, %, em, rem | To move element from bottom |
| inset | px, %, em, rem | shortcut for top, right, bottom and left. |
| z-index | number | To change z axis of element |
Click to Change position
CSS Positions example
This is an example to demonstrate css positions. Click buttons on top to change css position of gray div. Scroll in division to check css positions in action,
Static Position
Static position is the default position of all HTML elements ( except dialog tag ). Static means position according to the HTML Order or position taken by an element by its own.
In static positioning the top, bottom, left, right and z-index properties have no effect.
Element with static position.
Div with static position
<style>
.box{
width:200px;
height:200px;
background:#333;
position:static;
}
</style>
<div class="box">
<p> Div with Static Position</p>
</div>
CSS Position Properties top, left, bottom, right, inset and z-index doesn't work with static position.
Position Relative
Relative Position means position of an HTML element with respect to its actual position. Relative position can overlap over another elements. Relative elements moves, but their space remains.
By default, relative elements are above static elements on z axis. But when two or more elements are relative, the last relative element in stack will come to the top(z axis).
We can use top, left, bottom, right and z-index after giving position relative to an HTML element.
Position Relative Example
Div with static position
Div with relative position, left 200px and bottom 50px
<style>
.box1{
width:200px;
height:200px;
background:#333;
padding:10px;
}
.box2{
width:200px;
height:200px;
background:#999;
padding:10px;
position:relative;
left:100px;
bottom:50px;
}
</style>
<div class="box1">Div with position static.</div>
<div class="box2">Div with relative position, left 200px and bottom 50px</div>
Position Absolute
Absolute positioning removes the element from the normal document flow and positions it independently. An absolutely positioned element can be placed anywhere using the top, right, bottom, and left properties. It can overlap other elements — use z-index to control stacking order.
Offsets for an absolutely positioned element are calculated relative to the nearest positioned (non-static) ancestor, or transformed element. If no such ancestor exists, offsets are relative to the initial containing block (typically the viewport).
Properties of Position Absolute
- Leave space in normal document flow.
- Occupy width of content.
- Can move relative to its nearest non static parent.
- width 100% and height 100% of absolute element is actually 100% of its non static parent element.
- If all parent elements are static, absolute element moves relative to viewport window.
We can use top, left, bottom, right and z-index after giving position absolute to an HTML element.
Position Absolute Example
Position static
Position static.
<style>
.box1{
width:200px;
height:200px;
background:#333;
}
.box2{
height:50px;
background:#999;
position:static;
}
</style>
<div class="box1">Position static</div>
<div class="box2">Position absolute</div>
Position Fixed
Fixed Position means position of an HTML element with respect to viewport. It doesn't move even if we scroll window up or down.
For Exp: the Top Menu of this web-page is positioned fixed to the top. It doesn't move even after scrolling down or up.
We can use top, left, bottom, right and z-index after giving position fixed to an HTML element.
Click on the box below to make it fixed
Div with position fixed, left 200px; and top 300px from window;
Sticky
Position: sticky (introduced in modern CSS) behaves like relative until the element reaches a threshold within the viewport, at which point it behaves like fixed and 'sticks' in place. That's why it's called "sticky."
It's necessary to specify at least one offset (top, right, bottom or left) for a sticky element to take effect; otherwise it will not stick.
Sticky Example
Heading 1
- List Item
- List Item
- List Item
- List Item
- List Item
- List Item
- List Item
- List Item
Heading 2
- List Item
- List Item
- List Item
- List Item
- List Item
- List Item
Heading 3
- List Item
- List Item
- List Item
- List Item
- List Item
- List Item
<style>
.box{ margin: 1rem 0 }
.box h3{ position: sticky; top:0; background:#fff }
.box p{ margin-top: 1rem}
</style>
<div class="box">
<h3>Heading </h3>
<ol>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
</ol>
</div>
<div class="box">
<h3>Heading </h3>
<ol>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
<li>List Item</li>
</ol>
</div>
CSS z-index
z-index controls the stacking order of positioned (non-static) elements. It accepts integer values—elements with higher z-index appear above elements with lower values.
Note: z-index only applies to positioned elements (for example, those with position: relative, absolute, fixed or sticky). It has no effect on statically positioned elements.
Example: div elements using relative positioning and z-index.
<style>
.box1{
width:200px;
height:200px;
background:#333;
position:relative;
z-index:1;
}
.box2{
width:200px;
height:200px;
background:#999;
position:relative;
z-index:2;
}
.box3{
width:200px;
height:200px;
background:#ccc;
position:relative;
z-index:3;
}
</style>
<div class="box1">Div with position relative and z-index 1.</div>
<div class="box2">Div with position relative and z-index 2;</div>
<div class="box2">Div with position relative and z-index 3;</div>
z-index example
Div with z-index 1
Div 1
Div with z-index 2
Div 2
Div with z-index 3
Div 3
As z-index of last div ( div-3) is greater than other two, it is on the top.