This question arose due to a fix proposed to this related question
I have 3 nested divs: outer, inner and element.
<div class="outer"> <div class="inner"><div class="item"></div></div> </div>
Given the following basic CSS:
.outer{ width:50%; height:100px; border: 1px solid red; position:relative; overflow-x:hidden; box-sizing:border-box; } .inner{ border:1px solid blue; height:100%; position: absolute; box-sizing: border-box; line-height:98px; } .item{ width:100px; height:94px; background-color:yellow; display: inline-block; border:1px solid green; box-sizing:border-box; vertical-align:middle; }
The challenge is to center the div βelementβ vertically with equal (or absent) gaps above / below, and vertical scroll bars do not appear.
Codepen is here
Update:
As indicated below, I must add that several elements of different heights should be centered. At the moment, the best answer is to add a negative field to each element, which leads to the following: http://codepen.io/anon/pen/dYWEYZ
However, it smells bad (to me), because it requires an offset, which depends on 3 other properties.
source share