<style>
.box {
width: 200px;
height: 400px;
background: yellow;
text-align: center;
}
.box .child {
width: 100px;
height: 100px;
background: orange;
display: inline-block;
vertical-align: middle;
}
.box .flag {
display: inline-block;
width: 0;
height: 100%;
vertical-align: middle;
}
</style>
<body>
<div class="box">
<span class="flag"></span>
<img src="https://images2015.cnblogs.com/blog/315302/201704/315302-20170417105850790-1814593961.png"
class="child" />
</div>
</body>
思路解析:
<style>
.box {
width: 300px;
height: 300px;
background: orange;
display: flex;
justify-content: center;
align-items: center;
}
.child {
width: 100px;
height: 100px;
display: inline-block;
}
</style>
<style>
.box {
width: 300px;
height: 300px;
background: orange;
position: relative;
}
.child {
position: absolute;
display: block;
width: 100px;
height: 100px;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
思路:
.box {
width: 300px;
height: 300px;
background: orange;
overflow: hidden;
}
.child {
display: block;
width: 100px;
height: 100px;
margin: 0 auto;
margin-top: 50%;
transform: translateY(-50%);
}
思路:
原文:https://www.cnblogs.com/shenggao/p/12381318.html