Skip to content Skip to sidebar Skip to footer

How Do I Make A Div Fill An Entire Table Cell?

I'm trying to fill the center cell of a table with a div element. For the purposes of illustrating the problem, the div is styled with a red background. It seems to work in Chrom

Solution 1:

I did some more research on this and collected some info that might come in handy to others trying to solve similar problems. The CSS spec says the following three things that I think are important:

First, re: specifying the height (of a div) as a percentage:

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'.

http://www.w3.org/TR/CSS21/visudet.html#the-height-property

... a height of 'auto' won't fill the cell unless the content is taller than the cell's minimum height. But if we try to explicitly set the height of the containing cell or row, then we run into the following problem:

CSS 2.1 does not define how the height of table cells and table rows is calculated when their height is specified using percentage values.

http://www.w3.org/TR/CSS21/tables.html#height-layout

Since the spec doesn't define it, I guess it's not too surprising that Chrome and IE choose to calculate it differently.

Alternatively, (as xec indirectly pointed out) trying to use relative positioning has the following spec problem:

The effect of 'position:relative' on table-row-group, table-header-group, table-footer-group, table-row, table-column-group, table-column, table-cell, and table-caption elements is undefined.

www.w3.org/TR/CSS21/visuren.html#propdef-position

So I've concluded there's probably not a pure CSS way to solve the problem that one can reasonably expect to work on most browsers.

At first, I thought, "Wow, the CSS spec is pretty shoddy and incomplete for leaving all this stuff undefined." As I thought about it more, though, I realized that defining the spec for these issues would a lot more complicated than it appears at first. After all, row/cell heights are calculated as a function of the heights of their content, and I want to make the height of my content a function of the row/cell height. Even though I have a well-defined, terminating algorithm for how I want it to work in my specific case, it's not clear that the algorithm would easily generalize to all the other cases that the spec would need to cover without getting into infinite loops.

Solution 2:

Just set the table cell to: position:relative and the div to:

position:absolute;
top:0;
left:0;
width:100%;
height:100%;

Edit 2017: DEMO BELOW:

Note how you cannot see the red td because the yellow div covers it entirely...

#expandingDiv {
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  background: yellow;
}
<tablestyle="width: 120px"><tr><tdstyle="background: blue">blue&nbsp;td</td><tdstyle="background: green">green&nbsp;td</td></tr><tr><tdstyle="background: red; position: relative"><divid='expandingDiv'> yellow div </div></td><tdstyle="background: orange">
      Some longer text which makes the bottom two tds expand dynamically.
    </td></tr></table>

Solution 3:

Although I liked Craig's answer and will not use the approach in this answer myself, I did get quite far with this jsFiddle.

It relies on a hack, however: Setting height: 1px on the table. It works in Chrome, FF, IE11 and Edge (all that I tested), but Chrome starts misbehaving in edge cases. See the fiddle. Here are the interesting bits:

table {
    width: 100%;
    /* Whý does this make it work? */height: 1px;
}

td {
    border: 10px solid blue;
    height: 100%;
}

#container {
    width: calc(100% - 20px);
    height: calc(100% - 20px);
    border: 10px solid black;
}

Too much of a hack-smell to me.

Solution 4:

have you tried changing css to:

#centerCell{
    border: 1px solid black;
    height:100%;
}

seems to work for me on edge, firefox and chrome

Solution 5:

Simply set the line height of the div; as long as its display is still a block level element. There is no need for relative or absolute positioning or hard coding of the height at the div level or any of its parents. Works in IE 8+, Firefox, and Chrome.

Example:

line-height: 50px;
// orline-height: 2em;

Post a Comment for "How Do I Make A Div Fill An Entire Table Cell?"