Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

CSS - Stretch DIV to fit absolutely positioned objects

Options
  • 24-10-2011 3:55pm
    #1
    Registered Users Posts: 1,657 ✭✭✭


    Is it possible to stretch a DIV to fit its contents, when its contents are divs with position:absolute?
    <div id='container' style='position:relative; border:1px solid #000000'>
     <div id='content1' style='position:absolute; top:150px; left:560px;'>
      blah blah blah
     </div>
     <div id='content2' style='position:absolute; top:900px; left:240px;'>
      yadda yadda yadda
     </div>
     <img src='mypic.jpg' style='float:left' />
     <div style='clear:both;'></div>
    </div>
    

    So I want container DIV to be a box around all the items contained therein. But it seems only to stretch to be big enough to contain the image.


Comments

  • Registered Users Posts: 8,070 ✭✭✭Placebo


    you need to define a width and height for your container div,
    your position of the content is relative to that of its parent, so not sure why you have 900px; that must mean your parent div must be massive,

    anyway see rough example here:
    http://jsfiddle.net/TMgvy/


  • Registered Users Posts: 1,657 ✭✭✭komodosp


    you need to define a width and height for your container div
    A but that's my problem - I don't want to define a width and height for my container div. A user will define the positions of the contents but I don't want them to have to worry about them doing the container too.

    If they were relatively positioned, the container would just stretch to fit around them - I want to do the same thing only they are absolutely positioned.


  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    Is there a specific reason you need to use absolute positioning?

    Stuff like style='position:absolute; top:900px; left:240px;'
    Is breaking the flow of the document completely, and is pretty much abuse of css. Absolute position elements are not part of the flow, so don't affect objects which are.


  • Registered Users Posts: 3,140 ✭✭✭ocallagh


    If you reach a dead end with CSS3 and simple positioning you have usually done something wrong along the way! Ideally you should re-design the entire layout but if that's a non runner:

    If you know the exact height of content2, you could stick a margin-top: (900px + height of content2) on the image.

    Or use javascript.


Advertisement