2013-11-28 46 views
0

所以我试图将一个特定的样式应用于仅在类名为.one_third的页面上发生的第一类。我有三节课,三节课,因此共有9门课程。我试图只做第一个,左上角是黑色,背景是黑色。如何将样式应用于仅发生的第一类?

我试图用伪类创建它自己,我想出了这个:

.one_third:first-child 

虽然这所做的所有最左边的类黑,而不只是左上角之一。

然后,我试了一下,将其应用于我的content div,该div包含所有类,但未能做任何事情。

#content > .one_third:first-child 

我怎么能做到这一点?

编辑: 这是我确切的HTML标记:

enter image description here

+3

显示HTML标记。 – Hiral

+0

如果它适合你,你可以试试这个:'.one-third:first' –

+2

Typo?你的目标是'.one-third',但divs有一类'one_third'。更正它工作正常:http://jsfiddle.net/j08691/y5TB3/ – j08691

回答

0

例如,

<div class="jey"> 
    <p>Number 1</p> 
    <p>Number 2</p> 
    <p>Number 3</p> 
</div> 
<div class="jey"> 
    <p>Number 1</p> 
    <p>Number 2</p> 
    <p>Number 3</p> 
</div> 

将样式

.jey:first-of-type{ 
    background-color:Red; 
    } 

看到这个http://jsfiddle.net/jeyashri_shri/Td8bx/

,这一次在你的代码

#content .one_third:first-child { 
color:red; 
} 

但首先,孩子在IE工作还工作,一个'<!DOCTYPE>'必须申报。
它在IE7 +工作,但不是在IE6或更低

+0

仍使左侧的所有类都具有背景,而不仅仅是第一次出现。 – Fizzix

+0

ok.wait.I将给出另一个解决方案 –

+0

。one_third:first-of-type {background_color:Red; } –

0

变化:

<div class="one_third> //you forgot to add closing quote 

<div class="one_third"> 

.one-third:first-child //class name is incorrect 

.one_third:first-child 

Fiddle here.

+0

Hiral,请重新检查我的帖子。自从我忘记我的班级有下划线,而不是连字符,我有一些错字。 – Fizzix

+0

@fizzix使小提琴。 – Hiral

+0

@downvoter fizzix在我回答后更改了他的帖子。任何原因downvoting ?? – Hiral

0

您可以检查以下任何一种,如果其中一人对你的作品:

.clear div.one_third:first-child{ 
    color:red; 
} 

.columns div.one_third:first-child{ 
    color:red; 
} 

正如你已经使用了这一点:#content div.one_third:first-child{color:red;}这就是为什么我把那些。检查并让我知道。

+0

不幸的是,你的两个解决方案似乎仍然使最左边的所有类都有风格,而不仅仅是第一个。 – Fizzix

+0

好的。让你的完整代码jsfiddle。 –

相关问题