Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS table border spacing inside only

Tags:

css

html-table

I have trying to work this out for months, and Google hasn't helped me. I'm trying to have spacing between <td> and <th> tags in a table, but when I do, it does spacing in the outside. Therefore, the table isn't inline with anything else. So it looks like the table has some padding.

I can't seem to find a solution.

Here is an example of the issue

like image 674
user1112081 Avatar asked Jan 10 '12 15:01

user1112081


People also ask

How do I add internal lines to a table border in CSS?

CSS Table Borders. When you use CSS to add borders to tables, it only adds the border around the outside of the table. If you want to add internal lines to the individual cells of that table, you need to add borders to the interior CSS elements. You can use the HR tag to add lines inside individual cells.

How do I remove border spacing in a table?

This is a Default behavior of the table cells that there is some space between their Borders. To remove this space we can use the CSS border-collapsing Property. This Property is used to set the borders of the cell present inside the table and tells whether these cells will share a common border or not.

How is border spacing in a table is specified in?

Correct Option: C. border-spacing and border-collapse are the two properties by which one can set the border and its styling in a table. We give its value in pixels.

How do you put a border inside a table?

It can be achieved by using first-child and last-child in CSS. Here, we select the first column and remove its left-hand side border, then select the first row and remove its top border, then we go for the last column and remove its right-hand side border and at last select the last row and remove its bottom border.


2 Answers

Had the same problem, the border spacing property was adding space around the table as well, and to my knowledge, there wasn’t anyway to limit it to only ‘the inside’, so I used transparent borders instead:

table td {    border-left: 1em solid transparent;    border-top: 1em solid transparent; } 

This sets ‘border spacing’ as normal, except that there’s ‘unwanted’ spacing at the top and left of the table.

table td:first-child {    border-left: 0; } 

Selects the first column.

table tr:first-child td {    border-top: 0; } 

Selects the td elements of the first row (assuming that the top of the table starts with a tr element, change accordingly for th).

like image 63
JumpStomp Avatar answered Oct 03 '22 05:10

JumpStomp


I optimized the solution with transparent border so it has no more obliquely cut inner borders.

1) let table fill horizontal and collapse the borders:

table {   width: 100%;   border-collapse: collapse; } 

2) Set all borders of table cells to width 0 and prevent background is drawn below the border.

td {   border: 0px solid transparent;   background-clip: padding-box; } 

3) Set inner space with transparent border but not to first row and column.

tr > td + td {   border-left-width: 10px; }  tr + tr > td {   border-top-width: 10px; } 

here is a jsbin

like image 23
TLindig Avatar answered Oct 03 '22 03:10

TLindig