Pratham Profile picture
Apr 4, 2021 19 tweets 8 min read Read on X
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

Sep 23
5 GitHub repositories will make your life 90% easier as a developer:
1. StackQL

StackQL allows you to query multiple cloud and SaaS providers (Google, AWS, Azure, Okta, GitHub, etc.) using SQL.

github.com/stackql/stackql
Image
2. Open SaaS

Starter kit for your next SaaS.

Tech stack used:

• React
• Node.js
• Tailwind
• Stripe
• Plausible

And much more...

github.com/wasp-lang/open…
Image
Read 6 tweets
Apr 30
CORS is the biggest headache for developers.

In the next 3 minutes, you will be able to tackle CORS errors much more effectively.
CORS is not rocket science.

It's the biggest pain for developers because the majority of us don't know its core concept.

Let's try to build a solid fundamental.

Stands for Cross-Origin Resource Sharing.
It is a security feature implemented by web browsers (almost all) that controls how web pages from one domain can request resources hosted on another domain.
Read 9 tweets
Apr 7
APIs are the backbone of development.

Let me show you how to build your first basic REST API in four simple steps.
Step 1: Install Node.js

I'm assuming you have Node.js installed on your machine. If you haven't installed it, click on the following link and install it simply.

nodejs.org/en
Step 2: Initialize the project

Let's start; create an empty directory and initialize your project by running the following command. Image
Read 17 tweets
Apr 4
5 GitHub repositories will make your life 90% easier:
1. StackQL

StackQL allows you to query multiple cloud and SaaS providers (Google, AWS, Azure, Okta, GitHub, etc.) using SQL.

🔗 github.com/stackql/stackql
Image
2. 50 Projects 50 Days

50+ mini web projects using HTML, CSS & JavaScript.

🔗 github.com/bradtraversy/5…
Image
Read 6 tweets
Feb 2
Convert your Python application into a website in 2 minutes.

Let me show you how:
There's no easy way than this to build a data, AI-based web applications.

I came across Taipy.

It is an open-source Python library for building production-ready front-end & back-end in no time.
Let's see in five simple steps how to build a movie recommendation system using Taipy.
Read 11 tweets
Jan 23
6 free Books to learn web development:
1. The Magic of CSS

The content in this eBook is for all-level developers. Learn about layout, box-model, positioning etc. with sample code snippets.

adamschwartz.co/magic-of-css/
Image
2. DOM Enlightenment

"DOM Enlightenment" is a free online book that explores DOM in-depth, providing a clear and detailed understanding of how it works.

domenlightenment.com
Image
Read 7 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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(