In the example below, also available here, both WebKit (Safari 5, Chrome 10.0.648.205) and Mozilla (FF 4) keep DIV div around the visible width of the browser window, while the TABLE is as wide as its content.
I would expect the DIV to grow as wide as the TABLE, but since the behavior is consistent across browsers, I suspect this is a feature rather than a bug.
Interestingly, if the DIV is set to have float:left, it does grow as wide as the table.
Any explanations?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Wide Table</title>
<style>
#container {
background-color:blue;
/* float:left;*/
}
</style>
</head>
<body>
<div id="container">
<table >
<tr id="tr">
<td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td><td>Table Cell</td>
</tr>
</table>
</div>
</body>
</html>
Block elements take the width of their parent by default, not the width of their content; height on the other hand, works the other way around: block elements take the height of their content by default rather than the height of their parent. Things work this way because HTML/CSS is built around the usual top-to-bottom layout of English text.
So, your
#containertakes the width of<body>and then the<table>inside#containeroverflows outside#container. If you putoverflow: hiddenon#containeryou’ll clip the<table>,overflow-x: auto;on#containerwill add a scrollbar.UPDATE: As far as floating elements are concerned, the CSS3 spec has this to say:
The default width will be auto so shrink-to-fit it is:
There are no possible line breaks in your
<table>so the floated#containerwill take on the entire width of its<table>child.