Set border to table header

I am working on a table that can have no headers, the first row as a header, the first column as a header, or both the first column and the first row as headers.

The expected output is that the headers should have a border on the right, except when the first row is set as the header

Expected output

But the current output is

The issue is that I am getting a border-right even when the first row is set as the header. Is there a solution to avoid this?

table {
  border: 1px solid #D2D2D2;
  border-collapse: collapse;
  th {
    padding: 16px;
    border-right: 1px solid #D2D2D2;
    background: #F1F1F1
  }
  td {
    padding: 16px;
  }
  tr {
    border-bottom: 1px solid #D2D2D2;
  }
  tr:first-child th:not(:first-child) {
    border-right: 0;
  }
}
<p> Table with first row as header</p>
<table>
  <tbody>
    <tr>
      <th>Fruits</th>
      <th>Veggies</th>
      <th>Junks</th>
    </tr>
    <tr>
      <td>Apple</td>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <td>Orange</td>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>
<p> Table with first column as header</p>
<table>
  <tbody>
    <tr>
      <th>Fruits</th>
      <td>Veggies</td>
      <td>Junks</td>
    </tr>
    <tr>
      <th>Apple</th>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <th>Orange</th>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>
<p> Table with first row and column as header</p>
<table>
  <tbody>
    <tr>
      <th>Fruits</th>
      <th>Veggies</th>
      <th>Junks</th>
    </tr>
    <tr>
      <th>Apple</th>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <th>Orange</th>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>
<p>Table with no header</p>
<table>
  <tbody>
    <tr>
      <td>Fruits</td>
      <td>Veggies</td>
      <td>Junks</td>
    </tr>
    <tr>
      <td>Apple</td>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <td>Orange</td>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>

codepen

NOTE : I don’t have access to add/modify the HTML or add JS. Also the order of the tables here are just for example, it changes based on the requirement

1

You can fix this using CSS.

I have added &:nth-of-type(1) tr:first-child th { border-right: 0; }

nth-of-type(1) – Select only first Table
tr:first-child – Select first header row of first Table

For more info you can check MDN documentation
Link – https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type

Updated Code is below.

table{
      border: 1px solid #D2D2D2;
      border-collapse: collapse;
      th{
         padding: 16px;
         border-right: 1px solid #D2D2D2;
         background: #F1F1F1 
      }
      td{
         padding: 16px;
      }

      tr{
         border-bottom: 1px solid #D2D2D2;
      }
      tr:first-child th:not(:first-child){
         border-right: 0;
      }
      &:nth-of-type(1) tr:first-child th {
         border-right: 0;
      }
   }
<p> Table with first row as header<p>
<table>
  <tbody>
    <tr>
      <th>Fruits</th>
      <th>Veggies</th>
      <th>Junks</th>
    </tr>
    <tr>
      <td>Apple</td>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <td>Orange</td>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>
<p> Table with first column as header<p>
<table>
  <tbody>
    <tr>
      <th>Fruits</th>
      <td>Veggies</td>
      <td>Junks</td>
    </tr>
    <tr>
      <th>Apple</th>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <th>Orange</th>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>
<p> Table with first row and column as header<p>
<table>
  <tbody>
    <tr>
      <th>Fruits</th>
      <th>Veggies</th>
      <th>Junks</th>
    </tr>
    <tr>
      <th>Apple</th>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <th>Orange</th>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>
<p> Table with no header<p>
<table>
  <tbody>
    <tr>
      <td>Fruits</td>
      <td>Veggies</td>
      <td>Junks</td>
    </tr>
    <tr>
      <td>Apple</td>
      <td>Carrot</td>
      <td>Burger</td>
    </tr>
    <tr>
      <td>Orange</td>
      <td>Betroot</td>
      <td>Fries</td>
    </tr>
  </tbody>
</table>

4

The border-right is removed for all headers in the first row except the first column (using :not(:first-child)).
A specific rule ensures that the first column headers (th:first-child) always retain their border-right style, even when it’s part of a table that also has the first row as a header

<style>
  table {
    border: 1px solid #D2D2D2;
    border-collapse: collapse;
    width: 100%;
  }
  th, td {
    padding: 16px;
    border-right: 1px solid #D2D2D2;
    border-bottom: 1px solid #D2D2D2;
  }
  th {
    background: #F1F1F1;
  }
  /* Disable right border for headers in the first row */
  tr:first-child th:not(:first-child) {
    border-right: 0;
  }
  /* Always add border-right for first column headers */
  tr th:first-child {
    border-right: 1px solid #D2D2D2;
  }
  /* Set borders for table rows except headers */
  tr:last-child td {
    border-bottom: 0;
  }
</style>

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật