1. Horizontal center
<div class="box"></div>
.box{
width: 100px;
height: 100px;
margin: 0 auto;
background: #E33;
}
Copy the code
2. Center horizontally and vertically
(1) Absolute positioning horizontal vertical center
<div class="box">
<div></div>
</div>
.box {
width: 100%;
height: 500px;
background: #e33;
position: relative;
}
.box>div{
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: gold;
}
Copy the code
(3) Flex flex layout is vertically and horizontally centered
<div class="box">
<div></div>
</div>
.box {
width: 100%;
height: 500px;
background: #e33;
display: flex;
align-items: center;
justify-content: center;
}
.box>div{
width: 100px;
height: 100px;
background: gold;
}
Copy the code