Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a TD to be a certain height with CSS?

Although some questions similar to this one have been asked before, this one is a little different.

I have a table that looks a little something like this:

<table>
 <tr>
  <td style="height: 100px;">
   <img src="..." style="height: 100px;" />
   <img src="..." style="height: 100px; position: relative; top: -100px;" />
  </td>
 </tr>
</table>

This will overlay the second image on the first one. However, the td insists on being 200px tall even though there is only 100px of content. Setting the td's height does nothing, which seems consistent with the answers to the other questions.

However, I do not have the option of embedding the contents in a DIV and setting the height to 100px because the td will still insist on being 200px tall.

How can I tell the td to just be 100px tall?


Edit: Oh, and using absolute positioning is out of the question too because a lot of DOM manipulation goes on in the page and stuff gets moved around - whereas the absolutely positioned stuff does not.


Edit: jsFiddle example can be found here.

like image 347
Nathan Osman Avatar asked Mar 13 '11 01:03

Nathan Osman


People also ask

How do I change td height in CSS?

Complete HTML/CSS Course 2022 To set the cell width and height, use the CSS style. The height and width attribute of the <td> cell isn't supported in HTML5. Use the CSS property width and height to set the width and height of the cell respectively.

How do I fix my TD size?

There are multiple ways to fix the width for <td> tag. Some of them are mentioned below: Using width attribute: The <td> tag has width attribute to control the width of a particular column. By assigning a numeric value to this attribute between 0 to 100 in terms of percentage(or you can use pixel format).

How do you change tr height?

The height of rows 'tr' in a table can be fixed very easily. This can be done by adding the height attribute in the tr tag. If the height is not specified, the height of the row changes according to the content.


1 Answers

This has nothing to do with the td really, but with the nature of position: relative. A relative element's space stays reserved in the document flow.

Get rid of the relative, and use position: absolute on the first image instead.

Edit: I just saw your edit. Hmmm. Thinking.

Two workaround ideas come to mind:

  • Slap a overflow: hidden on the td

  • If that doesn't work in all browsers or isn't valid (I'm not 100% sure right now) Put the two images into a <div height='100px;'> and put a overflow: hidden on that.

like image 191
Pekka Avatar answered Sep 27 '22 22:09

Pekka