How can a div surround a table ?

Discussion in 'CSS' started by jreeman, Nov 8, 2009.

  1. #1
    Hello,

    here is an example of a container (a div) containing a table.


    <html> 
    <head><title>Tab-div</title></head> 
        <body> 
            <div style="border:1px solid red;padding:10px;"> 
                <table style="border:3px solid blue;"> 
                    <tr> 
                        <td style="border:1px solid green;padding:2px;">111111111111111111111111</td> 
                        <td style="border:1px solid green;padding:2px;">222222222222222222222222</td> 
                        <td style="border:1px solid green;padding:2px;">333333333333333333333333</td> 
                        <td style="border:1px solid green;padding:2px;">444444444444444444444444</td> 
                    </tr> 
                </table> 
            </div> 
        </body> 
    <html> 
    Code (markup):
    In that case, the div doesn't care about its content and if the window of the browser in smaller than the table content, the div cuts the table on the right side.

    I put in attachement the screenshot that show the div cutting the table on the right.

    I found a solution that solves the problem : if I set the css property display of the div to "display:table" or "display:inline-block" the div doesn't cut the table anymore, but I find that is more an hack than a clean solution (the good solution should be "display:table-block" but it doesn't exist).

    What are you thinking about this problem ?

    PS : all of this involve firefox, I didn't yet test the behavior on ie6.
     

    Attached Files:

    jreeman, Nov 8, 2009 IP
  2. khajeya

    khajeya Member

    Messages:
    256
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Please try to use display:table-cell
    so, the coding will be like below:

    <html> 
    <head><title>Tab-div</title></head> 
        <body> 
            <div style="border:1px solid red;padding:10px; display: table-cell"> 
                <table style="border:3px solid blue;"> 
                    <tr> 
                        <td style="border:1px solid green;padding:2px;">111111111111111111111111</td> 
                        <td style="border:1px solid green;padding:2px;">222222222222222222222222</td> 
                        <td style="border:1px solid green;padding:2px;">333333333333333333333333</td> 
                        <td style="border:1px solid green;padding:2px;">444444444444444444444444</td> 
                    </tr> 
                </table> 
            </div> 
        </body> 
    <html>
    Code (markup):
     
    khajeya, Nov 18, 2009 IP
  3. nehrav

    nehrav Peon

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can use this

    <div style="width:500px;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    </div>
    HTML:
     
    nehrav, Nov 18, 2009 IP
  4. jreeman

    jreeman Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The last solution wasn't good because I don't want to set the table and div width, I'll try with table-cell.
     
    jreeman, Nov 20, 2009 IP
  5. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That and divs can't have tr's just randomly floating around inside them. If there are tr's and td's then there must be a <table>

    Display: table-cell may not work cross-browser as a few of them only want to do table-cell on elements who are inside someone who is set to display: table-row who is inside someone set to display: table (Safari at least used to be like this and FF didn't like display: table skipping to display: table-cell either).

    Obviously I don't know enough about your situation because the easiest solution is to set a width (or min-width even) of the div to the smallest the table can ever become. We're assuming your table has a minimum width due to its contents and that it can grow wider if the browser allows it.
    Setting a min-width will force the browser to set scrollbars when the browser window gets too small to show all the content. Currently your div is shrinking with the browser window like any static block would do, while the table, being unable to shrink any further, overflows out (like it should).

    That or, remove the div entirely. What's it doing anyway?
    If the div can die a silent death, no reason the table itself can't have a width or min-width.

    IE6 nor 7 will not deal with display: table or related table-* properties at all... IE8 will.
    IE6 will not deal with min-width, but it does have an expanding-box bug where content can force a greater width of a container instead of overflowing like it should.
     
    Stomme poes, Nov 20, 2009 IP
  6. jreeman

    jreeman Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    the div contains different elements and all the elements must be in this box that has a border (surrounding box).

    I don't understand when you say I have to set the width of the div to the smallest the table can ever become. It works when the table width will be the smallest, but for higher size the div will not grow with the table.

    i confirm I have to find a solution only for firefox because fir IE, it seems there is a bug, as you said, that finally give what I want.

    There is another solution that is to set float:left for the div. I never knew it but when a div is float:left|right its size is the sum of the size of all the elements it contains.
     
    jreeman, Nov 21, 2009 IP
  7. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Floating the div will make it

    -contain all/any floated children
    -shrink-wrap to the width of its contents

    The shrinkage isn't assured in IE6 (depends on what the children are doing), but it will work in all the other browsers.
     
    Stomme poes, Nov 24, 2009 IP