Results 1 to 4 of 4

Thread: setting width of div to remainder of parent's space

  1. #1
    Join Date
    Jun 2008
    Posts
    192
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default setting width of div to remainder of parent's space

    Hello,

    How can I tell a div, in CSS, to take up the rest of the space that's left over from another div to its left. I mean, I have one div floating on the left and it's size is determine by the jpg image that's filling it up. Then, the div to its right is floating left and its size is also determined by its contents (text in this case). I would like to set its width, in CSS, to be whatever's left in its parent div (also the parent of the div to the left) after the first div took up space on the left side.

    I'm trying to force it to stick with the remaining width, and to force it to grow in height if it needs more room to display its contents. The problem now is that if its contents are too large, the div wraps around to the next line (i.e. it appears under the first div instead of to its right). I'm hoping that setting its width to be fixed at the remainder of its parent's available space, it won't do this and will grow its height down instead if it needs extra space for its contents.

    How can I do this?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Possibly make the parent -

    Code:
    overflow: hidden;
    But I can't be sure. Put up a demo.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Nov 2009
    Posts
    107
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Default

    Perhaps setting a max-width on both elements?

  4. #4
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    46
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    well you could change the width of the div dynamically, so something along the lines of:
    Code:
    <html> 
      <head>     
    <script type="text/javascript">
    var imageWidth = document.getElementById('image').style.width;
    var containerWidth = document.getElementById('container').style.width;
    var divWidth = document.getElementById('fillwidth').style.width;
    divWidth = containerWidth - imageWidth;
    </script>
      </head> 
     
    <body>
    <div id="container"> 
     <div id="fillwidth"></div> 
    <img src="fishcakes/image/fishcakeagain.png" id="image"></img>
    </div>
    </body> 
    </html>
    or something along those lines
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

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
  •