views:

194

answers:

2

The following table element in the "center" div causes the contents in the "left" divs to be offset by several pixels from the top (8 in my browser). Adding some text prior to the table removes this offset.

Why? How do I stop this from happening without requiring a "dummy" line of text before my table?

<html>
<head>
    <style type="text/css">
        #left {
            display: table-cell;
            background-color: blue;
        }
        #menu {
            background-color: green;
        }
        #center {
            background-color: red;
            display: table-cell;
        }
    </style>
<body>
    <div id="left">
        <div id="menu">
            Menu 1<br>
            Menu 2<br>
        </div>
    </div>
    <div id="center">
        <table><tr><td>This is the main contents.</tr></td></table>
    </div>
    <div id="left">
        <div id="menu">
            Menu 1<br>
            Menu 2<br>
        </div>
    </div>
</body>
</html>

Update0

Note that with floats, I am unable to get a centered column expanding to its content. The source from which I extracted this example uses display: table; margin-left: auto; margin-right: auto; to center everything in the body.

A: 

At a guess, I'd say you have a margin-top set on your <table>. Have a look in Firebug.

table {
margin-top: 0;
}

Any particular reason you're laying out like this? You can float divs and do advanced layouts without setting display: table-cell. Besides, tables should only be used for tabular data.

adam
No, I've already poked around with firebug. It's a strange layout issue, and margins have nothing (that I'm aware of) to do with it. I've already tried just as you've suggested.
Matt Joiner
See my update regarding why I don't use floats.
Matt Joiner
+1  A: 

I was able to fix it by adding vertical-align:top; to the '#left' style.

You should wrap the display: table-cell divs in another div with display:table-row

Ray
Ha!! Great, thanks. Thus ends about 3 hours extracting this little bit of evil by using Firebug.
Matt Joiner