テーブルのセル内に 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>