CSS border issue with THEAD TBODY and COLSPAN - html

i have an issue with css/html table:
When I use thead and tbody tags with a colspan attribute, the bottom border of the header is divided.
The gap size is dependent of the th border width.
Did you have a solution to get a continuous border on header bottom (without removing thead and tbody) ?
JSFiddle example
table {
border-collapse: collapse;
}
th {
border: 4px solid red;
border-bottom: 4px solid black
}
td {
border: 4px solid blue;
}
thead tr {
border-bottom: 5px solid green
}
table {
border-collapse: collapse;
}
th {
border: 4px solid red;
border-bottom: 4px solid black
}
td {
border: 4px solid blue;
}
thead tr {
border-bottom: 5px solid green
}
with THEAD and TBODY but without COLSPAN
<table>
<thead>
<tr>
<th>
Column 1
</th>
<th>
Column 2
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Content 1
</td>
<td>
Content 2
</td>
</tr>
</tbody>
</table>
<br /> COLSPAN with THEAD and TBODY <span style="background:yellow">(css bug in the middle of green border ?)</span>
<table>
<thead>
<tr>
<th>
Column 1
</th>
<th>
Column 2
</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">
Content 1 and 2 (merged cells)
</td>
</tr>
</tbody>
</table>
<br /> COLSPAN without THEAD and TBODY
<table>
<tr>
<th>
Column 1
</th>
<th>
Column 2
</th>
</tr>
<tr>
<td colspan="2">
Content 1 and 2 (merged cells)
</td>
</tr>
</table>

The corner rendering between collapsed borders does not seem well-specified, so it's not clear that this is actually a bug rather than just a variance in behaviour.
I did find a horrible workaround for Firefox, by creating a pseudo second row in the thead, and then hiding it, and also hiding the top border of the first tbody row like this:
thead:after {
content:'';
display:table-row; /* see note below */
position:absolute;
visibility:hidden;
}
tbody tr:first-child td {
border-top-width:0;
}
(Note that the display:table-row is just for show. In reality, the position:absolute causes the pseudo-element to be display:block regardless of whether the display property is set to table-row or left at its default inline. The table layout of the container will then cause that block to be wrapped in anonymous table objects of table-row and table-cell to form a well structured table.)
table {
border-collapse: collapse;
}
th {
border: 4px solid red;
border-bottom: 4px solid black
}
td {
border: 4px solid blue;
}
thead tr {
border-bottom: 5px solid green
}
table ~ table thead:after {
content:'';
position:absolute;
visibility:hidden;
}
table ~ table tbody tr:first-child td {
border-top-width:0;
}
with THEAD and TBODY but without COLSPAN
<table>
<thead>
<tr>
<th>
Column 1
</th>
<th>
Column 2
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Content 1
</td>
<td>
Content 2
</td>
</tr>
</tbody>
</table>
<br /> COLSPAN with THEAD and TBODY <span style="background:yellow">(css bug in the middle of green border ?)</span>
<table>
<thead>
<tr>
<th>
Column 1
</th>
<th>
Column 2
</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2">
Content 1 and 2 (merged cells)
</td>
</tr>
</tbody>
</table>
<br /> COLSPAN without THEAD and TBODY
<table>
<tr>
<th>
Column 1
</th>
<th>
Column 2
</th>
</tr>
<tr>
<td colspan="2">
Content 1 and 2 (merged cells)
</td>
</tr>
</table>

You can have an illusive fix to this by changing the borders from 4px/5px to 1px. As to why you are getting that must have to deal with the properties of thead and tbody, being that the problem only occurs with their presence.
Refer to: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/thead

Related

Add specific border styling and spacing that differs for the table, tr and cells

I was wondering if it was possible, with html and/or CSS, to collapse only tr with he table's outline, while having different border styles for the table's outline and trs, and the tds and ths.
I know this is complicated, so if this can make it clearer, here's a drawing of what I'm trying to achieve:
No, border-collapse applies only to the whole table, and it is not a valid property for tr or td elements so you cannot apply it to those to get a different spacing.
However you can “fake” it by adding the cell content into a div and using it for some of the styling:
Apply the outer table styling to the table as normal
Apply the row styling to the top and bottom borders of the th / td cells
Apply the "cell" styling to the divs inside the th & tds.
Working Example:
table {
border: 6px solid lightgray;
border-right-color: gray;
border-bottom-color: gray;
border-collapse: collapse;
}
td {
border-top: 5px solid gray;
}
tr:not(:last-child) td{
border-bottom: 5px solid gray;
}
th .cell,
td .cell {
margin: 5px;
padding: 5px;
border: 2px ridge lightblue;
}
<table>
<tr>
<th><div class="cell">First Name</div></th>
<th><div class="cell">Last Name</div></th>
</tr>
<tr>
<td><div class="cell">John</div></td>
<td><div class="cell">Smith</div></td>
</tr>
<tr>
<td><div class="cell">Jane</div></td>
<td><div class="cell">Doe</div></td>
</tr>
</table>
Found a way by just adding an hr and a minuscule tr between both trs.
hr {
border: 4px outset rgb(207, 172, 179);
width: 443px;
position: absolute;
top: 388px;
left: 35px;
}
tr#mini {
border: none;
padding: 0px;
margin: 0px;margin-top: 0px;
margin-bottom: 0px;
height: 8px;
}
HTML:
<table id="tableau" class="nomaltext">
<tr>
<th>
25g
</th>
<th>
50g
</th>
<th>
75g
</th>
<th>
100g
</th>
<th>
Personnalisé (min. 120g)
</th>
</tr>
<tr id="mini">
<hr>
</tr>
<tr>
<td>
5,99$
</td>
<td>
8,99$
</td>
<td>
13,80$
</td>
<td>
7,40$
</td>
<td>
11¢/gramme
</td>
</tr>
</table>

Why is border collapse and spacing not working when you target only the Table Head (TH) inside a Table?

My HTML:
<table style="width:100%">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
My CSS:
table, th, td {
margin-top:150px;
margin-bottom:150px;
border:1px solid black;
}
th, td {
padding:15px;
}
th {
text-align:left;
border-collapse:collapse;
}
I want to collapse the borders of the TH only, but it's not working. Border collapse and border spacing aren't working when i target only the TH. I can change the background color and the padding and do other changes to TH only, but border changes seems to not work. Why is that?
Note: Before you tell me how it can be done using other ways, please tell me why THIS way isn't working.
Because border-collapse is a style rule of the table and not of the single cells (td or th). This means that you set it on the table element and all the borders in the table will collapse or separate.
You can mimic the behavior of border-collapse: separate only in td by doing something "hacky" like inserting a div inside tds. Check out the fiddle below:
table {
border-collapse: collapse;
}
th {
border: 1px solid black;
}
td {
padding: 2px;
}
td:first-child {
padding-left: 0;
}
td:last-child {
padding-right: 0;
}
td > div {
height: 100%;
width: 100%;
border: 1px solid black;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td><div>Cell 1</div></td>
<td><div>cell 2</div></td>
</tr>
<tr>
<td><div>Cell 3</div></td>
<td><div>Cell 4</div></td>
</tr>
</tbody>
</table>
as everybody told you , border-collapse is a rule set for the whole table, it tells how cells should be printed at screen side by sides.
A work around could be to fake borders with a box-shadow.
inside tds :
table {
border-collapse: collapse;
}
th {
border: solid 2px;
box-shadow: inset 0 -2px;
}
td {
border: solid transparent;
box-shadow: inset 0 0 0 2px;
padding:3px;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td> Cell 1 </td>
<td> Cell 2 </td>
</tr>
<tr>
<td> Cell 3 </td>
<td> Cell 4 </td>
</tr>
</tbody>
</table>
outside th
thead {
padding-bottom: 2px;
}
th {
border: 0;
box-shadow: 0 -2px, inset 0 -2px, 2px 0, -2px 0, 2px -2px, -2px -2px;
padding: 2px;
}
td {
border: solid 2px;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td> Cell 1 </td>
<td> Cell 2 </td>
</tr>
<tr>
<td> Cell 3 </td>
<td> Cell 4 </td>
</tr>
</tbody>
</table>
The border-collapse property can only be applied to <table> elements - not individual rows or cells
https://developer.mozilla.org/en-US/docs/Web/CSS/border-collapse

CSS add border-spacing & border-collapse to thead

I have a little spesific question. I'm adding border-spacing: seperate to my table. But i want to add border-spacing to only thead tag. Is it possible?
What you can do (if you want border around your thead cells) is to wrap then in a span and manipulate it using css:
table {
border: solid 1px black;
}
table thead th {
padding: 20px;
}
table thead th span {
border: solid 1px black;
}
<table>
<thead>
<tr>
<th>
<span>Month</span>
</th>
<th>
<span>Savings</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$100</td>
</tr>
<tr>
<td>February</td>
<td>$80</td>
</tr>
</tbody>
</table>

Right fixed column table - background color of fixed column not working

I need a table just like describred in Fixed right column table scales to responsive design But when I define a background color using css the rule doesn't apply to the fixed column
Jsfiddle: https://jsfiddle.net/3ckvkr1f/2/
Thanks!
HTML
<div class="table-responsive">
<table class="table-striped" cellpadding="9">
<thead>
<tr>
<th>
col1
</th>
<th>
col2
</th>
<th class="crud-links"> Options</th>
</tr>
</thead>
<tr>
<td>
R1col1 alçkfjalçkfjalkjflaksjflaksj
</td>
<td>
R1col2 aslkfjasklçfjaklçfjasklfjasçklfjas
</td>
<td class="crud-links">
x
</td>
</tr>
<tr>
<td style="white-space: nowrap;">
R2col1 alçkfjalçkfjalkjflaksjflaksj slkfjsçklafjaslfkjsldk
</td>
<td style="white-space: nowrap;">
R2col2 aslkfjasklçfjaklçfjasklfjasçklfjas
</td>
<td class="crud-links">
x
</td>
</tr>
<tr>
<td style="white-space: nowrap;">
R3col1 alçkfjalçkfjalkjflaksjflaksj slkfjsçklafjaslfkjsldk
</td>
<td style="white-space: nowrap;">
R3col2 aslkfjasklçfjaklçfjasklfjasçklfjas
</td>
<td class="crud-links">
x
</td>
</tr>
</table>
</div>
CSS:
.table-striped > tbody > tr:nth-of-type(2n+1) {
background-color: blue;
}
.page-header {
padding-bottom: 9px;
margin: 40px 0 20px;
border-bottom: 1px solid #eeeeee;
}
.table-hover th, .table-hover td {
padding: 9px;
}
.table-responsive {
width: inherit;
max-width: 80%;
margin-bottom: 15px;
overflow-x: scroll;
overflow-y: hidden;
border: 0;
}
.crud-links{
position: absolute;
overflow:hidden;
width: 91px;
right: 0;
}
.table-striped > tbody > tr:nth-of-type(2n+1) {
background-color: blue;
}
Are you talking about the ones with the class .crud-links?
If so, just do
tr .crud-links { background: something; }
If you're talking about them not getting the same color as every other in the main part, just do the same, but using tr .crud-links:nth-of-type(odd)
Your css code refers to tbody tag, but You are missing it.
.table-striped > tbody > tr:nth-of-type(2n+1)
Correct yout html code, or change css like this:
.table-striped tr:nth-of-type(2n+1)
/*first three column class name as follow, */
/*tbody used for only tr td work otherwise table header also work with bgcolor*/
/*fixed column first three column hover color change*/
tbody > tr:hover > .freez,
tbody >tr:hover > .freez2,
tbody> tr:hover > .freez3{
background-color:#f5f5f5 !important;
}

Turning into certain inline css into a separate sheet

<table border="1" style="background-color:yellow;border:1px dotted black;width:80%;border-collapse:collapse;">
<tr style="background-color:orange;color:white;">
<th style="padding:3px;">Table header</th><th style="padding:3px;">Table header</th>
</tr>
<tr>
<td style="padding:3px;">Table cell 1</td><td style="padding:3px;">Table cell 2</td>
</tr>
<tr>
<td style="padding:3px;">Table cell 3</td><td style="padding:3px;">Table cell 4</td>
</tr>
</table>
My question is how do i convert the inline CSS in this html example into an external style sheet?
would the css look like this?
tr {background-color:orange;color:white;}
table {background-color:yellow;border:1px dotted black;width:80%;border-collapse:collapse;}
td, th {padding:3px;}
Use table tr:first-child instead of tr as only the first row has inline styles:
table {
background-color: yellow;
border: 1px dotted black;
border-collapse: collapse;
width: 80%;
}
table th, table td {
padding: 3px;
}
table tr:first-child {
background-color: orange;
color: white;
}
http://jsfiddle.net/URBMh/2/