Search code examples
csscss-positionvertical-alignment

position: absolute and vertical-align: middle


How to set the third column in my example to have vertical-align: middle as all the other rows? I realize that the problem is with position: absolute, but I can't figure out how to solve the issue.

https://jsfiddle.net/pr1v6Lhd/1/

Even positioning with top does not work for .data.

My HTML:

<table border="1">
  <tbody>
    <tr>
      <td>ADMIN</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>
        </div>
      </td>
      <td width="50">0</td>
      <td>59853.94</td>
      <td>4189.82</td>
      <td>7</td>
    </tr>
  </tbody>
</table>

My CSS:

.bar-chart-bar {
  background-color: #e8e8e8;
  display: block;
  position: relative;
  width: 100%;
  height: 20px;
}

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

.data {
  position: absolute;
  z-index: 1;
  display: block;
  top: 10;
}

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

table {
  font-size: 12px;
}

Solution

  • Yes, you need to remove position: absolute from .data, and absolute position the .bar-chart-bar and set the z-index accordingly :

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

    https://jsfiddle.net/jamesking/pr1v6Lhd/2/