位置:绝对和垂直对齐:中间

position: absolute and vertical-align: middle

如何将示例中的第三列设置为 vertical-align: middle 与所有其他行一样?我意识到问题出在 position: absolute,但我不知道如何解决这个问题。

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

即使使用 top 定位也不适用于 .data

我的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>

我的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;
}

display:table添加到class data

.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:table;
}

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

table {
  font-size: 12px;
}
<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>

是的,您需要从 .data 中删除 position: absolute,并绝对定位 .bar-chart-bar 并相应地设置 z-index :

.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/

删除位置并使您的数据 div 成为小柱 div 的子项。

.bar-chart-bar {
  background-color: #e8e8e8;
  display: block;
  width: 100%;
}

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

.data {
  z-index: 1;
}

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

table {
  font-size: 12px;
}
<table border="1">
  <tbody>
    <tr>
      <td>ADMIN</td>
      <td>222387</td>
      <td width='50' style='position:relative'>
        <div class="bar-chart-bar">
            <div class="bar" style='width:50%; background-color:#B8E4F5'>
                <div class='data'>59853.94</div>
            </div>
        </div>
      </td>
      <td width="50">0</td>
      <td>59853.94</td>
      <td>4189.82</td>
      <td>7</td>
    </tr>
  </tbody>
</table>