元素布局是
<div class="container">
<div class="div">
</div>
</div>
要是当行文本直接text-align:center ,line-height等于height
1.使用position
.container {
width: 1000px;
height: 200px;
background-color: aquamarine;
position: relative;
}
.div {
width: 30px;
height: 30px;
background-color: red;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
2.position+transform
.container {
width: 1000px;
height: 200px;
background-color: aquamarine;
position: relative;
}
.div {
width: 30px;
height: 30px;
background-color: red;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
3.flex
.container {
width: 1000px;
height: 200px;
background-color: aquamarine;
display: flex;
justify-content: center;
align-items: center;
}
.div {
width: 30px;
height: 30px;
background-color: red;
}