April 19, 2012

Table Layout Issue

Question by ic3b3rg

I have the following simplified layout:

jsfiddle: http://jsfiddle.net/rersW/

<table>
  <tr>
    <td style="border:1px solid black;height:300px;width:100px">A</td>
    <td style="border:1px solid black;height:100%">
      <div style="border:1px solid blue;height:100%;width:100px">B</div>
    </td>
  </tr>
</table>

I need the blue outlined div to fill its cell. I’ve tried box-sizing on the div and the cell as well as changing the display of both (inline-block, etc.). Nothing I try is working.

The contents of cell “A” determine the height of the table.

The contents of cell “B” determine the width of its cell.

It must work identically in Chrome, Firefox, Safari, Opera & IE 8+

TIA

Answer by user1289347

Height:100% requires that the parent has an explicit height. Set the same height in cell b as you do in cell a, which is determining the table height, in this case 300px and the child div will expland.

EDIT
If you can’t explicitly define any of the parent heights you will need to use JS/Jquery. See here http://jsfiddle.net/rersW/3/

Answer by Starx

The reason is height: 100%; needs to be have a parent with height defined , or else you have to

  • either fix the height.
  • or Chain height: 100% all the up to body, if no height is fixed anywhere

TO illustrate my point upgrade your markup to this

<td valign="top" style="border:1px solid black; height: 300px;">
  <div style="border:1px solid blue;height:100%; display: block;width:100px">B</div>
</td>

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!