css box-shadow untuk baris tabel?

Oct 14 2020

Ini kode HTML saya:

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

Tujuannya adalah untuk membuat bayangan di sekitar baris pertama. Masalahnya adalah warna latar belakang sel 4 mengaburkannya.

Pertanyaan saya adalah: bagaimana cara menampilkan drop show di atas latar belakang sel 4?

edit: lihat pertanyaan tindak lanjut: css inset box-shadow untuk baris tabel?

Jawaban

3 TemaniAfif Oct 14 2020 at 02:43

Turunkan indeks-z sel berikutnya:

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