如何为表格行添加CSS Box Shadow效果?

3

以下是我的HTML代码:

<style>
.yellow{
    background-color: yellow;
}
td{
    width:40px;
}
tr:first-child {
    box-shadow: 1px 1px 5px 5px #d99292;
}
</style>
<table>
    <tr>
        <td class=yellow>1</td>
        <td>2</td>
    </tr>
    <tr>
        <td>3</td>
        <td class=yellow>4</td>
    </tr>
</table>

目标是在第一行周围有一个投影阴影。问题是,单元格4的背景颜色遮盖了它。
我的问题是:如何使投影阴影显示在单元格4的背景上方?
编辑:参见后续问题:如何为表格行设置 CSS 内部投影阴影?
1个回答

3
降低下一个单元格的 z-index 值:

.yellow {
  background-color: yellow;
}

td {
  width: 40px;
}

tr:first-child {
  box-shadow: 1px 1px 5px 5px #d99292;
}

tr:first-child  + tr td {
  position: relative;
  z-index: -1;
}

/* the below is not mandatory but to make sure 
the cells doesn't go below the table */
table {
  position:relative;
  z-index:0;
}
<table>
  <tr>
    <td class=yellow>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
    <td class=yellow>4</td>
  </tr>
</table>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接