2011-08-11 23 views
2

因此,这是当前的代码结构:如何使用CSS3选择器来选择每一个元素,甚至

<section> 
    <article> 
     <p>Title</p> 
     <p>Content</p> 
    </article> 

    <article> 
     <p>Title</p> 
     <p>Content</p> 
    </article> 

    <article> 
     <p>Title</p> 
     <p>Content</p> 
    </article> 
</section> 

我如何获得第一和第三个标题float:left,第二个标题float:right ... ?

我尝试这样做:

section article p:first-child:nth-child(even){ 
    float:right; 
} 

但我没有运气... :-(

+0

你能做到这一点,但你没有漂浮甚至是那些。它们自然会与float:left元素在同一行。 – jfriend00

+0

是的,我知道,但我只是试图将选择逻辑整理出来......浮动元素看起来是最简单的区别,但现在我想起来了,着色文本也会起作用。 。:-) – Abhishek

回答

4
section article p:first-child { 
    float: right; 
} 
section article:nth-child(even) p:first-child { 
    float: left; 
} 
+0

啊,是的!正是我所需要的......感谢队友.. :-)只要SO让我把你的帖子标记为答案... – Abhishek

1

我觉得想你想要的是:

section article:nth-child(odd) p:first-child { 
    float: left; 
} 

section article:nth-child(even) p:first-child { 
    float: right; 
} 

您已经实际上选择了第一个孩子<p>,这也是偶数,当然,这绝对不会发生。

+0

好啊,你真的把我放在正确的轨道上..谢谢......: - ) – Abhishek

0

它只是改变这一

section article:nth-child(even){ 
    float:right; 
} 

http://jsfiddle.net/jasongennaro/cFTDj/3/

+1

嗯......它没用......我看着你的JSFiddle的例子,所有的标题都在左边,而内容都在右边......>。< – Abhishek

+0

再看一遍@Abhishek 。我很快就必须修复一个错误。 –

相关问题