stee1rat stee1rat - 26 days ago 12
CSS Question

vertical-align:middle a div inside a floating div?

Why doesn't the black stripe in column three vertical-align in the middle of the parent div? Instead in aligns a little below the top. Is there a way to fix it?

https://jsfiddle.net/pr1v6Lhd/4/

HTML:

<table border="1">
<tbody>
<tr>
<td>ADMIN<br>:(</td>
<td>222387</td>
<td width='50' style='position:relative'>
<div class='data'>59853.94</div>
<div class="bar-chart-bar">
<div class="bar" style='width:50%; background-color:#B8E4F5'>
<div style="height:10px; background-color:black; width:100%;vertical-align:middle; display: inline-block"></div>
</div>
</div>
</td>
<td width="50">0</td>
<td>59853.94</td>
<td>4189.82</td>
<td>7</td>
</tr>
</tbody>
</table>


CSS:

.bar-chart-bar {
background-color: #e8e8e8;
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -1;
}

.bar {
float: left;
height: 100%;
}

.data {

}

.table > tbody > tr > td {
vertical-align: middle;
}

table {
font-size: 12px;
}

Answer

To your .bar class, you can add

display:flex;
flex-direction:column;
justify-content:center;

as well as remove

vertical-align: middle;
display: inline-block;

from your .data class. Here is the result. The reason this wasn't working for you is that vertical-align doesn't really behave the way you want it to unless your element is in a table cell. See this for more about vertical align.

Comments