效果图
方法一
<style type="text/css">
.container{
width: 100%;
height: 100px;
background-color: blue;
position: relative;
}
.box{
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}
</style>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
方法二
.container{
width: 100%;
height: 100px;
background-color: blue;
position: relative;
}
.box {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 50%;
top: 50%;
margin: auto;
margin-left: -50px;
margin-top: -50px;
}
方法三
.container{
width: 100%;
height: 100px;
background-color: blue;
display:flex;
justify-content:center;
align-items:center;
}
.box {
width: 100px;
height: 100px;
background-color: red;
}