css

CSS display:table-row does not expand when width is set to 100%


I'm having a bit of a problem. I'm using FireFox 3.6 and have the following DOM structure:

<div class="view-row">
    <div class="view-type">Type</div>
    <div class="view-name">Name</div>                
</div>

And the following CSS:

.view-row {
width:100%;
display:table-row;
}

.view-name {
display: table-cell;
float:right;
}

.view-type {
display: table-cell;
}

If I take off the display:table-row it will appear correctly, with the view-row showing a width of 100%. If I put it back it shrinks down. I've put this up on JS Bin:

http://jsbin.com/ifiyo

What's going on?


Solution

  • If you're using display:table-row etc., then you need proper markup, which includes a containing table. Without it your original question basically provides the equivalent bad markup of:

    <tr style="width:100%">
        <td>Type</td>
        <td style="float:right">Name</td>
    </tr>
    

    Where's the table in the above? You can't just have a row out of nowhere (tr must be contained in either table, thead, tbody, etc.)

    Instead, add an outer element with display:table, put the 100% width on the containing element. The two inside cells will automatically go 50/50 and align the text right on the second cell. Forget floats with table elements. It'll cause so many headaches.

    markup:

    <div class="view-table">
        <div class="view-row">
            <div class="view-type">Type</div>
            <div class="view-name">Name</div>                
        </div>
    </div>
    

    CSS:

    .view-table
    {
        display:table;
        width:100%;
    }
    .view-row,
    {
        display:table-row;
    }
    .view-row > div
    {
        display: table-cell;
    }
    .view-name 
    {
        text-align:right;
    }