2012-12-16 3 views
0

내 상황에서는 :last-child이 작동하는 데 문제가 있습니다.마지막 자식이 잘못된 div를 선택했습니다.

HTML :

<div class="test">Some content</div> 
<div class="test">Some content</div> 
<div class="test">Some content</div> 
<div class="action">Some other content</div> 

CSS :

div.topic { 
    border-bottom: 1px solid black; 
} 

div.topic:last-child { 
    border-bottom: none; 
} 

JSFiddle

마지막 div.test 여전히 하단의 테두리가 있습니다. 내가 생각하는 문제는 div.action 인 마지막 div에 적용되고 클래스 테스트의 마지막 div가 아니라는 것입니다.

어떻게 해결할 수 있습니까?

+2

읽어 보시기 바랍니다. http://stackoverflow.com/questions/6401268/how-do-i-select-the-last-child-with-a-specific-class-name-in-css 질문 가능하다면 복제본입니다. 짧은 답에는'last-of-class' 셀렉터가 없으므로 JavaScript를 사용하거나 [border-top'] (http://jsfiddle.net/97Dr4/1/)을 사용해야합니다. –

+0

귀하가 링크 된 질문에 대한 해결책은 절대 주문 (예 : 4 번째 항목)을 사용하는 반면 내 div는 서버에 의해 생성되며 몇 개가 있는지 모릅니다. – Eleeist

+0

oops, 잘못된 탭 XD에서 복사 한 원본 설명을 수정하고 복사 할 링크로 변경했습니다. 혼란을 드려 죄송합니다. –

답변

2

원하는 결과를 얻으려면 마크 업을 업데이트해야합니다. 예를 들어, 래퍼에 알 수없는 항목을 랩 :

Working DEMO

<div id="list"> 
<div class="topic">Some content</div> 
<div class="topic">Some content</div> 
<div class="topic">Some content</div> 
</div> 
<div class="action">Some other content</div>​ 

다음이 CSS를 사용

#list .topic { 
border-bottom: 1px solid black; 
} 

#list .topic:last-child { 
border-bottom: none; 
} 
+0

나는 이걸 가지고 가야 할 것 같아. 나는이 선택기가 이런 방식으로 작동한다는 것에 놀랐다 (즉, 클래스를 무시한다 ...). – Eleeist

0

내가 first-child하지 last-child 사용하는 것이 좋습니다 것입니다.

div.topic { 
    border-top: 1px solid black; 
} 

div.topic:first-child { 
    border-bottom: none; 
} 

이것은 현재 HTML에서 작동하며 브라우저 간 호환이 가능합니다.

+0

더 많은 브라우저 간 호환이 가능합니까? ': first-child'도': last-child'처럼 지원됩니다. 둘 모두 구형 브라우저와 IE에서는 작동하지 않습니다. – Horen

+0

@Horen first-child는 IE8에서 작동하지만 last-child는 작동하지 않습니다. http://jsfiddle.net/JT5SN/3/. (참조 http://www.w3schools.com/css/css_pseudo_classes.asp) –

+0

내가 여기 찾고 있었다 http://reference.sitepoint.com/css/pseudoclass-firstchild – Horen

관련 문제