How to set max width of block element inside table to 100% of viewable area?

Go To StackoverFlow.com

0

To understand what I mean, please see this snippet:

<table style="width: 100%">
    <tbody><tr><td>
        <div style="width: 100%">
            <img src="myimage.png" />
        </div>
    </td></tr></tbody>
</table>

If myimage.png is smaller than the browser window, the div is renderd to fit the window size. So far so good. But if the img is larger, everything exceeds the viewable area.

Is there a way to let set the img width to something like "100% of the image, but not larger than 100% of the viewable area"?

UPDATE: max-width: 100% for the image seems not to do the job inside tables:
http://jsfiddle.net/h58Ra/

2012-04-04 08:14
by Zeemee


3

max-width is exactly what you should use: http://jsfiddle.net/h58Ra/ (on the img). The div should never exceed the size with or without width: 100%.

If you remove the max-width: 100% in the fiddle you'll notice how the div still doesn't get larger, even though the img does.

Apparently the div was inside a table which for some reason made it fail. We reached a solution together: (from the comments) "If I set style="table-layout: fixed; width: 100%"for the table, everything works fine".

2012-04-04 08:19
by powerbuoy
Truth in your words I find :) I now have to check my real html/css.. - Zeemee 2012-04-04 08:27
Ok, it is because everything is nested inside a table, please see my updates. Thank you - Zeemee 2012-04-04 08:47
You didn't actually update the fiddle (or at least forgot to change the link), but I did now and it still works fine: http://jsfiddle.net/h58Ra/1/. On a side note, what is the table doing there anyway? Seems like it serves no purpose (and is completely un-semantic) at least in the HTML posted in your question - powerbuoy 2012-04-04 08:51
Thank you for updating the fiddle. And yes, shame on me because I'm using tables for layout, but it is because of JSF which renders layout grids as tables. I found the solution, thanks to your help, while looking for the table/max-width problem. If I set style="table-layout: fixed; width: 100%"for the table, everything works fine. Maybe you want to update your answer like this. Here's your checkmark anyway (in advance) - Zeemee 2012-04-04 09:05
Ads