I recently realized how useless the position: static
Is , It doesn't offer you anything unless the relative
also does.
In fact my question Is "why not just adding this line to our CSS file ?"
* { position:relative; }
so we are always able to easily position any element absolute
ly from It's parent or moving the element It self around just by left: ...
or the same for any direction.
so why elements are not positioned to relative
by default?
thanks
Yes. You will not be able to work with absolutely positioned elements any more, for example - the absolute positioning will always be relative to the parent element, which is almost never the desired result.
If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. Position Absolute: When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.
I mostly use position :relative in the element when I know the inner element of that element is going to be positioned absolutely. For example If I have two divs and outside div is a static block and elements inside the outer div is going to be positioned absolute relative to the outer div.
By default, the position property for all HTML elements in CSS is set to static . This means that if you don't specify any other position value or if the position property is not declared explicitly, it'll be static .
Let's take some element in its default static state and make it position: relative;. Just by changing that value alone, nothing happens. It appears in the exact same place that it did before the change. But it does change two things:
If all elements started out with relative positioning, all two of the items above you'd have naturally. Your top/right/bottom/left values work as you would assume they do right out of the box. Z-index "just works" as well. The positioning context is always constrained to the next parent element, which makes logical sense.
Most notably, always constraining the positioning context to the parent element is limiting. Let's say you want to position an element in the upper right of the page regardless of where the element appears in the markup. That's going to be tough if the element is buried down the hierarchy. You'll need to reset that positioning context for every parent element. That means setting them back to position: static;, and losing all the benefits of relative positioning along the way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With