Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't IE respect table width with fluid image child

Consider the following code:

HTML:

<table>
    <tr>
       <td><img src="http://watduck.jpg.to" /></td>
    </tr>
</table>

CSS:

table { width: 10% }
img { max-width: 100% }

The image should obviously be a 10th the width of the window, which is exactly what it is in every browser except IE, where it simply falls back to its original size.

However, consider this:

HTML:

<div><img src="http://watduck.jpg.to" /></div>

CSS:

div { width: 10% }
img { max-width: 100% }

which IE does get right, and displays at a 10th of the window width.


So, here's the question: what causes this behavior, and what could possibly be done to force IE to respect the table's width?

Tested in IE8 & IE9 (don't care about IE7 and below).

like image 215
Joseph Silber Avatar asked Feb 05 '12 05:02

Joseph Silber


People also ask

Can every cell of table have different width?

You can't have cells of arbitrarily different widths, this is generally a standard behaviour of tables from any space, e.g. Excel, otherwise it's no longer a table but just a list of text.

How do you fix a fixed width table?

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).


1 Answers

If you specify table-layout: fixed; in the table css it works.

There seems to be some contradictory terminology in the standard regarding table layouts. In particular, table-layout: auto; says this:

The column width is set by the widest unbreakable content in the cells

Since the images content is unbreakable, it sets the width of the cell to the size of the content. The max-width seems to be overriden by it.

like image 61
Erik Funkenbusch Avatar answered Nov 15 '22 23:11

Erik Funkenbusch