Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: table border separated horizontally and collapsed vertically

Tags:

html

css

is there any way to apply to a table cells' both the separate and the collapsed border properties to have collapsed but separated? Thanks

EDIT: this is the wanted result:

alt text

like image 915
pistacchio Avatar asked Jul 20 '10 12:07

pistacchio


People also ask

What is the difference between border-collapse collapse and border-collapse separate?

(See Snippet below to confirm). The border-collapse CSS property determines whether a table's borders are separated or collapsed. In the separated model, adjacent cells each have their own distinct borders. In the collapsed model, adjacent table cells share borders.

Which CSS property can be used for collapsing the border between table cells?

The border-collapse CSS property sets whether cells inside a <table> have shared or separate borders.


3 Answers

Perhaps

table {
  border-spacing: 1px 0;
}
like image 87
Ms2ger Avatar answered Oct 06 '22 01:10

Ms2ger


The closest I can get is:

table {
    border-collapse: separate;
    border-spacing: 4px 0;
}
table td, table th {
    border: 1px solid black;
}

Unfortunately, this will create a double-thick line between the rows. Negative values are not allowed in the border-spacing property, otherwise -1px would probably work.


You could make the other lines 2px wide if that is acceptable, then at least you wouldn't have differing border thicknesses:

table {
    border-collapse: separate;
    border-spacing: 4px 0;
}
table td, table th {
    border-width: 1px 2px;
    border-style: solid;
    border-color: black;
}
table tr:first-child th,
table tr:first-child td {
    border-top-width: 2px;
}
table tr:last-child th,
table tr:last-child td {
    border-bottom-width: 2px;
}
like image 43
DisgruntledGoat Avatar answered Oct 06 '22 00:10

DisgruntledGoat


This can be achieved without using extra div elements in the th & td cells. This solution works in Chrome, Firefox and IE8+.

CSS

table
{
    border-collapse: separate;
    border-spacing: 10px 0px;
}
td, th
{
    padding: 10px;
    border: 1px solid #000;
    border-top: none;
}
table tr:first-child th
{
    border-top: 1px solid #000;
}

Change table tr:first-child th to table tr:first-child td if the table's first row doesn't contain table header cells (TH).

See my jsfiddle here: Table with column spacing but collapsed row border

like image 23
Ben Avatar answered Oct 05 '22 23:10

Ben