I've noticed when using ~ to select the siblings, the order that the rules occur in the stylesheet is causing them to overwrite one another.
What I would like is every .test after .ColorBlue to be blue and every .test after .ColorRed to be red.
The data is going to be pulled dynamically so the order of the colors will be constantly changing.
Additionally new data can be inserted dynamically so using jquery's "nextUntil" would be pretty impractical.
What am I doing wrong?
.ColorBlue, .ColorBlue ~ tbody.test {
background: blue;
}
.ColorRed, .ColorRed ~ tbody.test {
background: red;
}
<table>
<tbody class="ColorRed">
<tr>
<td>red</td>
</tr>
</tbody>
<tbody class="test ">
<tr>
<td>test</td>
</tr>
</tbody>
<tbody class="ColorBlue">
<tr>
<td>blue</td>
</tr>
</tbody>
<tbody class="test ">
<tr>
<td>≡</td>
</tr>
</tbody>
</table>
You can use the + Adjacent sibling selector to achieve this.
The ~ General sibling combinator will select all elements matching the selector, not just the first. It won't be overwritten by the subsequent rule as it is not more specific than the first rule.
.ColorBlue, .ColorBlue + tbody.test {
background: blue;
}
.ColorRed, .ColorRed + tbody.test {
background: red;
}
<table>
<tbody class="ColorRed">
<tr>
<td>red</td>
</tr>
</tbody>
<tbody class="test ">
<tr>
<td>test</td>
</tr>
</tbody>
<tbody class="ColorBlue">
<tr>
<td>blue</td>
</tr>
</tbody>
<tbody class="test ">
<tr>
<td>≡</td>
</tr>
</tbody>
</table>
Related
I have a div that has one paragraph and one table.
I want to change the font-size of the paragraph using the :not() selector.
I tried the following, but it failed to select table element with this selector.
div :not(table){font-size:5px;}/*not working*/
<div><p>....</p><table>....</table></div>
https://codepen.io/nur49/pen/eYBvOZG
You want to select the child of the div that is not the table, so you can use the > selector on the div
div > :not(table)
Here's the snippet:
body{
width: 100%;
background-color: #7df3e0;
font-size: 24pt;
}
div > :not(table){
font-size: 5px;
}
<div>
<p>paragraph</p>
<table border="1">
<thead>
<tr class="trr">
<th>Roll</th>
<th>Name</th>
<th>GPA</th>
</tr>
</thead>
<tbody>
<tr>
<th>01</th>
<th>Nur</th>
<th>5.0</th>
</tr>
<tr>
<th>02</th>
<th>prothomalo</th>
<th>5.0</th>
</tr>
<tr>
<th>03</th>
<th>Nur</th>
<th>5.0</th>
</tr>
<tr>
<th>04</th>
<th>Nur</th>
<th>5.0</th>
</tr>
</tbody>
</table>
</div>
This might helps you.
div >*:not(table){font-size:5px;}
table.parent td:nth-of-type(1):not(table.nested td){
color: red;
}
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<tbody>
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
I have a nested table as follows -
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<tbody>
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
Requirement - Only TEXTA and TEXTB should be colored in red. In real scenario there are many rows. I want only the first td of each row in the parent table to be colored. I am doing something like -
table.parent td:nth-of-type(1):not(table.nested td){
color: red;
}
This is not giving me any result. What is the correct way of achieving this?
Spent a while playing around with this. The best I can do is to suggest using 2 lines of CSS rather than 1. One selector to do all of the first row of td and one to set the nested ones back to how they belong.
table.parent tr:first-child td {
color: red;
}
table.nested tr:first-child td {
color: black;
}
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<tbody>
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
You said that..
I want only the first td of each row in the parent table to be colored
So, I am assuming you want TEXTA and TEXTC to be colored (and not TEXTB as you stated).
If thats the case, then your idea was to select elements (first td of each row) if they dont contain a specific child element (table.nested).
This is not possible with CSS2 or CSS3.
The CSS2 and CSS3 selector specifications do not allow for any sort of parent selection.
See CSS selector - element with a given child
Edit
You can use jquery/javascript to do so.
For example, to add opacity and color css properties:
$(document).ready(function(){
$('table.parent > tbody > tr > td:first-child').each(function(){
if ($(this).has('table.nested').length == 0){
$(this).css('opacity', '0.5');
$(this).css('color', 'red');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table parent">
<tbody>
<tr>
<td>TEXTA</td>
<td>TEXTB</td>
</tr>
<tr>
<td>Has nested table below
<table class="table nested">
<thead>
<th>S.No.</th>
<th>Name</th>
<th>Contact</th>
</thead>
<tbody>
<tr>
<td>1</td>
<td>ABC</td>
<td>PQR</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>TEXTC</td>
<td>TEXTD</td>
</tr>
</tbody>
</table>
Just give some class to TEXTA & TEXTB
for example:
(html)
<td class="red-color-text">TEXTA</td>
<td class="red-color-text">TEXTB</td>
(css)
.red-color-text{color: red;}
I have a problem with table. This is my code:
<table class="employers__table">
<thead>
<tr>
<td>Таб. №</td>
<td>Сотрудник / СИЗ</td>
<td>Разм. ряд</td>
<td>Кол.</td>
<td>Ед. изм.</td>
<td>Дата выдачи</td>
<td>Дата замены</td>
<td>Списать СИЗ</td>
<td>Продлить СИЗ</td>
</tr>
</thead>
<tbody>
<tbody class="label">
<tr>
<td colspan="9">
Aaa
</td>
</tr>
</tbody>
</tbody>
</table>
.employers__table {
overflow-y: scroll;
}
.label tr > td {
background-color: #000;
color: #fff;
}
Inner <tbody> doesn't stretch. This is how it looks.
Why does this happen?
I tried to create a table with the same structure in the new project, and it worked there...I don't understand.
You are basically going against the basic semantics of HTML TABLE.
It allows you to have multiple <tbody> inside a table, but not nested tbody
<TABLE>
<THEAD>
<TR> ...header information...
</THEAD>
<TFOOT>
<TR> ...footer information...
</TFOOT>
<TBODY>
<TR> ...first row of block one data...
<TR> ...second row of block one data...
</TBODY>
<TBODY>
<TR> ...first row of block two data...
<TR> ...second row of block two data...
<TR> ...third row of block two data...
</TBODY>
</TABLE>
Corrected Snippet
.employers__table {
overflow-y: scroll;
}
.label tr>td {
background-color: #000;
color: #fff;
}
<table class="employers__table">
<thead>
<tr>
<td>Таб. №</td>
<td>Сотрудник / СИЗ</td>
<td>Разм. ряд</td>
<td>Кол.</td>
<td>Ед. изм.</td>
<td>Дата выдачи</td>
<td>Дата замены</td>
<td>Списать СИЗ</td>
<td>Продлить СИЗ</td>
</tr>
</thead>
<tbody class="label">
<tr>
<td colspan="9">
Aaa
</td>
</tr>
</tbody>
</table>
I have this code in my HTML file. I need to target second <tr> with class detail-row and within the <tr> I need to target the .k-grouping-row class for a <tr>.
How can I target this using CSS? I tried nth-child, but it didn't work with the class names.
<table>
<tr class="master-row" ></tr>
<tr class="detail-row" ></tr>
<tr class="master-row" ></tr>
<tr class="detail-row" >
<td class="k-detail-cell" colspan="5">
<div class="k-grid k-widget">
<div class="k-grid-header" >
<div class="k-grid-header-wrap >
<table role="grid">
<thead role="rowgroup">
<tr role="row">
<th class="k-group-cell k-header" scope="col"> </th>
</tr>
</thead>
</table>
</div>
</div>
<div class="k-grid-content k-auto-scrollable" style="height: 0px;">
<table role="grid">
<tbody role="rowgroup">
<tr role="row" class="k-grouping-row"></tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</table>
Using Kendo UI huh?
First things first you have a missing double quote in your code at line 10 ("k-grid-header-wrap").
Now for the CSS part, you could use nth-child like you described.
tr.detail-row:nth-child(4) .k-grouping-row{
background-color:blue;
}
As with #Johannes' answer, the nth-child is a 4 because your target is the 4th child of its parent. This means you must use that exact HTML or else the CSS will not work.
On the other hand you can use
tr.detail-row ~ tr.detail-row .k-grouping-row{
background-color:blue;
}
tr.detail-row ~ tr.detail-row ~ tr.detail-row .k-grouping-row{
background-color:inherit;
}
The ~ character looks for the next selector specified no matter what is on its way (as long as the selector is a sibling of your element).
You can use the nth-child selector, and select the k-grouping-row by its class:
tr:nth-child(2) {
color: lime;
}
.k-grouping-row {
color: blue;
}
<table>
<tr class="master-row">
<td>master row</td>
</tr>
<tr class="detail-row">
<td>detail row</td>
</tr>
<tr class="master-row">
<td>master row</td>
</tr>
<tr class="detail-row">
<td class="k-detail-cell" colspan="5">
<div class="k-grid k-widget">
<div class="k-grid-header">
<div class="k-grid-header-wrap >
<table role=" grid ">
<thead role="rowgroup ">
<tr role="row ">
<th class="k-group-cell k-header " scope="col "> </th>
</tr>
</thead>
</table>
</div>
</div>
<div class="k-grid-content k-auto-scrollable " style="height: 0px ">
<table role="grid ">
<tbody role="rowgroup ">
<tr role="row " class="k-grouping-row ">
<td>k-grouping-row</td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</table>
The CSS selector would be
tr.detail-row tr.k-grouping-row { ... }
However, you probably want to address one or all cells in that row, so you'd have to add that
Addition: If the second row also contains a .k-grouping-row element, then you'd have to be more precise:
table > tr.detail-row:nth-of-type(4) tr.k-grouping-row { ... }
I have a table in HTML which I am applying a CSS class using nth-child to give the rows alternating colors. However there are some sets of rows I would like to be the same color. If I put a tbody around these sets of rows, is there a way to specify in CSS to make rows in that tbody the same color, and then continue alternating colors? The alternative is to manually set row colors which seems like a bit too much of a hack.
So for example:
<table class="alternate-row-colors">
<tr>
<td>Blah blah blah</td>
</tr>
<tbody>
<tr>
<td>Same color as below</td>
</tr>
<tr>
<td>Same color as above</td>
</tr>
</tbody>
<tr>
<td>Continue alternating colors of rows</td>
</tr>
</table>
The only solution I can find is to break each section into separate tbody elements and apply the alternate class to the ones with the required styling.
The rows would not completely alternate as the number of rows would, I presume not be known in advance...but it's close.
.alternate-row-colors tr:nth-child(odd) {
background: #bada55;
}
.alternate-row-colors tr:nth-child(even) {
background: gold;
}
<table>
<TBODY class="alternate-row-colors">
<TR>
<TD>Monday</TD>
<TD>09/11/2000</TD>
<TD>Kelsey</TD>
</TR>
<TR>
<TD>Tuesday</TD>
<TD>09/12/2000</TD>
<TD>Lindsey</TD>
</TR>
<TR>
<TD>Wednesday</TD>
<TD>09/13/2000</TD>
<TD>Randy</TD>
</TR>
<TR>
<TD>Thursday</TD>
<TD>09/14/2000</TD>
<TD>Susan</TD>
</TR>
<TR>
<TD>Friday</TD>
<TD>09/15/2000</TD>
<TD>Randy</TD>
</TR>
<TR>
<TD>Saturday</TD>
<TD>09/16/2000</TD>
<TD>Lindsey</TD>
</TR>
<TR>
<TD>Sunday</TD>
<TD>09/17/2000</TD>
<TD>Susan</TD>
</TR>
</TBODY>
<TBODY>
<TR>
<TD>Monday</TD>
<TD>09/18/2000</TD>
<TD>Melody</TD>
</TR>
<TR>
<TD>Tuesday</TD>
<TD>09/19/2000</TD>
<TD>Christiane</TD>
</TR>
<TR>
<TD>Wednesday</TD>
<TD>09/20/2000</TD>
<TD>Symphony</TD>
</TR>
<TR>
<TD>Thursday</TD>
<TD>09/21/2000</TD>
<TD>Starflower</TD>
</TR>
<TR>
<TD>Friday</TD>
<TD>09/22/2000</TD>
<TD>Miko</TD>
</TR>
<TR>
<TD>Saturday</TD>
<TD>09/23/2000</TD>
<TD>Cleo</TD>
</TR>
<TR>
<TD>Sunday</TD>
<TD>09/24/2000</TD>
<TD>Alyx</TD>
</TR>
</TBODY>
<TBODY class="alternate-row-colors">
<TR>
<TD>Monday</TD>
<TD>09/25/2000</TD>
<TD>Dancing Star</TD>
</TR>
<TR>
<TD>Tuesday</TD>
<TD>09/26/2000</TD>
<TD>Dawn</TD>
</TR>
<TR>
<TD>Wednesday</TD>
<TD>09/27/2000</TD>
<TD>Josh</TD>
</TR>
<TR>
<TD>Thursday</TD>
<TD>09/28/2000</TD>
<TD>Ryan</TD>
</TR>
<TR>
<TD>Friday</TD>
<TD>09/29/2000</TD>
<TD>Mary Kay</TD>
</TR>
<TR>
<TD>Saturday</TD>
<TD>09/30/2000</TD>
<TD>Hallie</TD>
</TR>
<TR>
<TD>Sunday</TD>
<TD>10/01/2000</TD>
<TD>Paul</TD>
</TR>
</TBODY>
</table>
If you are wanting to use the <tbody> type of element to hold the 'same colour' blocks then using the nth-child element is easy using the direct selector > thus:
.alternate-row-colors > tr:nth-child(even) {
background-color: #A4D1FF;
}
.alternate-row-colors > tr:nth-child(odd) {
background-color: #EAF4FF;
}
This will then only effect the tr elements which are direct descendent of the class.
How about
CSS
.alternate-row-colors > tr:nth-child(odd) {
background-color: hsla(0, 0%, 0%, .2);
}
.alternate-row-colors > tr:nth-child(even), .alternate-row-colors-solid {
background-color: hsla(0, 0%, 0%, .4);
}
Or set as a third color
.alternate-row-colors-solid {
background-color: blue;
}
And if you want to group than create a table in a table i suspect is the easiest method.
HTML
<table class="alternate-row-colors">
<tr>
<td>Zebra coloring</td>
</tr>
<tr>
<td>Zebra coloring</td>
</tr>
<tr>
<td>
<table class="alternate-row-colors-solid">
<tr>
<td>Same color</td>
</tr>
<tr>
<td>same color</td>
</tr>
<tr>
<td>same color</td>
</tr>
</table></td>
</tr>
<tr>
<td>Zebra coloring</td>
</tr>
<tr>
<td>Zebra coloring</td>
</tr>
</table>
.alternate-row-colors .alternate-row-colors-solid > tr {
background-color: orange;
}
And than set the second table and its childs with width and height of 100% remove padding, margin and so on so they dont look like a sub table.