- 定位+margin
javascript
<style>
*{
margin: 0;
padding: 0;
}
.father{
width: 400px;
height: 400px;
border: 1px solid;
position: relative;
}
.son{
position: absolute;
width: 200px;
height: 200px;
background-color: red;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<body>
- 定位+transform
javascript
<style>
.father{
width: 400px;
height: 400px;
border: 1px solid;
position: relative;
}
.son{
position: absolute;
width: 200px;
height: 200px;
background-color: blue;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<body>
- flex
javascript
<style>
.father{
width: 400px;
height: 400px;
border: 1px solid;
display: flex;
align-item: center;
justify-content: center;
}
.son{
width: 200px;
height: 200px;
background-color: pink;
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
<body>
- grid布局
- table布局