CSS width and max-width combined

Niet the Dark Absol picture Niet the Dark Absol · Oct 29, 2011 · Viewed 97.7k times · Source

I have the following code:

<table style="width: 100%; max-width: 800px; table-layout: fixed;">
    ... table stuff here ...
</table>

I think it's obvious I intend for the table to take the full width available, with a capped maximum size of 800px.

This works in Internet Explorer and Firefox, however in Chrome it appears that the max-width is being ignored when width is present.

I have tried using max-width: 100%; width: 800px;, which again works in IE and FF but the max-width is ignored in Chrome. I have tried using just max-width: 800px but in Chrome the table comes out 1159 pixels wide instead... !

If anyone can help with this, it would be much appreciated.

Answer

heisenberg picture heisenberg · Oct 29, 2011

Add

display: block;

to the table element's style attribute (preferably in a CSS file or the <style> section of the document rather than as in inline style).

<div> elements have display: block by default, while <table> elements have display: table; by default. Your problem is that the max-width property only applies to block elements, so you have to apply display: block; to the table.