How to select first td of first two rows using css - html

It's a complicated one I think. Let's see if I'm wrong.
I want to select only the td which are marked as "// to be selected"
I dont want to apply styles for any other td other than the marked ones.
Please help!
<table>
<tbody>
<tr>
<td> // to be selected
<div>
<div>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
<tr>
<td> // to be selected
<div>
<div>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
<tr>
<td> // to be selected
<div>
<div>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>
Thanks in Advance!

Use td to set styles to all td elements and use td td to reset the styles to the nested td elements.
td {
// styles
}
td td {
// styles to reset the nested elements
}

As suggested before (and in my opinion the best option), you can add a class to the td elements you want to select, you can also select them using specificity by using the path to those elements.
CSS
table tbody tr td{
/* your code here */
}

var table = document.getElementsByTagName('table')[0]
let tbody = table.children[0];
let trs = tbody.children
for (let i = 0; i < trs.length; i++){
var tdhtml = trs[i].getElementsByTagName('td')[0].innerHTML
trs[i].getElementsByTagName('td')[0].innerHTML ='selected' + tdhtml
}
<table>
<tbody>
<tr>
<td> 1<!--to be selected-->
<div>
<div>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
<tr>
<td> 2<!--to be selected-->
<div>
<div>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
<tr>
<td> 3 <!-- to be selected-->
<div>
<div>
<table>
<tbody>
<tr>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</td>
</tr>
</tbody>
</table>

You would need to apply a CSS class to the first TD of each row in the highest level table, as follows:
/* Set all root TR's and the first TD element */
table>tbody>tr>td:first-child {
background-color: yellow;
}
/* Reset so that this doesn't cascade */
table>tbody>tr>td:first-child td {
background-color: inherit;
}
Example here:
https://jsfiddle.net/0utqvhy6/

Related

Inner table adds 1-2 pixels of padding on the left

Let's say I have a table:
<table>
<tr>
<td>
Hello How are you this is a TEST!
</td>
<tr>
<tr>
<td>
<table>
<tr>
<td>
Hello this a test 2!
</td>
</tr>
</table>
<td>
<tr>
</table>
Just as an example, I have one TD in my main table, however I need to do 3 TD in my child table. If i do it as I shown above, it looks like this.
Hello How are you this is a TEST!
Hello this is a Test2!
The inner table gives me a padding-left of about 1-2 pixels. Is there any way to line up both statements?
Collapse your borders and remove your cell padding. Also you have a few td and tr tags improperly closed.
https://developer.mozilla.org/en-US/docs/Web/CSS/border-collapse
table {
border-collapse: collapse;
}
table td {
padding: 0;
}
<table>
<tr>
<td>
Hello How are you this is a TEST!
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>
Hello this a test 2!
</td>
</tr>
</table>
</td>
</tr>
</table>
Sure. That space is due to two factors:
Table cells, by default, have space between them. To collapse cells together, you need to apply border-collapse: collapse to the parent table.
Table cells have 1px of padding by default. You'll need to get rid of that by doing td { padding: 0; }
table {
border-collapse: collapse;
}
table td {
padding: 0;
}
<table>
<tr>
<td>
Hello How are you this is a TEST!
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td>
Hello this a test 2!
</td>
</tr>
</table>
</td>
</tr>
</table>
If you'd like a more visual example, you can check out this JSFiddle.
Try this:
<style>
table {
border-spacing:0;
}
td {
margin-left:0px;
padding-left: 0px;
}
</style>
...
<table>
<tbody>
<tr>
<td>
Hello How are you this is a TEST!
</td>
</tr>
<tr>
<td>
<table>
<tbody>
<tr>
<td>
Hello this a test 2!
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>

Why doesn't my direct descendant selector work?

I have a problem with my direct descendant selector. Look at a simple example:
.myDiv > table tr:first-child td {
font-weight: bold;
text-align: center;
}
<div class="myDiv">
<table style="width:100%">
<tr>
<td style="width:37%">Revenue & Cost</td>
<td style="width:43%">Name</td>
<td style="width:20%">Income</td>
</tr>
<tr>
<td>column 1</td>
<td colspan="2">
<table id="tableChild" width="100%">
<tr>
<td>child 1 - Should NOT bold</td>
<td>child 2 - Should NOT bold</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
As you can see, it will effect table id tableChild. I expect to get a bold font on the first row on the first table.
Here is my JSFiddle
First, finish defining the table correctly:
<table>
<thead>
<tr> TITLE ROW HERE </tr>
</thead>
<tbody>
CONTENT ROWS HERE
</tbody>
</table>
Then your CSS selector becomes:
.myDiv>table>thead>tr>td {
...
}
The browser fills in your missing table elements:
Try this:
.myDiv > table > tbody > tr:first-child td
https://jsfiddle.net/85t8qm5r/3/

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>

Prevent an HTML sub-table from getting cascade style

If I have the HTML below I want to find a single CSS class definition that will effect the outermost table but not the sub tables. Without changing the HTML can I get .myClass to do this:
I was playing around with the not selector but couldn't get it to work.
.myClass tr td div :not(table) {
background-color: red;
}
<body>
<div class="myClass">
<table>
<tr>
<td>
<div>My</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>Hello</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>World</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</body>
No you cannot do this with pure css without changing your html.
You can do it with jQuery or by simply putting the 'My' inside of a span, because text is not accesible by a selector.
But in all fairness your question was partly answered by #Pangloss. To access the outer table, just use >. Your problem lies in the fact that on the 2nd and 3rd rows, you still have the outer table present. Your question should actually be something like "What's the selector for an element that does not have a specific child type", and then you would find that you cannot target a parent of an element on css yet.
The first cell is the first div inside the first tr, we can target this using the following:
tr:first-child div:first-child{
background:red;
}
Full snippet:
tr:first-child div:first-child {
background: red;
}
<body>
<div class="myClass">
<table>
<tr>
<td>
<div>My</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>Hello</td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>World</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</body>

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!