I recall reading somewhere that relative positioning should be preferred to absolute because it scales better at varying screen resolutions and because it's easier to modify/maintain. So I've been using relative positioning for 95% of my elements. However, I am becoming frustrated with this approach because whenever the positioning of one element changes, it affects the positioning of all the following elements. This is becoming a problem because having to correct the positioning for these relative elements is exceedingly time-consuming, and my boss has mentioned that it takes me too long to create CSS layouts. I'm thinking about going with absolute so that I don't run into this problem. Is this a bad idea? I'd appreciate some feedback. Thanks.

  • It's hard to say what the best practice would be for your case without seeing how you've implemented your relative positioning. Do you have any examples of a change you have made that required even more changes as a result? If your code has been set up modularly, you shouldn't have to be making all these adjustments. David Clarke about 6 years ago

1 answer

This was chosen as the best answer

I've never heard that you should eschew absolute in favor of relative, but there's definitely a time and place for both. Most of your base elements should probably be relatively-positioned for a number of reasons - the biggest being that yes, they can move with different screen sizes. Within those elements, absolute is not only fine, it's sometimes preferable. If you have things that need to stay in the same spot within their parent element, you should use absolute. Especially when you need them to be pixel-perfect. If it's relative, it should be able to be flexible as an element. It sounds like you've got things that you don't want flexing that much. I tend only to use relative for things like my base containers - nav, header, content, etc.

Worse comes to worst, absolute positioning leaves extra space in wider monitors, or overflows in smaller ones. I think sometimes relative can cause things to wrap and sometimes cause more problems.

Answered about 6 years ago by Ben Saufley
  • That's a big help, and it makes a great deal of sense. Thanks! Chris Barnhill about 6 years ago