html - 如何使表格标题居中?

标签 html css html-table center caption

我在表格上有一个很长的标题,并希望它们都彼此中心对齐。

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;
}

https://codepen.io/anon/pen/RZyxzg

最佳答案

使用flexbox,使用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>

关于html - 如何使表格标题居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45815155/

相关文章:

javascript - 在选择菜单中选择的值未发送到服务器

html - 表格行未与 IE 中的表格标题正确对齐

android - 为什么即使使用width=device-width,android浏览器视口(viewport)也比手机实际屏幕尺寸小很多?

jQuery 动画。如何在自动设置顶部和左侧

jQuery 树表 : Get first and last child of all nodes

android - 在 Android 2.0+ 浏览器中使用 HTML5 Geolocation API

css - 如何使用 box-shadow 制作 css 动画

html - table-footer-group 和 table-header-group 不占用表格的宽度

表格内的php随机单元格

javascript - 用于删除可拖动的可克隆 DIV 元素的按钮