skip to Main Content

Im having trouble making a valid HTML table with vertical spacing between rows and shadow below each row.

The shadow always goes on top of other table data.

I have positioned the elements and set a z-index.

enter image description here

  table {
    border-collapse: separate;
    border-spacing: 0;
  }
  td,
  th {
    min-width: 170px;
  }

  .shadow {
    position: relative;
    z-index: 1;
    margin: 2px 0 2px 0;
  }

  .shadow:before {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: -1;
    box-shadow: 0 0 10px 10px #000;
  }
<main>
  <table>
    <thead>
      <tr>
        <td>head</td>
        <td>head</td>
        <td>head</td>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><div class="shadow">div</div></td>
        <td><div class="shadow">div</div></td>
        <td><div class="shadow">div</div></td>
      </tr>
      <tr>
        <td><div class="shadow">div</div></td>
        <td><div class="shadow">div</div></td>
        <td><div class="shadow">div</div></td>
      </tr>
      <tr>
        <td><div class="shadow">div</div></td>
        <td><div class="shadow">div</div></td>
        <td><div class="shadow">div</div></td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <td>foot</td>
        <td>foot</td>
        <td>foot</td>
      </tr>
    </tfoot>
  </table>
</main>

2

Answers


  1. One approach is as below, with explanatory comments in the code itself:

    /* removing all default padding and margins, and ensuring
       that all elements are sized to include their padding
       and border-widths: */
    *,
    ::before,
    ::after {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    
    table {
      /* CSS custom properties for various properties to ensure
         common styling where appropriate;
          --tr-space-between is the size of the percieved/visible
         gap between adjacent rows: */
      --tr-space-between: 0.5rem;
      /* --td-padding-block is derived from the previous variable,
         using calc() so that the space between 'rows' is inserted
         as padding (padding-block) to create that row-spacing: */
      --td-padding-block: calc(var(--tr-space-between)/2);
      --shadow-color: lightgray;
      --row-color: #fff;
      /* the desired radius of the 'rows': */
      --row-radius: 0.5rem;
      /* to ensure page background can be seen (if required)
         in the row-gaps: */
      background-color: transparent;
      /* collapsing the borders between cells in order to allow the
         content to be contiguous, and using a different means to
         achieve row-"separation": */
      border-collapse: collapse;
      border-spacing: 0;
      /* using a CSS filter, drop-shadow(), to create the shadows: */
      filter: drop-shadow(0 0 0.5rem var(--shadow-color));
      /* centering the <table> */
      margin-inline: auto;
    }
    
    td,
    th {
      min-width: 170px;
    }
    
    td,
    th {
      /* again to ensure that the page background is - where
         appropriate - visible through the visual gaps: */
      background-color: transparent;
    }
    
    td {
      /* setting the cell padding on the block axis, to "separate"
         the "rows", while no padding is applied on the inline
         axis, so that the rows are visually contiguous: */
      padding-block: var(--td-padding-block);
    }
    
    /* using logical properties to set the border radii: */
    td:first-child .content {
      border-start-start-radius: var(--row-radius);
      border-end-start-radius: var(--row-radius);
    }
    
    td:last-child .content {
      border-start-end-radius: var(--row-radius);
      border-end-end-radius: var(--row-radius);
    }
    
    .content {
      /* setting the background-color of the "row": */
      background-color: var(--row-color);
      /* applying padding on all axes, to move the content
         away from the edges of the 'row': */
      padding: 0.5rem;
    }
    <main>
      <table>
        <thead>
          <tr>
            <th>head</th>
            <th>head</th>
            <th>head</th>
          </tr>
        </thead>
        <tbody>
          <!-- I've changed the class-name of the <div> from 'shadow' to
               'content' to reflect what the "purpose" of the element: -->
          <tr>
            <td>
              <div class="content">div</div>
            </td>
            <td>
              <div class="content">div</div>
            </td>
            <td>
              <div class="content">div</div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="content">div</div>
            </td>
            <td>
              <div class="content">div</div>
            </td>
            <td>
              <div class="content">div</div>
            </td>
          </tr>
          <tr>
            <td>
              <div class="content">div</div>
            </td>
            <td>
              <div class="content">div</div>
            </td>
            <td>
              <div class="content">div</div>
            </td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <td>foot</td>
            <td>foot</td>
            <td>foot</td>
          </tr>
        </tfoot>
      </table>
    </main>
    Login or Signup to reply.
  2. You can do this like.

    table {
        border-collapse: collapse;
        border-spacing: 0;
      }
      
      .shadow {
        position: relative;
        z-index: 1;
        margin: 2px 0 2px 0;
      }
    
      .shadow:before {
        content: "";
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: -1;
        box-shadow: 0 0 2px 2px #000; 
        }
        
    
      
      tr:hover {
          box-shadow: 0 5px 8px 0 rgba(50, 50, 50, 0.35);
          -webkit-box-shadow: 0 5px 8px 0 rgba(50, 50, 50, 0.35);
          -moz-box-shadow: 0 5px 8px 0 rgba(50, 50, 50, 0.35);
    }
    
    
    td, th {
     text-align:center;
      min-width: 170px;
      border: 1px solid #999;
      padding: 0.5rem;
    }
    <main>
      <table>
        <thead>
          <tr>
            <td>head</td>
            <td>head</td>
            <td>head</td>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td><div class="shadow">1</div></td>
            <td><div class="shadow">2</div></td>
            <td><div class="shadow">3</div></td>
          </tr>
          <tr>
            <td><div class="shadow">4</div></td>
            <td><div class="shadow">5</div></td>
            <td><div class="shadow">6</div></td>
          </tr>
          <tr>
            <td><div class="shadow">7</div></td>
            <td><div class="shadow">8</div></td>
            <td><div class="shadow">9</div></td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <td>foot</td>
            <td>foot</td>
            <td>foot</td>
          </tr>
        </tfoot>
      </table>
    </main>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search