How to highlight parent table row based on table header attribute - html

I want to change the color for the entire table row (using CSS) when the table header has an attribute of commit="123". I've tried this:
[commit="123"] {
color:#aaffff;
background-color:#111111;
}
However, this only changes the table header (1). How can I select the entire table row (1 entry 1) in which the table header has commit set to "123"? Thanks!
[commit="123"] {
color: #aaffff;
background-color: #111111;
}
<table class="code">
<tbody>
<tr>
<th id="1" commit="123">1</th>
<td>
<i>entry 1</i>
</td>
</tr>
<tr>
<th id="2" commit="456">2</th>
<td>
<i>entry 2</i>
</td>
</tr>
</tbody>
</table>

Why not just apply the commit to the table rows instead of the table header? Unfortunately CSS does not currently support reverse selectors for parents.
[commit="123"] {
color: #aaffff;
background-color: #111111;
}
<table class="code">
<tbody>
<tr id="1" commit="123">
<th>1</th>
<td>
<i>entry 1</i>
</td>
</tr>
<tr id="2" commit="456">
<th>2</th>
<td>
<i>entry 2</i>
</td>
</tr>
</tbody>

Instead of adding commit to table headers (i.e.) th you need to write it in to achieve your requirement.
Hope this helps.

You could use the general sibling combinator (~) to select all following tds in the row.
This won't style the tr, there is no CSS selector that can do as you wish, but you could achieve a similar effect in this way.
[commit="123"],
[commit="123"]~td {
color: #aaffff;
background-color: #111111;
}
<table class="code">
<tbody>
<tr>
<th id="1" commit="123">1</th>
<td>
<i>entry 1</i>
</td>
<td>
<i>entry 1.2</i>
</td>
</tr>
<tr>
<th id="2" commit="456">2</th>
<td>
<i>entry 2</i>
</td>
<td>
<i>entry 2.2</i>
</td>
</tr>
</table>

Related

Bootstrap table rows does not fit headerrow (Angular)

I have a problem using Bootstrap tables in Angular:
My <td> tags does not fit the corresponding <th> from my tableheader: I am not sure, if it is caused due my calls to the template presenting the <td>:
Here is my code:
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Students</th>
<th>Links</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let c of tests">
<app-preview-container id={{c.id}} name={{c.name}} description={{c.description}} studentCount={{Count[c.id]}}"></app-preview-container>
</tr>
</tbody>
</table>
And thats the component which gets called (<app-preview-container>):
<td>
{{name}}
</td>
<td>
{{description}}
</td>
<td>
{{count}}
</td>
<td>
some buttons
</td>
Does anyone has a tip how I can fix that? I have tried a lot using Bootstrap width-params like w-xx or col-md-x or col-x or using scope="col"/"row". But none of these fixed it.
Tables often look that way when you change <tr> / <td> display property. HTML tables have their own unique display properties display: table-row; and display: table-cell;.
You either have done that or wrapped your <td>s with additional div.
You can inspect your table in the console, and check if <td>s are direct children of <tr> and then set by hand <tr> and <td> display property to table-row and table-cell.
An example of a broken table:
td {
border: 1px solid gray;
}
*[style] {
outline: 2px dashed red;
}
<table>
<tr>
<td>
Column 1
</td>
<td>
Column 2
</td>
<td>
Column 3
</td>
</tr>
<tr>
<tr style="display: block;">
<td>
Broken
</td>
<td>
Row
</td>
<td>
!
</td>
</tr>
<tr>
<td style="display: inline-block;">
Broken td
</td>
<td>
!
</td>
<td>
!
</td>
</tr>
<tr>
<td>
Correct
</td>
<td>
row
</td>
<td>
!
</td>
</tr>
</table>

How do I style the first three items of a list

i have a table,like:
<html>
<head></head>
<body>
<table class=" top-10">
<thead>
<tr>
<th>index</th>
<th>name</th>
</tr>
</thead>
<tbody>
<tr>
<td> 1 </td>
<td> john </td>
</tr>
<tr>
<td> 2 </td>
<td> mia </td>
</tr>
<tr>
<td> 3 </td>
<td> james </td>
</tr>
<tr>
<td> 4 </td>
<td> creed </td>
</tr>
<tr>
<td> 5 </td>
<td> perty </td>
</tr>
... and so on
</tbody>
</table>
</body>
</html>
This is a list
and I want the top three indexes to have different colors
It's kind of like a hot list
How do I write CSS3 styles to make this table look like this?
yes,The code for the first answer looks pretty neat, but there's one problem we seem to be missing.No matter how many pages there are, the first three have this effect.
How can index=1/2/3 have this effect?
You can use CSS nth-child:
https://css-tricks.com/useful-nth-child-recipies/
Example:
.top-10 tr:nth-child(1) td:first-child {
background-color: red;
}
.top-10 tr:nth-child(2) td:first-child {
background-color: orange;
}
.top-10 tr:nth-child(3) td:first-child {
background-color: yellow;
}

CSS need to target 2nd row of a table with a class

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 { ... }

CSS change multiple elements on hover [duplicate]

This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 6 years ago.
I want the whole row to change background-color on hover. The HTML is really old and I must leave it as it is. So I need this done purely in CSS. No JavaScript nor JQuery. Once I hover on .GMDataRow td I also need to trigger (change background) on the same element in another td. The order of elements is same in both td's.
Here's the smaller skeleton of code so you understand what im talking about:
<table>
<tr>
<td>code here</td>
<td>code here</td>
</tr>
</table>
Those two tds have identical children elements and the most important, they have same <tr class="GMDataRow">. So I was thinking maybe it can be done with :nth-child(). I need your suggestions
JSFiddle:
And here's the real skeleton:
<table class="GMMainTable" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<div class="GMBodyLeft">
<table>
<tr class="GMDataRow" >
<td>
xxx
</td>
</tr>
<tr class="GMDataRow">
<td>
xxx
</td>
</tr>
</table>
</div>
</td>
<td>
<div class="GMBodyMid">
<table>
<tr class="GMDataRow">
<td>
yyy
</td>
<td>
yyy
</td>
</tr>
<tr class="GMDataRow">
<td>
yyy
</td>
<td>
yyy
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
try this :
.hoverTable{
width:100%;
border-collapse:collapse;
}
.hoverTable td{
padding:7px; border:#4e95f4 1px solid;
}
.hoverTable tr{
background: #b8d1f3;
}
.hoverTable tr:hover {
background-color: #ffff99;
}
<table class="hoverTable">
<tr>
<td>Text 1A</td><td>Text 1B</td><td>Text 1C</td>
</tr>
<tr>
<td>Text 2A</td><td>Text 2B</td><td>Text 2C</td>
</tr>
<tr>
<td>Text 3A</td><td>Text 3B</td><td>Text 3C</td>
</tr>
</table>

How to apply CSS to td of particular table?

How to apply CSS to td of one particular table; excluding all other tables in web page?
<table class="pure-table fullWidth" id="ToBeApplied">
<tbody>
<tr class="pure-table-odd">
<td>
<label>Bank</label>
</td>
<td>
<label>Japha Bank</label>
</td>
</tr>
</tbody>
</table>
<table class="pure-table fullWidth" id="NotToBeApplied">
<tbody>
<tr class="pure-table-odd">
<td>
<label>Bank</label>
</td>
<td>
<label>Japha Bank</label>
</td>
</tr>
</tbody>
</table>
I want to apply CSS say
td {padding:23px;font-weight:bold}
i want to apply it on td's of table having ID ''ToBeApplied''.
I do not want to write a class and write same on each td of table
I do not want it to apply on td's of second table have ID ''NotToBeApplied''
How to modify HTML and CSS to achieve above?
Use CSS selectors like,
#ToBeApplied td {padding:23px;font-weight:bold}
This should work (Assuming that you dont want to specify id for table)
table.pure-table:first-child td {padding:23px;font-weight:bold}
DEMO
This is what you want.
Check out this fiddle.
#ToBeApplied td {
padding:23px;
font-weight:bold
}
Here is the snippet.
#ToBeApplied td {
padding: 23px;
font-weight: bold
}
<table class="pure-table fullWidth" id="ToBeApplied">
<tbody>
<tr class="pure-table-odd">
<td>
<label>Bank</label>
</td>
<td>
<label>Japha Bank</label>
</td>
</tr>
</tbody>
</table>
<table class="pure-table fullWidth" id="NotToBeApplied">
<tbody>
<tr class="pure-table-odd">
<td>
<label>Bank</label>
</td>
<td>
<label>Japha Bank</label>
</td>
</tr>
</tbody>
</table>
Just add style below:
<style type="text/css">
#ToBeApplied tr td{padding:23px;font-weight:bold}
</style>
One can limit the CSS application using the parent ahead of the style..
I hope this will help you achieve what you need!