2014-02-23 27 views
1

我正在尝试制作一个包含标题,第一段和第二段的框。我如何才能选择CSS中的最后一段,以便将其向下移动,因此它不会与第一段重叠?这是我的代码:你要找的:last-child如何在CSS中只选择一个子元素?

报价the specification

#info-box > p { 
    line-height: 1.3em; 
    font-size: 17px; 
    font-family: Arial; 
    text-align: center; 
    color: #979797; 
    margin-left: 20px; 
    margin-right: 20px; 
    margin-top: 0px; 
    position: absolute; 
} 

#info-box:nth-child(2) { 
    line-height: 1.3em; 
    font-size: 17px; 
    font-family: Arial; 
    text-align: center; 
    color: #979797; 
    margin-left: 20px; 
    margin-right: 20px; 
    margin-top: 100px; 
    position: absolute; 
} 
+0

你有什么浏览器支持? –

+4

为什么你使用绝对定位开始? – Nit

+1

你也可能不应该在ID上使用:nth-​​child,因为这意味着你可能在多个元素上使用相同的ID。只需使用一个班级并选择它即可。 – thesublimeobject

回答

1

:last-child伪类表示一些其他元素的最后一个子元素。

Here's an example

div { 
    width:100px; 
    height:100px; 
    background-color:red; 
    border:solid; 
    margin:2px; 
    display:inline-block; 
    border-width:1px; 
} 
div:last-child{ 
    background-color:white; 
} 

虽然是公平的,绝对位置很少 “的方式” 就像尼特暗示的意见。像素中的固定尺寸在不同的屏幕尺寸和不同的缩放环境下效果不佳,更喜欢更合理的布局。关于为什么,请参阅this question

0

CSS的最后一个元素,试图在这里使用:last-child

#info-box:last-child { 
} 

检查参考CSS选择器 http://www.w3schools.com/cssref/css_selectors.asp

+0

引用[规范](http://www.w3.org/TR/selectors/#selectors)而不是W3Schools通常会更好。 w3schools与w3c没有任何关系(尽管他们喜欢这种方式,当他们认为这种方式时,他们通常不是一个好资源)(http://w3fools.com)。 –

相关问题