html - 如何使用CSS将表格放在页面的中央?

标签 html css html-table center

我正在使用以下代码。如何使用 CSS 将此表格放在页面的中央?

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="styles.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>The Main Page</title>
    </head>
    <body>
        <table>
            <tr>
                <td><button class="lightSquare"></button></td>
                <td><button class="darkSquare"></button></td>
                <td><button class="lightSquare"></button></td>
                <td><button class="darkSquare"></button></td>
                <td><button class="lightSquare"></button></td>
                <td><button class="darkSquare"></button></td>
                <td><button class="lightSquare"></button></td>
                <td><button class="darkSquare"></button></td>
            </tr>
        </table>
    </body>
</html>

最佳答案

2022 年编辑:请使用 Flexbox

使用 Flexbox,说明在这里:https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container

.box {
  background-color: red;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 400px;
  width: 400px;
}

.box div {
  background-color: blue;
  width: 100px;
  height: 100px;
}
      
<div class="box">
  <div></div>
</div>
      


下面保留了 2021 年的答案。

html, body {
    width: 100%;
}
table {
    margin: 0 auto;
}

示例 JSFiddle

垂直对齐 block 元素并不是最简单的事情。以下是一些方法。

关于html - 如何使用CSS将表格放在页面的中央?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9402856/

相关文章:

javascript - 可编辑行在包含 JSON 数据的表中不起作用?

html - 将 div 附加到另一个 div 的右侧

html - 全宽列表项网格 Bootstrap 3

html - 老板平板电脑上的细条纹

c# - Internet Explorer 中的渐进式表呈现

php - foreach()表有空白吗?

html - 3 列网格导航栏 Bootstrap

css - 最奇怪的 css 错误。无法获取第一个元素

javascript - jquery 可调整大小的处理程序随内容移动?

html - 没有 <table> 是否可以实现这种对齐?