htmldiv盒子居中
时间: 2023-12-23 16:27:53 浏览: 42
以下是两种将HTML div盒子居中的方法:
1. 使用相对定位和transform属性:
```html
<style>
.bigbox {
width: 500px;
height: 500px;
background-color: pink;
margin: 0 auto;
position: relative;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
text-align: center;
line-height: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="bigbox">
<div class="box">居中</div>
</div>
</body>
```
2. 使用padding和margin属性:
```html
<style>
.bigbox {
width: 500px;
background-color: pink;
margin: 0 auto;
padding: 9% 0;
box-sizing: content-box;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
text-align: center;
line-height: 100px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="bigbox">
<div class="box"></div>
</div>
</body>
```
相关推荐
![html](https://img-home.csdnimg.cn/images/20210720083451.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)