SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple, Stupid CSS Question

    Hi. Is there any reason why I should NOT put something like the following at or near the beginning of my style sheets?

    Code:
    *
    {
      position: relative;
    }
    Obviously, I may absolute-position some elements that come after in the style sheet, but I often find myself having to go back and add position: relative to various elements and thought I could save myself the trouble by doing the above. Might it cause any problems? Any downside?

    Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2010
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The question is why would you want to?

  3. #3
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,731
    Mentioned
    31 Post(s)
    Tagged
    0 Thread(s)
    aside , from clairs' comment. YES there is. I try to refrain from positioning until I need it either to move something relatively, add a z-index or need to give absolute position to one of its descendants.

    That last one is the main concern for what you are talking about. if you give absolute position to everything, any time you use AP, the element will be positioned off of its parent.

    Suppose you had this code you want to position .dropMenu as a vertical menu, below another vertical menu...

    <ul id="main_menu">
    <li> Item</li>
    <li> Item</li>
    <li> Item</li>
    <li> <ul class="sub">Item
    <li> Item</li>
    <li> Item</li>
    <li> Item</li>
    </ul></li>
    </ul>

    .sub position and size would be calculated based on the PARENT LI rather than THE UL, which is what would make crating this effect easiest. Sure, you could say : #mainMenu > LI {position: static;} to fix this.. but isn't that the same amount of work and causing more browser compatibility issues than just simply giving #mainMenu {position relative}?


    you could, reserve a line on your code for relative elements

    #mainMenu, .shellDiv, .someOther, .another{ position:relative;} if you think that would make your code cleaner and /or more organized.

  4. #4
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, as I said, I often find myself having to go back and set some elements to position: relative. Reasons include being able to set a z-index, set elements within those elements to position: absolute, etc.

    If there's no harm in setting everything to position: relative, it would make things easier down the road, that's all.

  5. #5
    SitePoint Addict Phil-man's Avatar
    Join Date
    Nov 2000
    Posts
    291
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, dresden, my response was to clairs before your comment came through. :-)

  6. #6
    The CSS Clinic is open silver trophybronze trophy
    Paul O'B's Avatar
    Join Date
    Jan 2003
    Location
    Hampshire UK
    Posts
    39,789
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Using the universal selector is nearly always a bad idea unless you are targeting a specific section via a parent id or similar.

    Applying a property to all elements is bad for performance and usually just bad full stop as in some cases will kill the normal inheritance rules. The only things you can really globally set are margins and padding and even then you cause problems with form elements.

    If you apply position:relative to all elements then you can never escape from them in IE7 an under.
    Internet Explorer for Windows versions up to and including 7 always use the nearest positioned ancestor to determine the stacking context for the element in question.
    In IE6 position:relative also triggers a different rendering algorithm and if you apply too many position:relatives it can go berserk. Not to mention older safari which will hide all elements on the page containing position:relative if you have applied position:relative to the html element.

    As usual, if you do things properly and only apply when needed then there is no problem.

    Usually you know if a child is to be absolutely positioned so you would create the stacking context when you needed it right at the start. If you change the design at a later date then adding position:relative to the parent isn't such a hard task is it?

    I'm all for short-cuts but applying a property to all elements unnecessarily just seems like a bad practice and something that will come back to bite you more times than it helps you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •