我试图重新创建我在Photoshop样机中制作的材料设计表:

html - 在CSS中为tbody添加box-shadow?-LMLPHP

除了投影,我已经拥有了所有想要的东西。当我在tbody元素上添加阴影时,它在Firefox中显示,但在Chrome或Edge中不显示。如果我对它应用“ display:block”样式,它也会显示。但是,这删除了默认的“ display:table-row-group”样式,有效地破坏了表的结构。如何在不影响其余布局的情况下添加阴影?



* {
  font-family: Roboto, Arial;
}

body {
  background: #f8f8f8;
}

table {
  border-collapse: collapse;
  width: 100%;
}

th {
  font-weight: normal;
  opacity: .7;
  text-align: left;
  padding-left: 20px;
  padding-bottom: 10px;
}

td {
  padding: 20px;
}

tbody tr:nth-child(even) {
  background: rgba(128,128,128,.05);
}

tbody {
  background: #fff;

  /* This doesn't work unless "display: block" is applied, which ruins the structure */
  box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
  -moz-box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
  -webkit-box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
}

.grey {
  opacity: .5;
}

.green {
  color: #4caf50;
}

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Date</th>
      <th>Grade</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>You</td>
      <td>Today, 4:12 PM</td>
      <td class="green">92%</td>
    </tr>
    <tr>
      <td>John Smith</td>
      <td>Today, 3:28 PM</td>
      <td class="grey">Hidden</td>
    </tr>
    <tr>
      <td>Bobby Newport</td>
      <td>Yesterday</td>
      <td class="grey">Hidden</td>
    </tr>
    <tr>
      <td>Jim Halpert</td>
      <td>2 days ago</td>
      <td class="grey">Hidden</td>
    </tr>
    <tr>
      <td>Detlow Coffin</td>
      <td>2 days ago</td>
      <td class="grey">Hidden</td>
    </tr>
  </tbody>
</table>

最佳答案

我花了几个小时解决这个问题,终于有了一个不会破坏表布局的可行解决方案。但是现在我想起来,flexbox可能会更简单...

table {
  position: relative;
}

table:after {
  content: '';
  position: absolute;
  box-shadow: 0 2px 4px 0 rgba(37, 37, 37, 0.2); // your box-shadow
  left: 0;
  right: 0;
  top: 36px; // height of your table header
  bottom: 0;
  z-index: -1;
}


这是小提琴:https://jsfiddle.net/jitowix/o2gx7sc4/

07-24 09:47
查看更多