admin管理员组

文章数量:1399912

When I try to combine border-separate (I need it for border-spacing-y-2) with divide-y, then divide-y is ignored until I remove border-separate. Why is that and what is the best practice to get vertical spacing between table rows and horizontal dividers?

Playground:

Once I remove border-separate it looks like this: The horizontal divider is now visible, but the vertical spacing is gone. How to get both?

When I try to combine border-separate (I need it for border-spacing-y-2) with divide-y, then divide-y is ignored until I remove border-separate. Why is that and what is the best practice to get vertical spacing between table rows and horizontal dividers?

Playground: https://play.tailwindcss/ZwI9Szxm23

Once I remove border-separate it looks like this: The horizontal divider is now visible, but the vertical spacing is gone. How to get both?

Share Improve this question asked Mar 24 at 21:05 me.at.codingme.at.coding 18k48 gold badges182 silver badges343 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

I would say that border-spacing is not the ideal property to use to space out table rows vertically. Instead, you could consider applying vertical padding to the table cells and adding borders to the table rows:

<script src="https://unpkg/@tailwindcss/[email protected]"></script>

<table class="w-full text-left">
  <thead>
    <tr>
      <th class="py-2">Name</th>
      <th class="py-2">Age</th>
      <th class="py-2">City</th>
    </tr>
  </thead>
  <tbody>
    <tr class="border-t">
      <td class="py-2">John Doe</td>
      <td class="py-2">28</td>
      <td class="py-2">New York</td>
    </tr>
    <tr class="border-t">
      <td class="py-2">Jane Smith</td>
      <td class="py-2">34</td>
      <td class="py-2">Los Angeles</td>
    </tr>
    <tr class="border-t">
      <td class="py-2">Sam Wilson</td>
      <td class="py-2">22</td>
      <td class="py-2">Chicago</td>
    </tr>
  </tbody>
</table>

The reason the borders don't show because in the separated borders model, all borders on row groups and rows are ignored. So to get the effect you want with separated borders, the border settings must be inherited by the table cells. This can be done with a little arbitrary variants magic.

<script src="https://unpkg/@tailwindcss/[email protected]"></script>
<table class="w-full border-separate border-spacing-y-2 
              divide-y-2 [&_thead_tr,&_th]:[border:inherit] text-left">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>City</th>
    </tr>
  </thead>
  <tbody class="divide-y [&_td]:[border:inherit]">
    <tr>
      <td>John Doe</td>
      <td>28</td>
      <td>New York</td>
    </tr>
    <tr>
      <td>Jane Smith</td>
      <td>34</td>
      <td>Los Angeles</td>
    </tr>
    <tr>
      <td>Sam Wilson</td>
      <td>22</td>
      <td>Chicago</td>
    </tr>
  </tbody>
</table>

https://play.tailwindcss/JPqtsvnl7J

本文标签: cssHow to combine borderseparate with divideyStack Overflow