表格行旁边的浮动元素

时间:2017-03-14 11:20:28

标签: html css css3 pseudo-element

当我将鼠标悬停在表格行旁边时,我正试图显示两个图标,这将是典型的编辑/删除等按钮。

我已经尝试添加一个带有图标的额外列,并将其定位为相对和绝对,但它会搞乱后面第一列的样式

<td class="actions">
    <i *ngIf="editing" (click)="saveEdit()" class="fa fa-floppy-o" aria-hidden="true"></i>
    <i *ngIf="editing" (click)="cancelEdit()" class="fa fa-times" aria-hidden="true"></i>
</td>

.actions {
     position: relative;
     right: 5%;
     padding: 0;
     border: 0;
     vertical-align: middle;
}

这是我在悬停在一行时想要做的事情 Mockup

$background: #22304b;
$textColor: #58595b;
$lightBlue: #00b9cd;
$lightBlueDark: #0193a2;
$borderGrey: rgb(219, 219, 219);
$backgroundGrey: rgb(246, 246, 249);
table {
  border-radius: 4px;
  border: 1px solid $borderGrey;
  margin-bottom: 0;
  
  .header {
    background: $backgroundGrey;
    color: $background;
    padding: 25px 15px 15px;
    border-top: 1px solid $borderGrey;
    border-bottom: 0;
    text-align: center;
    font-size: 20px;
  }
  .header:last-of-type {
    border-right: 1px solid $borderGrey;
  }
  tr.row {
    transition: background 0.3s ease;
    padding: 15px;
    .column {
      border-right: 1px solid $borderGrey;
      border-top: 0;
      vertical-align: middle;
      font-size: 20px;
    }
    .column.checkbox,
    .column.centered {
      text-align: center;
    }
    .column:last-child {
      border-right: 0;
    }
  }
  tr.row:nth-child(even) {
    background: $backgroundGrey;
  }
  tr.row:hover {
    background: rgba($borderGrey, 0.7);
  }
}

#wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
<div id="wrapper">
  <table class="table">
    <thead>
      <tr>
        <th class="header">Company</th>
        <th class="header">Role</th>
        <th class="header rate">Start Date</th>
        <th class="header">End Date</th>
        <th class="header rate">Pay Rate</th>
        <th class="header rate">Charge Rate</th>
        <th class="header rate">Holiday Rate</th>
        <th class="header rate">Cost Code</th>
        <th class="header">Supervisor</th>
        <th class="header">Supervisor Email</th>
      </tr>
    </thead>
    <tbody>
      <tr class="row">
        <td class="column centered"> name </td>
        <td class="column centered">title</td>
        <td class="column centered">date</td>
        <td class="column centered">date2</td>
        <td class="column centered">10€</td>
        <td class="column centered">10€</td>
        <td class="column centered">10%</td>
        <td class="column centered">blabla</td>
        <td class="column centered">approver</td>
        <td class="column centered">email@email.com</td>
      </tr>
      <tr class="row">
        <td class="column centered"> name </td>
        <td class="column centered">title</td>
        <td class="column centered">date</td>
        <td class="column centered">date2</td>
        <td class="column centered">10€</td>
        <td class="column centered">10€</td>
        <td class="column centered">10%</td>
        <td class="column centered">blabla</td>
        <td class="column centered">approver</td>
        <td class="column centered">email@email.com</td>
      </tr>
      <tr class="row">
        <td class="column centered"> name </td>
        <td class="column centered">title</td>
        <td class="column centered">date</td>
        <td class="column centered">date2</td>
        <td class="column centered">10€</td>
        <td class="column centered">10€</td>
        <td class="column centered">10%</td>
        <td class="column centered">blabla</td>
        <td class="column centered">approver</td>
        <td class="column centered">email@email.com</td>
      </tr>
    </tbody>
  </table>
</div>

This is what I currently have (Codepen)

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以添加一个额外的,绝对定位的元素,其中里面的图标每个td(我给了一个名为.editicons的类)并使用以下CSS。这使得它在默认情况下不可见,并且在悬停行时可见(行的左侧)。

td:first-child {
    position: relative;
}
.editicons {
  position: absolute;
  display: none;
  color: red;
  width: 60px;
  left: -50px;
}
tr:hover .editicons {
  display: block;
}

http://codepen.io/anon/pen/peryea