2017-10-01 6 views
0

divs를 서로 옆에 쌓고 세 개의 하위 요소 뒤에 div를 나누기를 원합니다.div를 사용하여 브레이크 : nth-child는 적절하게

예 HTML :

<div class="wrap"> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
    <a> hello </a> 
</div> 

CSS :

.wrap a { 
    float:left; 
} 

.wrap a:nth-child(4n) { 
    clear:left; 
} 

jsFiddle를 참조하십시오. 첫 번째 행은 3 개의 하위 div가 서로 나란히 표시되어 잘 표시됩니다. 그러나 두 번째 줄에는 3을 가져야 할 때 4 개의 div가 있습니다. :nth-child을 올바르게 사용하여 3 개의 div 만 서로 쌓아 두도록하려면 어떻게해야합니까?

답변

1

사용 nth-child(3n+1)는 :

+0

아악 오른쪽, 당신이 설명해 할 수 있습니까? –

+0

은 4 원소를 모두 지우고 3n + 1을 사용하면 4,7,10 원소를 얻을 수 있습니다. clear property apply – Gattbha

1

.wrap a { 
 
    float:left; 
 
} 
 

 
.wrap a:nth-child(3n+1) { 
 
    clear:left; 
 
}
<div class="wrap"> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
</div>
설명 : 공식 (an + b) 사용. 설명 : a represents 사이클 크기, n은 카운터 (starts at 0)이고, b은 오프셋 값입니다.

.wrap a { 
 
    float:left; 
 
} 
 

 
.wrap a:nth-child(3n+1) { 
 
    clear:left; 
 
}
<div class="wrap"> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
    <a> hello </a> 
 
</div>