9

I have an HTML table whose cells contain divs with display:inline-block, containing text of varying sizes.

I need the text to align on the baseline, and I need the background colors of the divs to fill the height of the cells. For the largest font, the background color does fill the cell, but it doesn't for the smaller fonts:

How it looks in Firefox

Is this possible? Obvious solutions like div { height:100% } seem to be scuppered by the varying font sizes.

Here's the code so far:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <style type="text/css">
    table td {
        vertical-align: baseline;
        padding: 0;
    }

    td div {
        display: inline-block;
    }
  </style>
</head>
<body>
    <table>
        <tr>
            <td style="background-color:cyan">
                <div style="background-color:pink;">Pink</div>
                <div style="background-color:green;">Green</div>
            </td>
            <td style="background-color:cyan">
                <div style='font-size: 40pt; background-color:yellow;'>
                    Big yellow text
                </div>
            </td>
            </tr>
  </table>
</body>
</html>

It's also on jsfiddle here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RichieHindle
  • 272,464
  • 47
  • 358
  • 399

3 Answers3

2

Not perfect, but the closest I could get:

http://jsfiddle.net/gfPkV/14/

ptriek
  • 9,040
  • 5
  • 31
  • 29
  • Works great but, in the rare case that a user is zoomed in even a bit it's off again. – brenjt Dec 01 '11 at 17:52
  • @ptriek: Sadly the text is no longer aligned along the baseline. – RichieHindle Dec 01 '11 at 17:54
  • And would it be an option to add an extra div for the fill colour? See my updated link - this preserves baseline alignment (though still a bit messy) – ptriek Dec 01 '11 at 19:33
  • @ptriek: Lovely in Chrome, but broken in Firefox and IE. `position:relative`'s behaviour is undefined for ``, and Firefox ignores it. I'm not sure what IE thinks it's doing, but it's sure not right. :-) – RichieHindle Dec 02 '11 at 07:48
2

Quick and dirty fix:

CSS

div {
    line-height:60px;
    height:60px;
    vertical-align:middle;
}

Demo: http://jsfiddle.net/2YbBg/

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
1

I read once, that td does not report it's height. So any height: 100% or height:auto, etc.. won't work.

So my solution is here: http://jsfiddle.net/UGTYP/

It changes height of "pink" text to the height of "yellow" div with javascript.

JercSi
  • 1,037
  • 1
  • 9
  • 19