While margin: 0 auto; can center a div horizontally, margin: auto auto; does not align it vertically as intended. Additionally, vertical-align: middle; is ineffective for block-level elements.
One viable workaround involves nesting three elements:
.container { display: table; height: 100%; position: absolute; overflow: hidden; width: 100%; } .helper { position: absolute; top: 50%; display: table-cell; vertical-align: middle; } .content { position: relative; top: -50%; margin: 0 auto; width: 200px; border: 1px solid orange; }
<div class="container"> <div class="helper"> <div class="content"> <p>stuff</p> </div> </div> </div>
In this solution:
The above is the detailed content of Why Doesn't `margin: auto auto;` Vertically Center a Div?. For more information, please follow other related articles on the PHP Chinese website!