Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS selector to find the first tbody

My HTML code

<div id="myelement">
    <table class="myclass">
       <tbody>
           <tr>
               <td>something</td>
               <td>
                   <table>
                       <tbody>
                         <tr> hari </tr>
                       </tbody>
                   </table>
               </td>
           </tr>
           <tr>
              foo
           </tr>
       </tbody>
    </table>
</div>

Xpath solution

"//tbody[1]"

Problem

I am looking for a CSS expression which should select first tbody which is a direct child of table, not the one inside tr.

If I use the CSS as tbody, then it would select 2, but I am looking for a way to fix it here. I know table>tbody will work, I am looking for if any more is there or not. As in my case I can't use table>tbody.

like image 456
Arup Rakshit Avatar asked Dec 26 '22 21:12

Arup Rakshit


2 Answers

 tbody tr td:first-of-type {
     color: red;
 }

DEMO

td:first-of-type will works too.


:nth-of-type(1) and :first-of-type are the same. Docs

like image 170
daniel__ Avatar answered Dec 29 '22 08:12

daniel__


Try using the immediate child selector >:

.myclass > tbody

Or if you just want the first one inside that div, you can do:

#myelement:first-child tbody
like image 20
mattytommo Avatar answered Dec 29 '22 09:12

mattytommo