1

I have two table cells in one row. First one has small div, second one has larger one. Second div stretches the whole row. How do I make the first div stretch to the whole cell as well?

This is what I get:

what I get

This is what I want:

what I want

HTML:

<table>
    <tr class="tr">
        <th class="th">
            <div class="div green">This div is short</div>
        </th>
        <th class="th">
            <div class="div blue">This div has more content so it pushes 
            the height of the whole row. However, the shorter div doesn't 
            stretch to the whole row. How do I fix this?</div>
        </th>
    </tr>
</table>

CSS:

.tr {
    width:100%;
    border:1px solid red;
    height:auto;
}
.th {
    display:table-cell;
    width:50%;
}
.div {
    /* What should be here? */
}
.green {
    background:green
}
.blue {
    background:blue
}

jsfiddle here

Why I need this: the table stores lots of tabular data. Table headers contain widgets for filtering out table contents, and these widgets have variable height.

Georgii Oleinikov
  • 3,865
  • 3
  • 27
  • 27

1 Answers1

1

The easiest way is to add height: 100%; to .inner and change height: auto; from .tr to height: 100%;:

.container{
    width:100%;
    border:1px solid red;
    height: 100%;
}
.outer{
    display:table-cell;
    width:50%;
}
.inner{
    height: 100%;
}
.green{
    background:green
}
.blue{
    background:blue
}

This, however, will mean your text is not centered vertically in the cell anymore. Even though that's what you have shown, it's not the default/proper behavior for table-cell content. This is because there is a div nested inside.

You can remove your child divs and simply slide their classes up to the <th> elements, and it will center the text, work more semantically, and use less code:

HTML:

<table>
    <tr class="container">
        <th class="outer green">first</th>
        <th class="inner outer blue">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        </th>
    </tr>
</table>

CSS:

.container {
    width: 100%;
    border: 1px solid red;
    height: 100%;
}

.outer {
    display: table-cell;
    width: 50%;
}

.green {
    background: green;
}

.blue {
    background: blue;
}

JSFiddle Example

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • you can add vertical-align: middle if it is displayed as table-cell – Danny van Holten Jul 05 '14 at 01:26
  • @DannyvanHolten Normally, yes. It doesn't seem to work for this situation, though, so I didn't include that bit. I'm not sure why it doesn't work. – TylerH Jul 05 '14 at 01:26
  • 1
    You probably didn't understand what I want. You just removed divs and added green and blue classes to the table cells themselves. Ofcourse the colors will fill the whole space. What I need is to stretch the divs, because they have much more complicated content then just color that you could simply set as a background for the cell. I used green and blue classes just to demonstrate what sizes the divs have. Here is the fixed JSFiddle that you provided, you can see it still doesn't work: http://jsfiddle.net/R58fv/ – Georgii Oleinikov Jul 05 '14 at 13:23
  • @GeorgiiOleinikov No, that's not the JSFiddle I provided, so it wouldn't make sense to say mine doesn't work; you've added the divs and old classes back in. Divs aren't meant to be placed inside tables; divs and tables work in opposite ways, so it becomes a battle with yourself to try and control your content. See this answer: http://stackoverflow.com/a/1111197/2756409 – TylerH Jul 05 '14 at 18:46
  • 1
    @TylerH Maybe that's not a good idea, but I explained the incentive in the question: div is a widget that has to be placed in the table headers – Georgii Oleinikov Jul 05 '14 at 23:31