我知道在SO上有多种解决方案可用于水平和垂直对中divs。我问这个问题是为了更好地了解CSS内部的工作原理。
此代码将子div水平居中放置在容器div中。
.container {
height: 200px;
width: 200px;
background: blue;
}
.child {
margin: 0 auto;
width: 100px;
background: red;
color: white;
}<div class="container"> <div class="child">LinkedIn</div> </div>
我的问题是,为什么下面的方法不适用于垂直对齐?
.container {
height: 200px;
width: 200px;
background: blue;
}
.child {
margin: auto 0;
height: 100px;
background: red;
color: white;
}<div class="container"> <div class="child">LinkedIn</div> </div>