```
//子元素是行内元素
<div id="box">
<span class="child">content</span>
</div>
//样式
#box{
width: fit-content;//让内容充满
margin: auto;
}
```
![](https://img.kancloud.cn/08/31/08317bdf2a02d14e16ffb41b37aa87c8_1332x107.png)
```
//子元素是块元素
<div id="box">
<div class="child">content</div>
</div>
通过定位,计算距离
#box {
height: 200px;
background: red;
position: relative;
}
.child {
width: 100px;
height: 100px;
background-color: aqua;
position: absolute;
left: calc(50% - 50px);
top: calc(50% - 50px);
}
```
![](https://img.kancloud.cn/47/87/4787d50666e15f194731fb59e4bea2a2_1611x315.png)