我正在使用以下代码。如何使用 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/