Chrome, Safari ignoring max-width in table

HtmlGoogle ChromeSafariScaling

Html Problem Overview


I've got HTML code like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251" /> 
    <title></title> 
    </head> 
    
    <body> 
       <table style="width:100%;"> 
          <tr> 
             <td> 
                <table style="width:100%; max-width:1000px; background:#000099;"> 
                       <tr> 
                           <td> 
                               001 
                           </td> 
                       </tr> 
                   </table> 
               </td> 
           </tr> 
       </table> 
    </body> 
    </html> 

The problem is that Chrome and Safari are ignoring "max-width:1000px" My friend has found that we can prevent it by adding "display:block" for the inner table, and it's somehow working.

So, what I want to know is - are there any other ways of solving this problem and why is this happening?

Html Solutions


Solution 1 - Html

Max-width applies to block elements. <table> is neither block nor inline. Ambiguous enough? haha. You can use display:block; max-width:1000px and forget about width:100%. Chrome and Safari follow the rules!

Edit May 2017: please note, this comment was made 7 years ago (in 2010!). I suspect browsers have changed a bunch over the years (I wouldn't know, I no longer do web design). I recommend using a more recent solution.

Solution 2 - Html

I know this has been answered for a while and with a working workaround, but the answer stating that max-width only applies to block elements and citing a source that's not the spec is completely incorrect.

The spec (the CSS 3 spec for CSS Intrinsic & Extrinsic Sizing refers to the CSS 2.1 spec on this rule) clearly states:

>all elements but non-replaced inline elements, table rows, and row groups

which would mean it should apply to table elements.

It means that WebKit's behavior of not honoring max-width or min-width on table elements is incorrect.

Solution 3 - Html

I think what you're looking for here is:

table-layout: fixed

Apply this style to the table and your table will respect the width you assign to it.

Note: Applying this style directly in Chrome will look like it is not working. You need to apply the style in your CSS / HTML file and refresh the page.

Solution 4 - Html

Wrap inner table with div and set max-width to this wrapping div.

Solution 5 - Html

I had the same issue. I used a table as a means to center my content on the page, but safari ignored width:100%; max-width:1200px as a style I applied to the table. I learned that if I wrap the table in a div and set left and right margins on auto on the div, it would center on the page and obey the max and min width attributes in safari and firefox on the mac. I have yet to check explorer or chrome on windows. Here is an example:

<div style="position:relative; width:100%; max-width:1200px; min-width:800px; margin-left:auto; margin-right:auto">

Then I nested the table inside the div...

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">

Solution 6 - Html

I just came across this answer and it is worth noting that according to MDN (https://developer.mozilla.org/en-US/docs/Web/CSS/max-width), their compatibility table for max-width says:

|             Feature  | Chrome        | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
--------------------------------------------------------------------------------------------------------
|applies to <table> [1]| Not supported | (Yes)           | Not supported     | (Yes) | Not supported   |

"[1] CSS 2.1 explicitly leaves the behavior of max-width with

undefined. Therefore any behavior is CSS2.1-compliant; newer CSS specifications may define this behavior, so Web developers shouldn't rely on a specific one now."

There is some cool stuff on MDN though such as "fill-available" and "fit-content" - we have some things to look forward to when the spec stabilises and is explicit on this front...

Solution 7 - Html

Here is an unambiguous reference: 10 Visual formatting model details (w3.org) > In CSS 2.1, the effect of 'min-width' and 'max-width' on tables, inline tables, table cells, table columns, and column groups is undefined.

References given previously in this thread, erroneously read “table rows” as “table”. This one however, really says “table”. That's part of CSS2, but CSS3 refers to CSS2 for the basis of max-width.

So it's just undefined and browser are free to do what they wish and it's unsafe to rely on it.

Solution 8 - Html

I was able to fix my problem with media query:

I replaced

max-width: 360px

with

@media (min-width: 440px) {
    width: 360px;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGaGar1nView Question on Stackoverflow
Solution 1 - HtmlJasonView Answer on Stackoverflow
Solution 2 - HtmldustinwilsonView Answer on Stackoverflow
Solution 3 - HtmlSerj SaganView Answer on Stackoverflow
Solution 4 - HtmlJan MazánekView Answer on Stackoverflow
Solution 5 - HtmlCliffView Answer on Stackoverflow
Solution 6 - HtmljcuenodView Answer on Stackoverflow
Solution 7 - HtmlHibou57View Answer on Stackoverflow
Solution 8 - Htmluser2908392View Answer on Stackoverflow