Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is * {position:relative} a bad idea?

Tags:

css

Is this going to cause me untold grief if I stick it at the top of my stylesheet?

* {position:relative}
like image 352
David Meister Avatar asked Feb 23 '10 12:02

David Meister


People also ask

Should I use position relative?

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.

Should I use position relative or absolute?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.

Is it bad practice to use position absolute?

As long as you structure your HTML so that the elements follow a logical order that makes sense when rendered without CSS, there is no reason why using absolute positioning should be considered bad practice.

What does a relative position mean?

Relative positions are words that describe where objects are in an environment. For example: top, behind, or next to.


2 Answers

Is this going to cause me untold grief if I stick it at the top of my stylesheet?

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.

I could imagine there are even more side-effects field of z-index settings.

Not a good idea IMO.

And no, position: static is not deprecated, after all, it is the default setting :)

like image 68
Pekka Avatar answered Oct 10 '22 16:10

Pekka


Wildcards can cause performance issues when not used carefully. That would probably not be the case in your example, but it's a bad habit to develop.

But more importantly, it's rare that you can conclusively say you want any behavior to apply to all elements.

With relative positioning, you will at best achieve nothing and at worst create many headaches for yourself trying to troubleshoot things that would normally "just work".

Relative positioning definitely has its uses. Apply it when you need it.

like image 41
Tim M. Avatar answered Oct 10 '22 17:10

Tim M.