DEV Community

Cover image for CSS 101 - Position static vs relative vs absolute
Eric The Coder
Eric The Coder

Posted on

CSS 101 - Position static vs relative vs absolute

One of my 2021 resolution is to strength my knowledge of CSS. I postpone the training since almost one year. So this time no excuse, everyday, I will publish here on Dev.to what I learn from my CSS course the day before.

Click follow if you want to miss nothing.

CSS Position

The position property specifies the type of positioning method used for an element (static, relative, fixed, absolute or sticky).

The position CSS property sets how an element is positioned in a document. The top, right, bottom, and left properties determine the final location of positioned elements.

position: static;
The element is positioned according to the normal flow of the document. The top, right, bottom, left, and z-index properties have no effect. This is the default value.

position: relative;
The element is positioned according to the normal flow of the document, and then offset relative to itself based on the values of top, right, bottom, and left. The offset does not affect the position of any other elements; thus, the space given for the element in the page layout is the same as if position were static.

position: absolute;
The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, and left.

position: fixed;
Similar to position: absolute but relative stay to viewport and not the page like absolute.

Let create a example. Create those elements:

<body>
    <div>BOX 1</div>
    <h1>position: static;</h1>
    <div>BOX 2</div>
</body>
Enter fullscreen mode Exit fullscreen mode
div {
    color: lighgray;
    font-size: 20px;
    background-color: darkgray;
    border: solid gray;
    padding: 10px;
    height: 40px;
    margin: 10px;
  }


h1 {
  background-color: red;
  margin: 10px;
  padding: 10px;
  font-size: 20px;
  border: solid;
  position: static;
  left: 20px;
  top: 10px;
}
Enter fullscreen mode Exit fullscreen mode

Static (left and top are ignore):
static

Relative

div {
    color: lighgray;
    font-size: 20px;
    background-color: darkgray;
    border: solid gray;
    padding: 10px;
    height: 40px;
    margin: 10px;
  }


h1 {
  background-color: red;
  margin: 10px;
  padding: 10px;
  font-size: 20px;
  border: solid;
  position: relative;
  left: 20px;
  top: 10px;
}
Enter fullscreen mode Exit fullscreen mode

Relative (element position left 20px and top 10 px from original position)
relative
Absolute

div {
    color: lighgray;
    font-size: 20px;
    background-color: darkgray;
    border: solid gray;
    padding: 10px;
    height: 40px;
    margin: 10px;
  }


h1 {
  background-color: red;
  margin: 10px;
  padding: 10px;
  font-size: 20px;
  border: solid;
  position: absolute;
  left: 20px;
  top: 10px;
}
Enter fullscreen mode Exit fullscreen mode

Absolute (element position left 20px and top 10 px from page)
absolute

Conclusion

That's it for today. Tomorrow the journey continue, see you later! If you want to be sure to miss nothing click follow me here or on twitter!

Follow me on Twitter: Follow @justericchapman

Top comments (2)

Collapse
 
sznroe profile image
Sujan Rai

Where is the follow-up post, sir?

Collapse
 
ericchapman profile image
Eric The Coder

Will be out in couples of hours... Stay tune!