<div id="wrapper">
<div class="content">
</div>
</div>
实现方式1:
#wrapper {
width: 60%;
height: 500px;
background-color: #fc0;
position: relative;
}
.content {
width: 30%;
background-color: chartreuse;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50% -50%);
}
实现方法2:
#wrapper {
width: 60%;
height: 500px;
background-color: #fc0;
position: relative;
}
.content {
width: 300px;
height:200px;
background-color: chartreuse;
position: absolute;
top: 50%;
left: 50%;
margin-top:-100px;
margin-left:-150px;
}
实现方式3:
#wrapper {
width: 60%;
height: 500px;
background-color: #fc0;
display: flex;
justify-content: center;
align-items: center;
}
.content {
width: 30%;
background-color: chartreuse;
}
实现方法4:
#wrapper {
width: 60%;
height: 500px;
background-color: #fc0;
position: relative;
}
.content {
width: 200px;
height: 200px;
background-color: chartreuse;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
实现方法5:
<div id="wrapper">
<div>hello world</div>
</div>
#wrapper {
width: 600px;
height: 500px;
background-color: #fc0;
display: table-cell;
vertical-align: middle;
text-align: center;
}