2012-09-12 34 views
0

說我有這種形式的表第一​​元素:如何風格,是包含在形式

<form> 
    <table> 
    <tbody> 
     <tr> 
     <td style="font-family : Arial"> 
      Sample text 
     </td> 
     </tr> 
    </tbody> 
    </table> 
</form> 

我如何添加附加新的樣式,只是第一個這種形式的,因爲我沒有其ID,這裏是我到目前爲止有:

form table tbody tr td{ 
    font-family : Arial; 
    font-weight: bold; 
} 

回答

1

我會根據這裏的其他答案使用第一個孩子,但是您不一定需要對CSS規則的其餘部分做出如此特定的規定。例如下面的規則應該足夠了:

form td:first-child { 
    font-family: Arial; 
    font-weight: bold; 
} 

不僅將你的CSS更容易理解和調試,但它會更簡單重寫此規則如果您需要,無需獲得與其他規則尤伯杯具體。

5

使用first-child如果你希望你的規則只適用於其父的第一個孩子:

form table tbody tr td:first-child { 
    font-family : Arial; 
    font-weight: bold; 
} 

Reference from the MDN

+1

+ 1爲更好的瀏覽器支持(CSS 2)比n-child(CSS3) – Smamatti

2

嘗試,

form table tbody tr td:first-child{ 
    font-family : Arial; 
    font-weight: bold; 
} 
0

在你的CSS,你必須先用胎

form table tbody tr td:first-child{ 
font-family : Arial; 
font-weight: bold; 
} 
0

您可以將classtd,然後部署你的CSS規則到class

<form> 
    <table> 
    <tbody> 
     <tr> 
     <td class="firstChild"> 
      Sample text 
     </td> 
     </tr> 
    </tbody> 
    </table> 
</form> 

form table tbody tr td.firstChild{ 
    font-family : Arial; 
    font-weight: bold; 
} 

僞選擇:first-child是不錯的選擇,但不是所有的瀏覽器版本的支持。

See bottom table

+1

有什麼理由使用類而不是使用僞':first-child'嗎? –

+0

@Vega只爲瀏覽器戰鬥... :)安全的方式.. – thecodeparadox

0

您可以使用CSS first-child選擇,如:(注意嫡系選擇,這隻會在表單的第一個表中選擇在每一個tr第一td

CSS:

form > table > tbody > tr > td:first-child { 
    font-family: Arial; 
    font-weight: bold; 
}​ 

演示:http://jsfiddle.net/SO_AMK/hVChC/