Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do not apply css style to child elements

we have

<div class="xTable">
 <table>
  <tr>
   <td>
    <div class="xTable">
     <table>
      <tr>
       <td>
        <div class="xTable">
         <table>...</table>
        </div>
       </td>
      </tr>
     </table>    
    </div>
   </td>
  </tr>
 </table>
</div>

How do I apply custom css styles to 2nd div, only to 2nd div and not to 1 or 3 or deeper?

NO WAY TO ADD EXTRA CLASSES or IDS! Html is generated dynamically and is unmanageable.

I would use

.xTable table .xTable

but that means 3rd and deeper divs will be affected.

no IDs! Please CSS selectors only.

Thanks!

like image 430
Max Avatar asked Mar 20 '13 14:03

Max


Video Answer


1 Answers

To cater fully for both this default markup and browsers which correctly add a tbody where it isn't already specified you'd need to use:

body > .xTable > table > tr > td > .xTable,
body > .xTable > table > tbody > tr > td > .xTable {
    ...
}

JSFiddle example.

This assumes that your first <div class="xTable"> has no parents other than <body>. If this isn't the case then replace body with your parent.

like image 131
James Donnelly Avatar answered Sep 18 '22 01:09

James Donnelly