如何将 div 与另一个 div 对齐并将其集中在一个表格中?
echo '<table border="0" cellspacing="1" cellpadding="7" width="100%">';
echo '<tr bgcolor="'.$config['site']['purple'].'">';
echo '<td align="center">';
foreach($image as $img){
echo '<div style="width:24%;height:130px;position:relative;float:left;margin:2px auto;background-image:url('.$img['location'].');background-size:100%;"></div>';
}
echo '</td>';
echo '</tr>';
echo '</table>';
我将 divs 一个完美地放在一边,但它仍然在<td>
的左边而不在中心。
如何解决?
答案 0 :(得分:1)
检查这个JSfiddle。你需要的格式比我想象的少:p
注意:我手动添加了一些div用于测试目的。
HTML
<table border="0" cellspacing="1" cellpadding="7" width="100%">
<tr bgcolor="purple">
<td align="center">
<div class="image" style="background-image: url(https://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png)"></div>
<div class="image" style="background-image: url(https://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png)"></div>
<div class="image" style="background-image: url(https://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png)"></div>
<div class="image" style="background-image: url(https://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png)"></div>
<div class="image" style="background-image: url(https://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png)"></div>
</td>
</tr>
</table>
CSS
div.image {
display: inline-block;
width: 24%;
height:130px;
border: 1px solid black; /* For visual reference */
}
感谢TR元素上的align="center"
,div是居中的
当您清理代码(从样式中分离HTML)时,您也可以在该行上使用CSS类。
编辑:
我做过的一点测试(没有CSS课程)。
$config['site']['purple'] = "purple";
$image = array(
'StackOverflow' => 'https://stackoverflow.com/content/stackoverflow/img/apple-touch-icon.png',
'W3C' => 'http://www.propra.nl/img/w3c.png',
'CSS' => 'http://a.dryicons.com/images/icon_sets/coquette_part_5_icons_set/png/128x128/css_file.png',
'HTML' => 'http://a.dryicons.com/images/icon_sets/coquette_part_5_icons_set/png/128x128/html_file.png',
'LifeIsPain' => 'http://oldpunks.com/1iconlifeispain.gif'
);
echo '<table border="0" cellspacing="1" cellpadding="7" width="100%">
<tr bgcolor="'.$config['site']['purple'].'">
<td align="center">';
foreach($image as $name => $location){
echo '<div style="display: inline-block; width: 24%; height:130px; border: 1px solid black; background-image:url('.$location.');background-size:100%;"></div>';
}
echo '</td>
</tr>
</table>';