I’m looking for a way to fit table content to table size.
For example, I have table with width and height =100, and 5 col and 5 row, if row or col change to 20 only content re-size to fit into old table size.
I wrote some code,
but it resize table if row or col change!
<style>
.tbl {
max-width: 100px;
max-height: 100px;
height: 100px;
width: 100px;
border: 1px solid #333;
}
.blck {
border: 1px solid #0F3;
background:#F00;
}
.wht {
border: 1px solid #0F3;
background:#069;
}
</style>
<?php
$w = 15;
$h = 15;
$max = 100;
if ($w>=$h) {
$cell = ($max/$w);
} else {
$cell = ($max/$h);
}
$clr = 0;
$tbl .= '<table width="'.$max.'px" align="center" border="0" height="'.$max.'px" class="tbl">';
for ($r=1;$r<=$h;$r++){
$tbl .= '<tr>';
for($c=1;$c<=$w;$c++){
if($clr == '0'){
$tbl .= '<td width="'.$cell.'%" height="'.$cell.'%" class="blck"></td>';
$clr = 1;
} else {
$tbl .= '<td width="'.$cell.'%" height="'.$cell.'%" class="wht"></td>';
$clr = 0;
}
}
$tbl .= '</tr>';
}
$tbl .= '</table>';
echo $tbl;
?>
be careful when you add borders to the table cells, because it will add 2px to the current width/height.
use floor() when you divide the table width by cols/rows count, because width and height attributes doesn’t accept fractions.
And it seems that height attribute is not accurate (no idea why).
Also you have to consider text size. for example i put:
Check this out: