Positioning in CSS allows you to display your element wherever you want on the screen

But when I was learning it, I found it little bit confusing😅

So in this thread I'll try to explain it in easiest manner with practical implementation. Let's start

THREAD🧵👇

There are 5 values that you can pass in position property

- static
- relative
- absolute
- fixed
- sticky

In this thread we will be focusing on relative and absolute positioning as both are widely used
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.
As now you know about document flow, let's start with Relative positioning

📌 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
🔹 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
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
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
📌 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
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
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
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
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
I think that's pretty much it for this thread. I hope you get a overview of CSS positioning

This may sound a bit confusing but try to play with code. You'll be able to build better understanding😄

Feel free to post your doubts below❤️

More from Pratham 👨‍💻🚀

Use these 5 websites to learn web development for free in 2023 (take a look):

1. Codedamn

All-in-one platform for learning web development:

• Hands-on practice and projects bundled
• Video content along with interactive labs, quizzes, and articles
• Every course comes with a certificate
• code brushes AI in playgrounds

🔗
https://t.co/mXmUtJZnOU


2. Codepip

Codepip is the platform for your favorite web development games. Gain an edge in your following interview or project.

🔗 https://t.co/MjHNsLvkiL


3. RapidAPI Comics

Learn API development with RapidAPI Comics (sketch notes and fun analogies).

🔗 https://t.co/KONorFomgu


4. JS Visualizer 9000

Learn JavaScript by visualizing your code.

🔗 https://t.co/9NId4GjGAq
Five free VS Code extensions that will change the way you do web development:

1. RapidAPI Client

Fulfill all your need for API development.

• Test and consume APIs
• Work with your VS Code theme
• Generate TypeScript, Swift, and Python interfaces from responses.

🔗
https://t.co/FR0QhyTi8v


2. Better Comments

Create more human-friendly comments in your code.

• Alerts
• Queries
• TODOs
• Highlights

🔗 https://t.co/AV2BwAMAlL


3. Git Graph

View a Git Graph of your repository and efficiently perform Git actions from the graph.

🔗 https://t.co/S5wagmTgsa


4. Peacock

Do you have a habit of keeping multiple VS Code windows open?

Peacock lets you change the color of your VS code even if multiple instances are open.

🔗 https://t.co/bmidPkTQOK

More from Tech

You May Also Like

हिमालय पर्वत की एक बड़ी पवित्र गुफा थी।उस गुफा के निकट ही गंगा जी बहती थी।एक बार देवर्षि नारद विचरण करते हुए वहां आ पहुंचे।वह परम पवित्र गुफा नारद जी को अत्यंत सुहावनी लगी।वहां का मनोरम प्राकृतिक दृश्य,पर्वत,नदी और वन देख उनके हृदय में श्रीहरि विष्णु की भक्ति अत्यंत बलवती हो उठी।


और देवर्षि नारद वहीं बैठकर तपस्या में लीन हो गए।इन्द्र नारद की तपस्या से घबरा गए।उन्हें हमेशा की तरह अपना सिंहासन व स्वर्ग खोने का डर सताने लगा।इसलिए इन्द्र ने नारद की तपस्या भंग करने के लिए कामदेव को उनके पास भेज दिया।वहां पहुंच कामदेव ने अपनी माया से वसंतऋतु को उत्पन्न कर दिया।


पेड़ और पौधों पर रंग बिरंगे फूल खिल गए और कोयलें कूकने लगी,पक्षी चहकने लगे।शीतल,मंद,सुगंधित और सुहावनी हवा चलने लगी।रंभा आदि अप्सराएं नाचने लगीं ।किन्तु कामदेव की किसी भी माया का नारद पे कोई प्रभाव नहीं पड़ा।तब कामदेव को डर सताने लगा कि कहीं नारद क्रोध में आकर मुझे श्राप न देदें।

जैसे ही नारद ने अपनी आंखें खोली, उसी क्षण कामदेव ने उनसे क्षमा मांगी।नारद मुनि को तनिक भी क्रोध नहीं आया और उन्होने शीघ्र ही कामदेव को क्षमा कर दिया।कामदेव प्रसन्न होकर वहां से चले गए।कामदेव के चले जाने पर देवर्षि के मन में अहंकार आ गया कि मैने कामदेव को हरा दिया।

नारद फिर कैलाश जा पहुंचे और शिवजी को अपनी विजयगाथा सुनाई।शिव समझ गए कि नारद अहंकारी हो गए हैं और अगर ये बात विष्णु जी जान गए तो नारद के लिए अच्छा नहीं होगा।ये सोचकर शिवजी ने नारद को भगवन विष्णु को ये बात बताने के लीए मना किया। परंतु नारद जी को ये बात उचित नहीं लगी।
Great article from @AsheSchow. I lived thru the 'Satanic Panic' of the 1980's/early 1990's asking myself "Has eveyrbody lost their GODDAMN MINDS?!"


The 3 big things that made the 1980's/early 1990's surreal for me.

1) Satanic Panic - satanism in the day cares ahhhh!

2) "Repressed memory" syndrome

3) Facilitated Communication [FC]

All 3 led to massive abuse.

"Therapists" -and I use the term to describe these quacks loosely - would hypnotize people & convince they they were 'reliving' past memories of Mom & Dad killing babies in Satanic rituals in the basement while they were growing up.

Other 'therapists' would badger kids until they invented stories about watching alligators eat babies dropped into a lake from a hot air balloon. Kids would deny anything happened for hours until the therapist 'broke through' and 'found' the 'truth'.

FC was a movement that started with the claim severely handicapped individuals were able to 'type' legible sentences & communicate if a 'helper' guided their hands over a keyboard.