Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why CSS absolute doesn't work with static parent?

Tags:

html

css

W3Schools says that

element with position: absolute is positioned relative to the nearest positioned ancestor. Where positioned element is one whose position is anything except static.

My question is why static elements are excluded? I know that I can set position: relative;left:0px;top:0px;right:0px;bottom:0px which is the same as position:static but IMO it looks like a workaround.

like image 967
Paweł Adamski Avatar asked Dec 15 '15 12:12

Paweł Adamski


2 Answers

My question is why static elements are excluded?

Because otherwise absolute wouldn't be able to position with respect to anything other than the element's immediate parent.

I know that I can set position: relative;left:0px;top:0px;right:0px;bottom:0px

You don't need to explicitly set the distances. Just the position property will do.

like image 74
Quentin Avatar answered Oct 27 '22 21:10

Quentin


Just position: relative will do.

This is a good thing, because otherwise the browser will have to apply calculations on every element instead of being able to take shorter routes for static positioning.

It also means you can have arbitrary containers, so long as they're static, and still have an element be positionable relative to the container of your choice.

like image 21
Niet the Dark Absol Avatar answered Oct 27 '22 21:10

Niet the Dark Absol