InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS properties

  • CSS3 flexbox
  • all
  • animation
  • background-color
  • background-image
  • border
  • border-color
  • border-radius
  • border-style
  • border-width
  • bottom
  • box-shadow
  • box-sizing
  • calc function
  • clip
  • color
  • color value currentColor
  • color values
  • cursor
  • float
  • font
  • font-family
  • font-size
  • font-style
  • font-variant
  • font-weight
  • generated content
  • left
  • letter-spacing
  • line-height
  • list-style
  • list-style-image
  • list-style-position
  • list-style-type
  • margin
  • max-height
  • max-width
  • min-height
  • min-width
  • opacity
  • outline
  • outline-color
  • outline-style
  • outline-width
  • overflow
  • padding
  • pointer-events
  • position
  • resize
  • right
  • text-align
  • text-decoration
  • text-indent
  • text-overflow
  • text-shadow
  • text-transform
  • top
  • transform
  • transition
  • transition-delay
  • transition-duration
  • transition-property
  • transition-timing-function
  • visibility
  • white-space
  • word-break
  • word-spacing
  • word-wrap/overflow-wrap
  • z-index
 
  • Home
  • > Tutorials
  • > CSS
  • > CSS properties

Css position property – static, relative, absolute and fixed positioning

on Feb 9, 2016

CSS position property decides how an element will be places in document. It can have 4 values static (default), relative, absolute and fixed.

css-position-static-relative-absolute-fixed

CSS property position

CSS version: CSS 2.1
Value: static | relative | absolute | fixed | inherit
Initial: static
Applies to: all elements
Inherited: no

Understanding position values

position valuetop,leftdocument placement and impact on other items
staticNA (not used)Part of document normal flow
relativeRelative to its normal positionPart of document flow but shifted. May leave some space empty and overlap with other elements if shifting is non-zero. Due to its shifting, other elements are not shifted. They pretend as if this element is not shifted.
absoluteRelative to first relative/absolute positioned ancestorRemoved from document normal flow and placed at its position. Will overlap with some elements
fixedRelative to viewportRemoved from document normal flow and placed fixed in viewport. Will overap with some elements

Default top,left values

If top and/or left values are not specified then the default values are taken as if the element is static.

Comments on bottom,right properties

We can also use bottom and/or right properties. That that case offset applies from bottom or right. Also note that bottom:10px will mean that elements bottom will be 10 less that reference bottom and similarly for property right.

Example – position static

<style type="text/css" media="screen">
.outer {
  background:lightgreen; width:100px;
}
.inner1 {
  background:lightgray; width:100px;
}
</style>
<div class="outer">
outer1
<div class="inner1">
inner1 inner1 inner1 inner1 inner1 inner1
</div>
<div class="inner2">
inner2 inner2 inner2 inner2 inner2 inner2
</div>
</div>
refresh done
try it online

Note that top, left etc are not applicable here. Try setting top and left for inner1 and you will see no difference.

Example – position relative

<style type="text/css" media="screen">
.outer {
  background:lightgreen; width:100px;
}
.inner1 {
  background:lightgray; width:100px;
  position: relative;
  top: 10px;
  left: 10px;
}
</style>
<div class="outer">
outer1
<div class="inner1">
inner1 inner1 inner1 inner1 inner1 inner1
</div>
<div class="inner2">
inner2 inner2 inner2 inner2 inner2 inner2
</div>
</div>
refresh done
try it online

Few points to note:

  1. inner1 div is shifted. But no impact on inner2 position in document normal flow.
  2. inner1 shifting cause some vacant space and some overlapping.
  3. If set specify top,left 0 for innner1 div, it would just act like static.

Example – position absolute

<style type="text/css" media="screen">
.outer {
  background:lightgreen; width:100px;
  position: relative;
}
.inner1 {
  background:lightgray; width:100px;
  position: absolute;
  top: 30px;
  left: 30px;
}
</style>
<div class="outer">
outer1
<div class="inner1">
inner1 inner1 inner1 inner1 inner1 inner1
</div>
<div class="inner2">
inner2 inner2 inner2 inner2 inner2 inner2
</div>
</div>
refresh done
try it online

Few points to note:

  1. inner1 being position:absolute, will be positioned relative to first ancestor with position relative or absolute. That ancestor is outer div here.
  2. In there is no ancestor with position relative or absolute, absolute elements are placed relative to body.
  3. The inner1 div is not part of document normal flow and hence it is removed from normal flow. you can see next element (inner2) has been placed as if inner1 does not exists.

Example – position fixed

<style type="text/css" media="screen">
.outer {
 height:1000px;
  background:lightgreen; width:100px;
  position: relative;
}
.inner1 {
  background:lightgray; width:100px;
  position: fixed;
  top: 50px;
  left: 100px;
}
</style>
<div class="outer">
outer1
<div class="inner1">
inner1 inner1 inner1 inner1 inner1 inner1
</div>
<div class="inner2">
inner2 inner2 inner2 inner2 inner2 inner2
</div>
</div>
refresh done
try it online

Few points to note:

  1. Even when we scroll, fixed element remains stationary. It position is relative to viewport.
  2. We can also use bottom,right values to place it.
  3. The fixed element is not part of document normal flow and hence it is removed from normal flow. you can see next element (inner2) has been placed as if inner1 does not exists.

Specification

  • W3C CSS 2.1 position specification

Suggested posts:

  1. Edit and debug css in Chrome
  2. CSS left
  3. CSS top
  4. CSS right
  5. CSS – place a div in bottom right corner of browser
  6. CSS list-style-type – specify marker (disc, square, numeric, etc.) for a list
  7. Javascript parse json string
  8. CSS pointer-events – disable click on an element
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, CSS layout, CSS properties, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress