为什么不在表格的单元格中包含一个 div,名称的跨度和一个用 css 或 br 开始的新行?您可以使用 CSS 将单元格的内容居中
尝试输出如下内容:
<table>
<tr>
<td>
<div>
<span class="user-name">NAME</span>
<img class="user-face" src=""/>
</div>
</td>
<td>
<div>
<span class="user-name">NAME</span>
<img class="user-face" src=""/>
</div>
</td>
</tr>
</table>
和风格:
.user-name {
width: 10em;
display: table-cell;
text-align: center;
}
.user-face {
width: 10em;
height: 10em;
}
当然假设他们有偶数个朋友...... :) 如果没有,你必须在末尾添加一个空的 td !
编辑:这次完全基于 div 并包括 PHP。也会处理奇数个朋友:)
<div id="user-data">
<?php
$cellPosition = "left";
foreach ($users as $user) {
$userScreenName = $user->screen_name;
$userName = $user->name;
$userImage = $user->profile_image_url;
$userDescription = $user->description;
if ($cellPosition == "left") {
echo "<div class=\"user-table-row\">\n";
}
echo "<div class=\"cell $cellPosition\">\n";
echo " <span class=\"user-name\">$userName</span>\n";
echo " <img class=\"user-face\" src=\"$userImage\"/>\n";
echo "</div>";
if ($cellPosition == "right") {
echo "</div>";
$cellPosition = "left";
}
else {
$cellPosition = "right";
}
}
?>
</div>
样式:
<style>
#user-data {
margin:2em;
}
div.user-table-row {
margin: 2em;
width: 24em;
clear: right;
}
div.cell {
width:10em;
display: table-cell;
text-align: center;
margin-bottom: 2em;
}
div.left {
float: left;
}
div.right {
float: right;
}
span.user-name {
width: 10em;
}
img.user-face {
width: 10em;
height: 10em;
}
</style>