I'm trying to style table with what I thought would be a fairly simple style to achieve but have run in to a little issue.
The table will show a coloured indicator on the left hand side of each row so I'm using border-left: 5px solid red; to add it. However, although the border applies - half of it is inside the row and half outside. I've tried adding border-collapse: collapse to no avail, I'm also using box-sizing: border-box but still have the same issue.
Finally, I've also tried adding the border to the first-child cell (td) but the same issue appears.
I've set up an example of what's happening - I've put in an oversized border to highlight the issue:
http://www.cssdesk.com/TVa67
Has anyone run into this before or have any solutions?
body {
background: blue;
}
table {
border-collapse: collapse;
box-sizing: border-box;
table-layout: fixed;
width: 100%;
}
th,
td {
background-color: #fff;
padding: 10px 15px 8px;
}
th {
border-bottom: 1px solid blue;
font-weight: normal;
text-align: left;
}
td {
border-bottom: 1px solid gray;
}
tr.low {
border-left: 25px solid red;
}
<table style="
border-collapse: collapse;
">
<tbody>
<tr>
<th>#</th>
<th>Status</th>
<th>Title</th>
<th>Project</th>
<th>Assigned To</th>
<th>Age</th>
</tr>
<tr class="low">
<td>1</td>
<td>New</td>
<td>This is an example ticket</td>
<td>Something</td>
<td>Something</td>
<td>2 days ago</td>
</tr>
</tbody>
</table>
However, although the border applies - half of it is inside the row
and half outside
This behaviour is expected and is as per specs. Refer to: http://www.w3.org/TR/CSS2/tables.html#collapsing-borders where it says:
Borders are centered on the grid lines between the cells...
It also illustrates that with a diagram with description.
Has anyone run into this before or have any solutions?
Yes, it can be easily demonstrated as in this fiddle: http://jsfiddle.net/abhitalks/xs7L9wn1/1/ and the below Snippet:
* { box-sizing: border-box; }
table {
border-collapse: collapse;
border: 1px solid gray;
table-layout: fixed; width: 70%;
margin: 0 auto;
}
th, td {
border: 1px solid gray;
padding: 6px;
text-align: center;
}
tbody > tr:nth-child(1) > td:first-child { border-left: 16px solid red; }
tbody > tr:nth-child(2) > td:first-child { border-left: 8px solid green; }
tbody > tr:nth-child(3) > td:first-child { border-left: 24px solid blue; }
tbody > tr:nth-child(1) > td:last-child { border-left: 16px solid red; }
tbody > tr:nth-child(2) > td:last-child { border-left: 8px solid green; }
tbody > tr:nth-child(3) > td:last-child { border-left: 24px solid blue; }
<table>
<thead>
<tr>
<th>#</th>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Caption</td>
<td>Description</td>
</tr>
<tr>
<td>2</td>
<td>Caption</td>
<td>Description</td>
</tr>
<tr>
<td>3</td>
<td>Caption</td>
<td>Description</td>
</tr>
</tbody>
</table>
Solution:
Just add a transparent border of the same width to all rows. That way the border-width will be same and it will neatly align. (Update: added a white border-left to first column to hide the hanging border on highlighted cell. As pointed out by your comment.)
th, td { border-left: 15px solid transparent; }
tr > td:first-child, tr > th:first-child { border-left: 5px solid #fff; }
tr.low > td:first-child { border-left: 5px solid red; }
Example Fiddle: https://jsfiddle.net/abhitalks/s9taanz7/5/
Snippet:
* { box-sizing: border-box; }
body { background-color: blue; }
table {
border-collapse: collapse;
table-layout: fixed; width: 100%;
}
th, td {
background-color: #fff;
padding: 10px 15px 8px 8px;
border-left: 5px solid transparent;
border-bottom: 1px solid gray;
}
th {
border-bottom: 1px solid blue;
font-weight: normal; text-align: left;
}
tr > td:first-child, tr > th:first-child { border-left: 10px solid #fff; }
tr.low > td:first-child { border-left: 10px solid red; }
<table>
<thead>
<tr>
<th>#</th>
<th>Status</th>
<th>Title</th>
<th>Project</th>
<th>Assigned To</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr class="">
<td>1</td>
<td>New</td>
<td>This is an example ticket</td>
<td>Something</td>
<td>Something</td>
<td>2 days ago</td>
</tr>
<tr class="low">
<td>2</td>
<td>New</td>
<td>This is an example ticket</td>
<td>Something</td>
<td>Something</td>
<td>2 days ago</td>
</tr>
<tr class="">
<td>3</td>
<td>New</td>
<td>This is an example ticket</td>
<td>Something</td>
<td>Something</td>
<td>2 days ago</td>
</tr>
</tbody>
</table>
However, this approach will have a side-effect of hidden border-bottom because the border-left overlaps it.
Solution 2:
You could have an extra cell on the left to use as indicator. You can then control this by use of colgroup. This approach is neater than above and also requires you to have the width specified only once in css.
Example Fiddle 2: http://jsfiddle.net/abhitalks/z7u1nhwt/1/
Snippet 2:
* { box-sizing: border-box; }
body { background-color: blue; }
table {
border-collapse: collapse;
table-layout: fixed; width: 100%;
}
th, td {
background-color: #fff;
padding: 10px 15px 8px 8px;
border-bottom: 1px solid gray;
}
th {
border-bottom: 1px solid blue;
font-weight: normal; text-align: left;
}
.col1 { width: 10px; }
tr.low > td:first-child {
background-color: #f00;
}
<table>
<colgroup>
<col class="col1" />
<col class="coln" span="6" />
</colgroup>
<thead>
<tr>
<th></th>
<th>#</th>
<th>Status</th>
<th>Title</th>
<th>Project</th>
<th>Assigned To</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr class="">
<td></td>
<td>1</td>
<td>New</td>
<td>This is an example ticket</td>
<td>Something</td>
<td>Something</td>
<td>2 days ago</td>
</tr>
<tr class="low">
<td></td>
<td>2</td>
<td>New</td>
<td>This is an example ticket</td>
<td>Something</td>
<td>Something</td>
<td>2 days ago</td>
</tr>
<tr class="">
<td></td>
<td>3</td>
<td>New</td>
<td>This is an example ticket</td>
<td>Something</td>
<td>Something</td>
<td>2 days ago</td>
</tr>
</tbody>
</table>
And of course, you can also try the approach of using pseudo-element as proposed by #misterManSam, depending on ease of implementation for you.
Related
I have a table with border-collapse applied. I want to remove some td border such as border-right within the table. I used the following css to do the job but this code also remove 1px of the other borders I don't want to remove. In fact it adds 1px solid white to the top and bottom border of the table where the removed border-right was there
.no-border-right {
border-right: solid 10px #FFF!important;
}
table {
border-collapse: collapse;
font-size: 16px;
padding: 6px;
}
table td {
border: 10px solid gray;
}
table th {
border: 10px solid gray;
}
<table align="center">
<tr>
<th>sl</th>
<th>name</th>
<th>score</th>
<th>rank</th>
</tr>
<tr>
<td>1</td>
<td>John</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td class="no-border-right">James</td>
<td>1</td>
<td>2</td>
</tr>
</table>
<table>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
<tr>
<td></td>
<td class="no-border-right"></td>
<td></td>
</tr>
</table>
How can I remove without affecting the other borders?
My expected result from the snippet is below:
table {
border-collapse: collapse;
padding: 6px;
}
table td, table th {
border: 1px solid gray;
}
table td.no-border-right {
border-right: none!important;
}
table td.no-border-right + td {
border-left: none!important;
}
<table align="center">
<tr>
<th>sl</th>
<th>name</th>
<th>score</th>
<th>rank</th>
</tr>
<tr>
<td>1</td>
<td>John</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td class="no-border-right">James</td>
<td>1</td>
<td>2</td>
</tr>
</table>
You need to use the border-right: none; that should do it for you, also I left you a code snippet down there if you get stuck.
.no-border-right {
border: 1px solid red;
display: inline-block;
/* from here up is not it's necessary just to help visually see it demonstration */
border-right: none; /* Use this to remove right border */
}
<div class="no-border-right">
<p>Hello Wolrd!</p>
</div>
Make the right-border color's alpha 0 like so:
border-right { 10px solid rgba(0,0,0,0); }
.no-border-right {
border-right: 10px solid rgba(0,0,0,0);
}
table {
border-collapse: collapse;
font-size: 16px;
padding: 6px;
}
table td {
border: 10px solid gray;
}
table th {
border: 10px solid gray;
}
<table align="center">
<tr>
<th>sl</th>
<th>name</th>
<th>score</th>
<th>rank</th>
</tr>
<tr>
<td>1</td>
<td>John</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td class="no-border-right">James</td>
<td>1</td>
<td>2</td>
</tr>
</table>
What is the most elegant way to remove the up down border?
table {
border: 1px solid #CCC;
border-collapse: collapse;
text-align: center;
}
.noborder {
border: none;
width: 50px;
}
<br>
<table border='1' width='500'>
<tr>
<th>Product</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th class="noborder"> </th>
<th>Delete</th>
</tr>
<tr>
<td>AA</td>
<td>BB</td>
<td>CC</td>
<td>DD</td>
<td class="noborder"> </td>
<td>FF</td>
</tr>
</table>
JSfiddle Demo
You need to apply border for the th and td elements and not for the entire table. Setting border for the table will not be affected by the noborder class applied on the child elements.
Updated JSfiddle
table {
border-collapse: collapse;
text-align: center;
}
.noborder {
border: none;
width: 50px;
}
th,
td {
border: 1px solid #CCC;
}
<br>
<table width='500'>
<tr>
<th>Product</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th class="noborder"> </th>
<th>Delete</th>
</tr>
<tr>
<td>AA</td>
<td>BB</td>
<td>CC</td>
<td>DD</td>
<td class="noborder"> </td>
<td>FF</td>
</tr>
</table>
I'd say specify only the borders you need.
If you don't want the top and bottom... only set the left and right border.
table {
border-left: 1px solid #000;
border-left: 1px solid #000;
}
If you're trying to remove only the top and bottom of that blank column, you could try setting the border for each column separately, thus allowing the removal of the desired border.
td {
border: 1px solid #000;
}
#blank-td {
border: none /* or even 1px solid #fff */;
}
Just mess around with it
More info: http://www.w3schools.com/css/css_border.asp
I have a table that when I use border-collapse:collapse; it shows a single 1px in the border of the row coming from the following row. Any ideas how I can fix that?
I want it to look exactly like that without any padding between the cells or anything.
This is in Firefox by the way, I haven't tried other browsers.
Here is my JSFiddle.
HTML / CSS
.responsive-table {
border-collapse: collapse;
width: 100%;
}
.responsive-table td {
border: 1px solid #ddd;
}
.responsive-table tbody tr:nth-of-type(2n) {
background: #f7f7f7;
}
.responsive-table tbody tr:nth-of-type(2n+1),
.resp-content,
.resp-div {
background: #eee;
}
.responsive-table thead th,
.basic-table thead th {
background: #006bb2;
color: #fff;
}
.responsive-table th {
border: 1px solid #0087e0;
}
<table class="responsive-table">
<thead>
<tr>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>
Just increase the lower border width of the table header:
.responsive-table th {
border: 1px solid #0087e0;
border-bottom-width: 2px;
}
.responsive-table {
border-collapse: collapse;
width: 100%;
}
.responsive-table td {
border: 1px solid #ddd;
}
.responsive-table tbody tr:nth-of-type(2n) {
background: #f7f7f7;
}
.responsive-table tbody tr:nth-of-type(2n+1),
.resp-content,
.resp-div {
background: #eee;
}
.responsive-table thead th,
.basic-table thead th {
background: #006bb2;
color: #fff;
}
.responsive-table th {
border: 1px solid #0087e0;
border-bottom-width: 2px;
}
<table class="responsive-table">
<thead>
<tr>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>
Alternatively, border-bottom-style: double; will specify, as the name suggests, a double border:
.responsive-table th {
border: 1px solid #0087e0;
border-bottom-style: double;
}
.responsive-table {
border-collapse: collapse;
width: 100%;
}
.responsive-table td {
border: 1px solid #ddd;
}
.responsive-table tbody tr:nth-of-type(2n) {
background: #f7f7f7;
}
.responsive-table tbody tr:nth-of-type(2n+1),
.resp-content,
.resp-div {
background: #eee;
}
.responsive-table thead th,
.basic-table thead th {
background: #006bb2;
color: #fff;
}
.responsive-table th {
border: 1px solid #0087e0;
border-bottom-style: double;
}
<table class="responsive-table">
<thead>
<tr>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
<th>H</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>
More information: Border conflict resolution
Use pseudo-elements to cover it up:
.responsive-table thead th:after {
width: 0;
height: 0;
content: '';
position: absolute;
right: 0;
bottom: 0;
border-left: 1px solid #0087e0;
}
Just change the color of the border to whatever matches. You could also use a :before to cover up the far left side's blemish.
i am trying to handle the bottom lines inside a table (i want to cut them a bit to the right)
here is my table code:
<table class="reportTable">
<thread>
<tr>
<th>firstColumn</th>
<th>secondColumn</th>
<th>thirdColumn</th>
</tr>
</thread>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>11</td>
<td>22</td>
<td>33</td>
</tr>
<tr>
<td>111</td>
<td>222</td>
<td>333</td>
</tr>
</tbody>
</table>
and here is my css code:
.reportTable th,td{
border-bottom: 1px solid #E7E7E7;
border-right:16px solid transparent;
}
You can add border-spacing to the table and position it relative with a negative left to push it back to it's original spot.
http://jsfiddle.net/hyn1db04/3/
<table class="reportTable">
<thread>
<tr>
<th>firstColumn</th>
<th>secondColumn</th>
<th>thirdColumn</th>
</tr>
</thread>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>11</td>
<td>22</td>
<td>33</td>
</tr>
<tr>
<td>111</td>
<td>222</td>
<td>333</td>
</tr>
</tbody>
CSS
.reportTable {
border-spacing: 30px 0;
}
.reportTable th, .reportTable td {
border-bottom: 1px solid #E7E7E7;
position: relative;
left: -30px;
}
EDIT:
Alternatively, you can do this: http://jsfiddle.net/m021e4gh/8/
.reportTable th,td {
border-bottom: 1px solid #E7E7E7;
}
table td + td, table th + th {
position: relative;
left: 30px;
}
table td + td + td, table th + th + th {
position: relative;
left: 60px;
}
Add a width if you want.
I believe what you want is the border-spacing property in CSS:
.reportTable {
border-spacing: 10px 0;
}
.reportTable th, .reportTable td {
border-bottom: 1px solid #E7E7E7;
border-right: 1px solid #E7E7E7;
}
Example on JSFiddle
Below is a small simple table as an example. I want the header row to be of one color (grey). The alternate rows below header to be of another color (yellow). And the first column of another color (orange).
I am not able to achieve the desired results with the CSS. How do I go about it without individually coloring the cells.
Code in JSFiddle
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
padding: 5px;
}
table {
width: 100%;
}
th {
background-color: grey;
color: white;
}
tr:nth-child(odd) {
background-color: yellow;
}
<table>
<col style="background-color: orange;" />
<tr>
<th> </th>
<th>Bank</th>
<th>Amount</th>
</tr>
<tr>
<td>John</td>
<td>ABC</td>
<td>12345</td>
</tr>
<tr>
<td rowspan="2">David</td>
<td>DEF</td>
<td>456789</td>
</tr>
<tr>
<td>GHI</td>
<td>147258</td>
</tr>
<tr>
<td>Kevin</td>
<td>JKL</td>
<td>258369</td>
</tr>
<tr>
<td>Mike</td>
<td>MNO</td>
<td>369258</td>
</tr>
</table>
It's not a very good solution but here is a fix:
http://jsfiddle.net/sthmw0dk/2/
I have added the following lines:
tr td:first-child
{
background-color: orange;
}
tr:nth-child(even) td:nth-last-child(2)
{
background-color: white;
}
tr:nth-child(odd) td:nth-last-child(2)
{
background-color: yellow;
}
The last selector is necessary when you change your rowspan to 3 or more.
I would however suggest using classes for your first column. It will be a lot easier a better when you want to use more columns in the future.
demo - http://jsfiddle.net/victor_007/sthmw0dk/3/
The only thing i have changed is styling td instead of tr with background-color
Using td:not(:first-child) the first-child of tr will not get any styling
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
text-align: center;
padding: 5px;
}
table {
width: 100%;
}
th {
background-color: grey;
color: white;
}
.first-col {
background-color: orange;
}
tr:nth-child(odd) td:not(:first-child) {
background-color: yellow;
}
<table>
<colgroup>
<col class="first-col" />
</colgroup>
<tr>
<th> </th>
<th>Bank</th>
<th>Amount</th>
</tr>
<tr>
<td>John</td>
<td>ABC</td>
<td>12345</td>
</tr>
<tr>
<td rowspan="2">David</td>
<td>DEF</td>
<td>456789</td>
</tr>
<tr>
<td>GHI</td>
<td>147258</td>
</tr>
<tr>
<td>Kevin</td>
<td>JKL</td>
<td>258369</td>
</tr>
<tr>
<td>Mike</td>
<td>MNO</td>
<td>369258</td>
</tr>
</table>