我在桌子上有一个很长的标题,并且希望它们都与彼此的中心对齐。
HTML:
<table>
<caption>This is a very long caption.</caption>
<tr>
<td>1</td><td>2</td>
</tr>
<tr>
<td>3</td><td>4</td>
</tr>
</table>
CSS:
caption {
white-space: nowrap;
}
答案 0 :(得分:1)
使用align-items:center
table {
display: flex;
flex-direction: column;
align-items: center;
border: red solid
}
<table>
<caption>This is a very long caption.</caption>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
答案 1 :(得分:0)
已经 。使用text-align: center
您可以提供边框,并看到 long 文本将扩展表格的整个宽度。 (另请参阅将width
设置为table
)
注意:这似乎不适用于Firefox - 使用white-space: nowrap
时,表格不会像Chrome一样调整其宽度。
见下面的演示:
table {
border-collapse: collapse;
}
caption {
white-space: nowrap;
}
td {
border: 1px solid;
text-align: center;
}
&#13;
<table>
<caption>This is a very long caption and even longer</caption>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
</table>
&#13;