2015-01-08 32 views
0

我想使用css選擇器選擇我的表格的第一個td。 這是我的Html代碼。如何選擇特定div內的表格中的第一個TD

<div class="mydiv"> 
    <table class="mytable"> 
     <tbody> 
      <tr> 
       <td></td> 
       <td></td> 
       <td></td> 
       <td></td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

這裏是我的CSS代碼 第一次嘗試。

div.mydiv > table > tbody > tr > td 
    { 
     width:25px ; 
    } 

第二次嘗試

div.mydiv > table > tr > td 
{ 
    width:25px ; 
} 

看來他們都不工作。

問候。

+1

請留下您的評論之前ü投下來的問題,它可以幫助我提高的問題。 –

回答

1

既然你只有一個元素.mydiv表只是用:first-of-typenth-child(1):first-child

div.mydiv td:first-of-type { 
 
    width:25px 
 
}
<div class="mydiv"> 
 
    <table class="mytable"> 
 
     <tbody> 
 
      <tr> 
 
       <td>a</td> 
 
       <td>b</td> 
 
       <td>c</td> 
 
       <td>d</td> 
 
      </tr> 
 
     </tbody> 
 
    </table> 
 
</div>

在這裏沒有必要使用!important這是不好的做法。

enter image description here

1

試試這個:

div.mydiv > table > tbody > td:first-of-type { 
    width:25px !important 
} 
+0

'... tr> td:first-child'具有更好的兼容性。這當然假設OP不需要行中的「th」。 –

+0

確切的,在這種情況下,他只會改變選擇器,以通過其行中的thead – kitensei

1
div.mydiv > table > tbody > tr > td:first-child{ 

} 

這將選擇tr的第一個孩子是td。這應該給你你需要的東西。
希望這有助於

0

試試這個:

table.myTable > tbody > tr:first-child > td:first-child { 
    width:25px !important; 
} 

編碼快樂!

相關問題