【问题标题】:position: absolute and vertical-align: middle位置:绝对和垂直对齐:中间
【发布时间】:2017-03-23 04:41:04
【问题描述】:

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

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

即使使用顶部定位也不适用于.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;
}

【问题讨论】:

    标签: css css-position vertical-alignment


    【解决方案1】:

    是的,您需要从 .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/

    【讨论】:

    • 谢谢!太完美了!
    【解决方案2】:

    display:table 添加到类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>

    【讨论】:

      【解决方案3】:

      删除位置并使您的数据 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>

      【讨论】:

        猜你喜欢
        • 2013-06-01
        • 1970-01-01
        • 2017-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-25
        • 1970-01-01
        • 2014-10-19
        相关资源
        最近更新 更多