CSS Child selector not working as expected - html

I have an HTML table with another table embedded in it like:
table.index {
border-radius: 10px;
border: solid 1px #61a2d1;
border-spacing: 0;
}
table.index > thead > tr:first-child {
background-color: #61a2d1;
}
table.index > thead > tr:first-child > td:first-child {
border-top-left-radius: 10px;
}
table.index > thead > tr:first-child > td:last-child {
border-top-right-radius: 10px;
}
table.index > thead td {
font-weight: bold;
text-align: center;
}
table.index > tr:nth-child(3) {
background-color: rgba(97, 162, 209, 0.5);
}
table.index > tr:hover {
background-color: #ffda6d;
}
table.index > tbody > tr:last-child td:first-child {
border-bottom-left-radius: 10px;
}
table.index > tbody > tr:last-child td:last-child {
border-bottom-right-radius: 10px;
}
table.index .no-right-borrder {
border-right: none;
}
table.index .no-left-border {
border-left: none;
text-align: right;
}
table.details {
margin: 0;
padding: 0;
border: solid 1px #61a2d1;
border-spacing: 0;
}
<table class="index" style="width:100%">
<thead>
<tr>
<td style="width:2%"></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<td class="no-right-borrder" style="vertical-align:middle"><span class="fa fa-caret-right fa-lg"></span></td>
<td class="no-left-border" style="text-align:left;"></td>
<td class="no-right-borrder" style="text-align:center"></td>
<td class="no-left-border"></td>
</tr>
<tr>
<td colspan="4" style="padding:0;margin:0">
<table class="details" style="width:100%">
<tbody>
<tr>
<td colspan="2"><label for=""></label></td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2"><label for=""></label></td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2"><label for=""></label></td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="2"><label for=""></label></td>
<td colspan="2"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
When I open this page, the cells in the table with the class details have rounded corners just like the cells in the table with the class index.
I've used the direct child selector to indicate that I only wanted the routed borders to be applied to direct children of a table with the index class, so I'm not sure why it's happening. When I examine the DOM in Google Developer Tools, it tells me that the border-radius attribute is coming from the .index class. What do I need to do to stop that from happening?
edit: This is what I'm seeing on my local machine. CSS code is copied exactly from my code. Table was simplified because it's auto-generated, but the classes are applied identically.

I suspect you need to change table.index > tbody > tr:last-child td:first-child into table.index > tbody > tr:last-child > td:first-child (putting the direct descendant selector between tr > td), and the same for the td:last-child selector that directly follows.

Related

Remove borders in nested table

How does one prevent the inheriting of borders in a nested tabled?
I tried to a add a borderless class to the nested table, but the borders of the parent class are still inherited.
.plain_tbl {
border: none;
}
.plain_tbl tr td {
border: none;
}
.tbl {
border: 1px solid;
}
.tbl tr td {
border: 1px solid;
}
<table class="tbl">
<tr>
<td align=center style="padding:50px">
<table border=0 class="plain_tbl">
<tr>
<td>0--------------------100</td>
</tr>
<tr>
<td align=center> ^ <br>50</td>
</tr>
</table>
</td>
</tr>
</table>
I am looking for a solution which does not change the parent class, i.e. how to make the nested class overwrite the parent class
The problem you have is
.tbl tr td {
border: 1px solid;
}
which will add a border to every <td> also if it is a table inside a table you could fix it by using
.tbl > tr > td {
border: 1px solid;
}
.plain_tbl {
border: none;
}
.plain_tbl tr td {
border: none;
}
.tbl {
border:1px solid;
}
.tbl > tr > td {
border: 1px solid;
}
<table class="tbl">
<tr>
<td align=center style="padding:50px">
<table border=0 class="plain_tbl">
<tr><td>0--------------------100</td></tr>
<tr><td align=center> ^ <br>50</td></tr>
</table>
</td>
</tr>
</table>
Update, another solution then previous using specificity
In the comments you asked
why does the nested class not overwrite the parent class?
The reason is Specificity and the order of your CSS rules. If you are not allowed to change the CSS order you can change the specificity. You need to be more specific then this
.tbl tr td {
border: 1px solid;
}
To be more specific you can change like so
.tbl .plain_tbl tr td {
border: none;
}
run the code snippet and see it work
.plain_tbl {
border: none;
}
.tbl .plain_tbl tr td {
border: none;
}
.plain_tbl tr td {
border: none;
}
.tbl {
border:1px solid;
}
.tbl tr td {
border: 1px solid;
}
<table class="tbl">
<tr>
<td align=center style="padding:50px">
<table border=0 class="plain_tbl">
<tr><td>0--------------------100</td></tr>
<tr><td align=center> ^ <br>50</td></tr>
</table>
</td>
</tr>
</table>
Third method, only changing the order of your css
because both rules actually have the same specificity you can also just place the rule .plain_tbl tr td after .tbl tr td { in your css file like so:
.plain_tbl {
border: none;
}
/*
move this to the bottom
.plain_tbl tr td {
border: none;
}
*/
.tbl {
border: 1px solid;
}
.tbl tr td {
border: 1px solid;
}
/* see here */
.plain_tbl tr td {
border: none;
}
<table class="tbl">
<tr>
<td align=center style="padding:50px">
<table border=0 class="plain_tbl">
<tr><td>0--------------------100</td></tr>
<tr><td align=center> ^ <br>50</td></tr>
</table>
</td>
</tr>
</table>
your problem lies with the order of rules, since .tbl td is more general, it also applied to the inner element, and since its the last rule applied, it overrides the previous rule, which is the more specific rule
.
so simply rearrange your css and fix
.tbl {
border: 1px solid;
}
.tbl tr td {
border: 1px solid;
}
.plain_tbl {
border: none;
}
.plain_tbl tr td {
border: none;
}
<table class="tbl">
<tr>
<td align=center style="padding:50px">
<table border=0 class="plain_tbl">
<tr>
<td>0--------------------100</td>
</tr>
<tr>
<td align=center> ^ <br>50</td>
</tr>
</table>
</td>
</tr>
</table>
You can f.e. remove the plain_tbl class completely, and just apply the border to table cells that are in rows that are direct descendants of your main table:
.tbl,
.tbl > tr > td {
border: 1px solid;
}

Making the html table responsive without using jQuery or Javascript

How to we make the HTML table to responsive without using bootstrap or JQuery, it has more than eight-column.
I'm Using Percentage for each column even its not coming responsive when we increase the text
Scroll coming at the bottom. how to avoid scroll, I don't want to use the Unorder list or div elements
body{
font-family: arial, sans-serif;
font-size:12px
}
.text-to-right {
text-align: right;
}
.text-to-left {
text-align: left;
padding-left: 1rem;
}
table {
width: 100%;
border-left: 1px solid #f3f1f1;
border-bottom: 1px solid #f3f1f1fa;
border-spacing: 0;
}
.table-bordered > tbody > tr > td,
.table-bordered > tbody > tr > th,
.table-bordered > tfoot > tr > td,
.table-bordered > tfoot > tr > th,
.table-bordered > thead > tr > td,
.table-bordered > thead > tr > th {
border-left: 1px solid #f3f1f1;
border-bottom: 1px solid #d2cecefa;
/*box-shadow: 0 2px 4px 0 rgba(241, 230, 230, 0.16), 0 2px 10px 0 rgba(247, 245, 245, 0.12);*/
box-shadow: 0 1px 1px 0 #f3f1f1,0 1px 1px 0 #f3f1f1;
}
.table-bordered > tbody > tr > td:first-child,
.table-bordered > tbody > tr > th:first-child,
.table-bordered > tfoot > tr > td:first-child,
.table-bordered > tfoot > tr > th:first-child,
.table-bordered > thead > tr > td:first-child,
.table-bordered > thead > tr > th:first-child {
border-left-width: 0;
}
.table-bordered tbody tr td:last-child,.table-bordered thead tr th:last-child {
border-right: 1px solid #f3f1f1;;
}
.table-bordered > thead > th:last-child {
border-right: 1px solid #f3f1f1;
}
th {
padding: 1rem 0;
background: #192b4b;
color: #fff;
font-size: 1.3rem;
font-weight: normal;
border-left: 1px solid #f3f1f1;
}
td {
padding: 1rem;
text-transform: capitalize;
color: #6b5e5e;
}
.table-bright-data {
color: #007dc5;
font-size: 1.8rem;
font-family: 'Roboto Condensed', Arial, sans-serif;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table class="table-bordered">
<thead>
<tr>
<th width="05%">
S.No
</th>
<th class="text-to-left" width="23%">Name</th>
<th width="10%">Code</th>
<th width="12%">Type</th>
<th width="10%"> Profile</th>
<th width="13%">Time</th>
<th width="10%">Size</th>
<th width="11%">Date</th>
</tr>
</thead>
<tbody>
<tr>
<td class="align-center">
1
</td>
<td class="text-to-left table-bright-data">Responsive layout</td>
<td class="text-to-left">LDHSNSJ734674</td>
<td class="text-to-left">Fixed Interest </td>
<td class="text-to-left">high </td>
<td class="text-to-right">No minimum </td>
<td class="text-to-right">0 </td>
<td class="text-to-right">05-Dec-2001 </td>
</tr>
<tr>
<td class="align-center">
2
</td>
<td class="text-to-left table-bright-data">Responsive layout</td>
<td class="text-to-left">DHEYE933</td>
<td class="text-to-left">Responsive layout </td>
<td class="text-to-left"> </td>
<td class="text-to-right">No minimum </td>
<td class="text-to-right">0 </td>
<td class="text-to-right">05-Dec-2001 </td>
</tr>
</tbody>
</table>
</body>
</html>
You could collapse a table like this
CSS
#media (max-width: 1024px) {
.collapse {
display: block;
}}
HTML
<td class="collapse">
Content
</td>
<td class="collapse">
Content
</td>
I would add multiple classes with different #media (max-width: xxx) so they don't collapse all at once. How you want to collapse them, and in what order you should figur out yourself.
You can look over here https://css-tricks.com/responsive-data-tables/ if you got any more questions.
Personally I like using div over tables, e.g: http://www.responsivegridsystem.com/

Table Row Border - Half In, Half Out

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.

Color first column when there is rowspan in HTML table

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>

Table Cells lose border in Google Chrome PC

The following code, in all browsers - apart from Google Chrome Latest on the PC - displays a border on the tbody table cells, as specified in the CSS.
Chrome PC, displays the thead border, but not the TD borders. Why? Is it a bug in Chrome, or in my HTML/CSS?
Heres a jsFiddle that replicates it:
<table width="505" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>Testing</td>
<td>123</td>
</tr>
<tr>
<td>Testing</td>
<td>456</td>
</tr>
</tbody>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
table {
width:736px;
border-collapse: collapse;
thead {
border-top: 2px solid #aaaaaa;
tr {
th {
border: 0;
padding: 12px 5px;
}
}
}
tbody {
border-top:0;
tr {
td {
padding: 10px 5px;
border-top: 1px solid #aaaaaa;
border-bottom: 1px solid #aaaaaa;
}
}
Try with putting tbody after thead.
HTML
<table width="505" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>Testing</td>
<td>123</td>
</tr>
<tr>
<td>Testing</td>
<td>456</td>
</tr>
</tbody>
</table>
JSFiddle
From MDN:
thead - A table element. The thead must appear after any caption or colgroup element, even implicitly defined, but before any tbody, tfoot and tr element.
Remove border-collapse: collapse and use border-top for TD and border-bottom for TABLE.
Live demo
Try this.
table {
width:736px;
border-collapse: collapse;
}
thead {
border-top: 2px solid #aaaaaa;
}
th {
border: 0;
padding: 12px 5px;
}
tbody {
border-top:0;
}
td {
padding: 10px 5px;
border-top: 1px solid #aaaaaa;
border-bottom: 1px solid #aaaaaa;
}
I had a table using -webkit-backface-visibility:hidden;, which was hiding border color, so to fix I used -webkit-backface-visibility:visible;.