Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why not setting default position to relative?

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 absolutely 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

like image 472
bobD Avatar asked Jun 29 '16 22:06

bobD


People also ask

Is it bad to use position relative in CSS?

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.

Should I use position absolute or relative?

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.

When should we use relative positions?

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.

Is position relative the default position for all elements?

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 .


1 Answers

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:

  1. It's original location will still be occupied by the element (like there is a ghost of the original element still there taking up space).
  2. If the element has child elements that are absolutely positioned, they are now absolutely positioned within the context of this element.

Advantages

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.

Disadvantages

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.

like image 171
Pranjal Avatar answered Oct 17 '22 19:10

Pranjal