Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML table cell spacing - only between cells, no outer one

Tags:

html

css

I am trying to add cell spacing to a html table.

I want to add spacing between cells without the outer spacing.

My problem is, that the cellspacing html attribute and border-spacing CSS property adds spacing outside too.

cellspacing image

I would like to put cell spacing without the red (outer) part - only the yellow one.

Is it possible?

Edit:

  1. The image was drawn by hand (MS-Paint) only for illustration.
  2. The coloring is for debugging - so that one can see where the borders, and spacing is.

I have found a roundabout solution including some additional div-s:

.inner-spacing {
  border-collapse: collapse;
  background-color: yellow;
  border: 2px solid black;
}
.inner-spacing td {
  padding: 0;
}
.inner-spacing td > div {
  width: 60px;
  height: 60px;
  background-color: green;
  border: 2px solid black;
  margin: 10px;
}
.inner-spacing tr:first-child > td > div {
  margin-top: 0px;
}
.inner-spacing tr:last-child > td > div {
  margin-bottom: 0px;
}
.inner-spacing tr > td:first-child > div {
  margin-left: 0px;
}
.inner-spacing tr > td:last-child > div {
  margin-right: 0px;
}
<table class="inner-spacing">
  <tr>
    <td>
      <div/>
    </td>
    <td>
      <div/>
    </td>
  </tr>
  <tr>
    <td>
      <div/>
    </td>
    <td>
      <div/>
    </td>
  </tr>
</table>

So to summarize, I would like the table to have border spacing with the table border collapsing onto the cells (no spacing).

I wonder if there are some other solutions - so any new solution is welcome!

like image 233
MrMeszaros Avatar asked Jul 16 '15 13:07

MrMeszaros


People also ask

How do you control the space between table and cell borders?

The space between the table cells is controlled by the CELLSPACING attribute in the TABLE tag. By setting CELLSPACING to zero, you can remove all the space between the cells of your table.

How do you put space between cells in HTML table?

The HTML <table> cellspacing Attribute is used to specify the space between the cells. The cellspacing attribute is set in terms of pixels. Attribute Values: pixels: It sets the space between the cells in terms of pixels.

Which property is used on tables to set that there should be no spacing between cells?

The border-spacing CSS property sets the distance between the borders of adjacent cells in a <table> .

How can we give a space between the cell contents and its borders?

Cell padding specifies the space between the cell content and its borders.


1 Answers

This will be tricky a little bit...you will need to set display:block and border-spacing:10px for spacing between cells and same negative margin:-10px to remove the outer spacing

Stack Snippet

table {
  font: bold 13px Verdana;
  background: black;
  margin: 30px auto;
  border-spacing: 0;
}

table td {
  padding: 30px;
  background: red;
  color: #fff;
}

table tbody {
  margin: -10px;
  display: block;
  border-spacing: 10px;
}
<table>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
    <td>3</td>
    <td>4</td>
  </tr>
</table>
like image 133
Bhuwan Avatar answered Sep 28 '22 10:09

Bhuwan