tags:

views:

33

answers:

2

Given this example (http://jsfiddle.net/A8gHg/, code also below), try making your window smaller such that the size of the example is squished.

<div style="background-color:blue">
    <table style="width:100%">
        <tr>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
        </tr>
    </table>
</div>

You will note that the textboxes (correctly) do not wrap to new lines as they are in individual <td>s.

However, the containing div, as denoted by the blue colour, does not completely wrap the table.

How can I make the containing div fully contain the child table?

Thanks.

+2  A: 

Edit:

Add display:table to the wrapping div.

<div style="background-color:blue;padding:5px;display:table;">
    <table style="margin:5px;">
        <tr>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
            <td>
                <input type="text" style="width:150px"/>
            </td>
        </tr>
    </table>
</div>

http://jsfiddle.net/A8gHg/11/

Michael Robinson
OK, but this only works if you fix the widths of the table elements in advance :).
Daniel Cassidy
Yeah ... updated
Michael Robinson
It’s always struck me as hacky to add `display: table` to things that aren’t tables, but +1 because this works.
Daniel Cassidy
I agree, but without knowing the width of the content or dynamically changing width with JS ...
Michael Robinson
It’s also worth noting that this works when the `div` contains any arbitrary oversized content (for example `img`), not just a `table`.
Daniel Cassidy
ooo good to know. If it wasn't for this question forcing me to to do 5 seconds of research I still wouldn't know about `display:table`. Thanks SO!
Michael Robinson
Thanks, I tried this initially but it doesn't work in IE7 (which is my lowest common denominator browser) but I didn't specify it should so you get the accept.
Graphain
IE7! Lucky b*stard! Mine is IE6 :( Thanks. To get it working in IE7 you could try some JS that detects the total width of the table and sets the relevant width to the wrapping div, not ideal but...
Michael Robinson
A: 

Firefox 3.6.8 and Internet Explorer 8 both render the blue div behind the entire table. What am I missing?

edit: Oh, really small screen resolution needed.

anonymous