This question is similar to this one, but with an added requirement: I want the table to have 100% width inside its parent. Copied image from that question:
So I want the "green part" to take up 100% width of the parent, and with the yellow spacing between cells.
We can use negative margin on the table to 'undo' the red spacing, at least for most cases.
However, that does not always work with fluid layouts. Actually it works fine as long as there is enough content to make the table take up 100% width (with the table having width:auto
). The problem arises when there is not enough content to do that, because the obvious solution width:100%
does not fix that: the table will be wide enough to fit the parent with the border spacing included, but we're stripping that off, so the table is not wide enough anymore.
The only 'solution' I have found so far is force-filling the table with long, preferably invisible, content so that it will fill up to 'real' 100%. But I wish for a pure-css solution for this... I'd also prefer not using calculations / expressions to have as large browser support as possible.
body {padding: 1em}
section {background-color: yellow}
table {background-color: pink}
td {background-color: lightgreen}
table {border-spacing: 1em}
table.working, table.failing {margin: -1em;}
table.failing {width: 100%}
<body>
<section>
<h2>Simple table</h2>
<table>
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
</table>
<br>
<h2>Succesful "100% width" for both cells</h2>
<table class="working">
<tr>
<td>cell with a lot of content to make sure that the table will be wide enough</td>
<td>cell with a lot of content to make sure that the table will be wide enough</td>
</tr>
</table>
<br>
<h2>Unsuccesful 100% width</h2>
<table class="failing">
<tr>
<td>table with</td>
<td>100% width</td>
</tr>
</table>
<br>
</section>
</body>