Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"border-collapse: collapse" removes padding from table

Tags:

html

css

I have two questions:

  1. is there a logical explanation, Why is this working like that?
  2. Is there a CSS workaround for that situation?

Thanks!

table {
      border-collapse: collapse;
      padding:30px; /* Padding not working */
    }
<table>
    <tr>
      <td>cell</td>
      <td>cell</td>
    </tr>
    <tr>
      <td>cell</td>
      <td>cell</td>
    </tr>
</table>
like image 480
m.popov Avatar asked Nov 22 '17 15:11

m.popov


1 Answers

is there a logical explanation, Why is this working like that?

When you collapse the borders, it joins adjacent borders together.

The border around the outside edge of the table is adjacent to the outside borders of the outside cells.

If those borders are joined together then you can't put padding between them. There is no "between".

Is there a CSS workaround for that situation?

Not, in general, a plain one.

You need to add another element around the table, and place the padding there.

In this case, you already have the container div, so you can move the padding to that.

.container {
  width: 400px;
  height: 300px;
  margin: 40px auto;
  background: #ddd;
  padding: 30px;
}

table {
  text-align: center;
  width: 100%;
  height: 100%;
  border-collapse: collapse;
}

td {
  border: 1px solid red;
}
<div class="container">
  <table>
    <tr>
      <td>cell</td>
      <td>cell</td>
    </tr>
    <tr>
      <td>cell</td>
      <td>cell</td>
    </tr>
  </table>
</div>
like image 62
Quentin Avatar answered Sep 24 '22 13:09

Quentin