Everything you need to know about CSS position property

Thread🧵👇
There are 5 values that you can pass in position property

- static
- relative
- absolute
- fixed
- sticky

In this thread we will be look at all of them

{ 2 / 19 }
Let's start with understanding what document flow is?

📌 Elements are displayed on the screen as they written in the HTML document

Consider the following piece of code:

H1, P, H3 and div are displayed on the screen in exact order as they written in the HTML file

{ 3 / 19 }
Alright let's start with position concept. The first value we have is "static"

HTML elements are positioned static by default. An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page:

{ 4 / 19 }
Moving forward, next we have is

📌 Relative Position

- Relative positioning do not take an element out of document flow
- Relative positioning is relative to element's original position which can be changed using offset

{ 5 / 19 }
🔹 Relative position is relative to itself.

For example: Consider the code and output in the attached image below

As you can see red box is shifted 100px from left because I applied left offset after giving it relative positioning

{ 6 / 19 }
In the attached image below, the black dotted area would be the original position of red box if I don't apply position relative in it.

As you can see it proved that relative position is relative to itself

{ 7 / 19 }
So now let me shift the blue box 100px towards left. So how can I do that? it's simple

.blue {
position: relative;
right: 100px;
}

Notice here that document flow is as it is. So the relative position does not affect the document flow

{ 8 / 19 }
📌 Absolute Position

- The element is removed from the normal document flow
- You can consider it as, after applying absolute position the element will no longer in the flow and no space is created for the element in the page layout

{ 9 / 19 }
For example:

If I apply absolute position in the red box, then the red box will be out of the flow and hence no space will be allocated to it.

See the image below, red box is out of flow and hence yellow box is at top and followed by green and blue

* Yellow box is below red
- The absolute position of an element is relative to its closest ancestor, which has some position property.

Consider the code below, Red is the parent div and black is the child div. In this particular case, body is the parent of red div

{ 11 / 19 }
Now let me apply relative position to red(parent) div and absolute position to black(child) div.

As I mentioned absolute position is relative to closest ancestor having some position property

{ 12 / 19 }
Let's understand it in little more details👇

Consider this piece of code.

Here green div is a parent of red and red div is a parent of black

{ 13 / 19 }
So let me apply position property in green and black. In black div we have absolute position so in that case black div will be relative to green not red

Because here black's closest ancestor is green which has some position property

{ 14 / 19 }
Next we have is position: fixed;

Fixed position elements is always relative to the viewport. Which means it always stays in the same place even if the page is scrolled

{ 15 / 19 }
For example, consider this red element. It will always fixed at same place even if the page is scrolled.

Fixed element also break the document flow.

codepen.io/prathamkumar/p…

{ 16 / 19 }
Great! Moving even further, Next we have is

📌 Position: sticky

Sticky position is the mixture of relative and fixed position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed)

{ 17 / 19 }
The sticky state does not break the document flow.

Play around with the output here for better understanding

codepen.io/prathamkumar/p…

{ 18 / 19 }
I think that's pretty much it. Did I forget to add something? Feel free to add below.

Thanks for reading ❤️

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Pratham 👨‍💻🚀

Pratham 👨‍💻🚀 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Prathkum

6 Apr
16 amazing HTML attributes that are so powerful and maybe you haven't heard of them yet

🧵👇🏻
1️⃣ Type

The "type" attribute of <ol> element let you change the type of order i.e, numeric, alphabetic or roman numbers Image
2️⃣ Target

"Open link in new tab" by just adding 'target' attribute as "_blank" Image
Read 19 tweets
5 Apr
Complete resources pack to make a website from scratch

Thread🧵👇
Editors ✍️

➸ codeinterview .io
➸ codesandbov .io
➸ codepen .io
➸ godbolt .org
➸ wandbox .org
➸ carbon .now.sh
➸ pastebin .com
➸ jsfiddle .net
➸ ideone .com
➸ jsbin .com
➸ ide .judge0.com
Color picker 🎨

🔹colorhunt .co
🔸webgradients .com
🔹gradienta .io
🔸0to255 .com
🔹colors .lol
🔸colorbrewer2 .org
🔹colormind .io
🔸flatuicolors .com
🔹happyhues .co
🔸color-hex .com
🔹farbvelo .elastiq.ch
Read 10 tweets
5 Apr
Are you planning to learn Front-end development in 2021?

Here is a quick start guide 👇🏻

Thread 🧵 Image
You can start learning web development with backend as well but starting with Front-end is more beneficial.

Well it depends on person to person. Let's move forward 🔽

{ 2 / 28 }
First things first, you need an editor to write code. There are plethora of editors out there like atom, VS code, sublime etc

I recommend you to start with VS code. Why?

Many built-in features
It's fast
Large community
IntelliSense code completion and debugging

{ 3 / 28 }
Read 28 tweets
4 Apr
Seven amazing tools / websites a developer should visit

A Thread 🧵👇🏻
1️⃣ linuxsurvival.com

Linux Survival is a free tutorial designed to make it as easy as possible to learn Linux. Even though Linux has hundreds of commands, there are only about a dozen you need to know to perform most basic tasks. Image
2️⃣ learn-anything.xyz

Search anything here and it will show you the correct roadmap Image
Read 8 tweets
3 Apr
Create 7 complicated figures using CSS ♾️➡️⭐🔍❤️

🧵👇🏻
1️⃣ Infinity Symbol

It may look tough but trust me I created this symbol using border-radius only .infinity {   position: relative; }     .infinity::before {
2️⃣ Arrow

Combination of rectangle and a triangle .arrow {   height: 40px;   width: 100px;   background: black
Read 9 tweets
3 Apr
2️⃣8️⃣ github.com/streamich/reac…

A great collection of pre built custom hooks that you can use in your project straight away. Hooks are divided into categories.
2️⃣9️⃣ github.com/Developer-Y/cs…

List of Computer Science courses with video lectures.
3️⃣0️⃣ github.com/leonardomso/33…

33 concepts every JavaScript developer should know.
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!