如何为表格标题添加底盒阴影

时间:2020-07-14 01:02:56

标签: html css

我试图为表格标题添加一个底盒阴影,但是由于某种原因,阴影也出现在侧面。下面是我正在尝试的代码。

#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td,
#customers th {
  padding: 8px;
}

#customers th {
  box-shadow: 0 1px 2px 0px grey;
}
<table id="customers">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbköp</td>
    <td>Christina Berglund</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
</table>

我正在寻找带有blur:2pxspread:0pxy-offset:1pxx-offset:0px

的盒子阴影

3 个答案:

答案 0 :(得分:2)

猜猜这可能就是您要寻找的内容,我在box-shadow上进行了一些编辑,并且将表格数据td和表格头th居中对齐,以使其外观看起来很好

#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td,
#customers th {
  padding: 8px;
  /* added */
  text-align: center;
}

#customers th {
  /* edited */
  box-shadow: 0px 2px 0px rgba(128,128,128, .15);
}
<table id="customers">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbköp</td>
    <td>Christina Berglund</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
</table>

答案 1 :(得分:0)

您需要在左侧阴影中添加负值,以便将阴影移至左侧,从而将其全部隐藏在原始元素的后面。

编辑:给tr加上阴影,以消除阴影之间的间隔问题。

#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td,
#customers th {
  padding: 8px;
}

.headingtr {
  box-shadow: 0 2px 2px -2px grey;
}
<table id="customers">
  <tr class="headingtr">
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbköp</td>
    <td>Christina Berglund</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
</table>

答案 2 :(得分:0)

您可以添加剪切路径来剪切不需要的阴影:

#customers {
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

#customers td,
#customers th {
  padding: 8px;
}

#customers th {
  box-shadow: 0 1px 4px 0px grey;
  clip-path:inset(0 0 -10px 0); /* 10px or any bigger value than the blur */
}
<table id="customers">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Maria Anders</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbköp</td>
    <td>Christina Berglund</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
</table>