Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate gap between tbody tags

Tags:

html

css

Is there a way to eliminate the slight gap between two tbody tags when they are both displayed inline like this?

http://jsfiddle.net/kttss/

what the html renders

As you can see in the fiddle, in between the two tables there is a slight gap. I know I can get rid of this manually by using negative margin, but this seems like a hassle since I have a table with a variable number of tbodys.

<table style="margin:0;" border="1">
   <tbody  style="display: inline-block;  margin:0;">
      <tr>
         <td>
            1
         </td>
         <td>
            2
         </td>
      </tr>
      <tr>
         <td>
            3
         </td>
         <td>
            4
         </td>
      </tr>
   </tbody>
   <tbody  style="display: inline-block; margin: 0;">
      <tr>
         <td>
            1
         </td>
         <td>
            2
         </td>
      </tr>
      <tr>
         <td>
            3
         </td>
         <td>
            4
         </td>
      </tr>
   </tbody>
</table>
like image 231
user886596 Avatar asked Jul 03 '13 21:07

user886596


People also ask

How can I remove space between TR and TD in HTML?

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 give space between TH and TD?

Use the <tr> tag for each row. For the first row, use the <th> tag which defines a header cell in an HTML table. For the other rows, use the <td> tag which defines a standard data cell in an HTML table. For the cells belonging to the first row set a "td" class.

Can a table have multiple tbody tags?

You may use more than one <tbody> per table as long as they are all consecutive.


2 Answers

Looks like adding border-spacing: 0; to your table elements will help. Without this, there's 2 pixels surrounding each of the borders, which means there's 4 pixels between the tables.

like image 60
Joe Enos Avatar answered Sep 27 '22 18:09

Joe Enos


Use float instead of inline-block display. You also have to collapse the borders with border-collapse:collapse, or use border-spacing: 0 as in @JoeEnos's answer, as well.

table { border-collapse:collapse; }
tbody { float:left; }

Demo

With display: inline-block, the white-space in the markup (line-breaks, tabs) are collapsed and rendered as a single space. floats are not affected by this.

like image 27
Fabrício Matté Avatar answered Sep 27 '22 17:09

Fabrício Matté