I'm writing a template for a flashcard program called Anki which uses QtWebkit to render the cards.
I would like to center a group of three elements vertically on the center of the middle element. The elements are of dynamic size and should grow according to their contents but still align on the left.
I tried two approaches thus far, the one is to center the middle element and then absolutely position the top and bottom elements within that middle element. Unfortunately this means that the overall group of elements will not grow horizontally depending on each of the element but instead only depending on the content of the middle one. Also the overall size only reaches 50% of the height and width of the page.
CSS:
.container {
position: absolute;
top: 0;
bottom: 0;
height: 100%;
}
.left {
background-color: green;
left: 0;
right: calc(100% / 3 * 2);
}
.left .middle {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.left .top {
position: absolute;
bottom: 100%;
left: 0;
}
.left .bottom {
position: absolute;
top: 100%;
left: 0;
}
HTML:
<div class="container left">
<div class="middle">
<div class="top"> ödifj asduflsdfsk<br>j dkfj aölkdsjf df sdf sd f</div>
dgs lk lj lkj lku öoiu ökj df sadlfjslk dsfs df s
<div class="bottom">dfffd gfds gdsf f</div>
</div>
</div>
The second option I tried was a flexbox with justify-content: center; but this means that the middle element isn't centered but only the whole group of elements.
CSS:
.container {
position: absolute;
top: 0;
bottom: 0;
height: 100%;
}
.center {
background-color: blue;
left: calc(100% / 3);
right: calc(100% / 3);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
HTML:
<div class="container center">
<div class="top"> ödifj asduflsdfsk<br>j dkfj aölkdsjf df sdf sd f</div>
<div class="middle">
dgs lk lj lkj lku öoiu ökj df sadlfjslk dsfs df s
</div>
<div class="bottom">dfffd gfds gdsf f</div>
</div>
So again: 3 elements, as a group horizontally centered but aligned on the left side of the elements. Vertically centered relative to the middle element. And no fixed widths or heights. The overall group should be as wide as the widest of the elements.
Why not try and use an html table? They've great for vertical centering with minimal css
http://jsfiddle.net/6yody1fn/
will need a lot of tinkering to get it to look nice but it might be an option