4

I have demonstrated my issue in this JSFiddle: http://jsfiddle.net/6jJaF/

If you right click on the red cell around the image, and click 'Inspect Element', you'll see that even though the IMG element is 200px in height, the TR and TD elements around it are each 205px in height. Why is its height greater than the image? How to get rid of this extra height?

This problem doesn't occur if the content of the TD element is text instead of IMG as can be seen in the second TABLE in this example.

Here is the HTML code.

<table>
    <tr>
        <td><img src="http://i.imgur.com/dj7aqdo.jpg"/></td>
    </tr>
</table>

<table>
    <tr>
        <td>Foo</td>
    </tr>
</table>

Here is the CSS code.

td {
    padding: 0;
    background: red;
    height: 200px;
}

img {
    height: 200px;
}
Lone Learner
  • 18,088
  • 20
  • 102
  • 200

3 Answers3

7

Set the vertical alignment of the image. For example:

img {
    height: 200px;
    vertical-align:top;
}

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 2
    @ZenOut The default vertical alignment for inline elements is baseline, and this reserves a little gap for descender text elements like `g`, `y` and `j`. Changing the alignment removes that gap. – j08691 Oct 17 '17 at 13:12
0

You need to set the cell padding and cell spacing to 0 and "collapse"

Set cellpadding and cellspacing in CSS?

Community
  • 1
  • 1
Kodlee Yin
  • 1,089
  • 5
  • 10
0

Simply add this to your current CSS:

table {
    border-collapse:collapse;
} 

Demo: http://jsfiddle.net/6jJaF/2/

Arbel
  • 30,599
  • 2
  • 28
  • 29