定义一个高度为300像素*200像素的3*2的表格,要求其在页面中水平和垂直居中
时间: 2024-03-26 18:39:26 浏览: 19
可以使用HTML和CSS来定义一个高度为300像素*200像素的3*2的表格并使其在页面中水平和垂直居中。
HTML代码:
```
<div class="table-container">
<table class="my-table">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>5</td>
<td>6</td>
</tr>
</table>
</div>
```
CSS代码:
```
.table-container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.my-table {
width: 600px;
height: 400px;
margin: auto;
border-collapse: collapse;
}
.my-table td {
border: 1px solid black;
text-align: center;
}
```
解释:
1. 用一个 `div` 元素包裹 `table` 元素,并为这个 `div` 元素定义样式,使用 `display: flex` 属性使其成为一个 flex 容器,使用 `justify-content: center` 和 `align-items: center` 属性使其在页面中水平和垂直居中,使用 `height: 100vh` 属性使其占满整个页面的高度。
2. 为 `table` 元素定义样式,使用 `width: 600px` 和 `height: 400px` 属性定义其宽度和高度,使用 `margin: auto` 属性使其在容器中水平居中,使用 `border-collapse: collapse` 属性使其边框合并。
3. 为 `td` 元素定义样式,使用 `border: 1px solid black` 属性定义其边框样式,使用 `text-align: center` 属性使其中的内容居中对齐。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)