2017-09-14 6 views
1

2 열 레이아웃을 만들고 각 행의 첫 번째 항목을 왼쪽으로 떠올리게 할 때 문제가 발생했습니다. 요소의 텍스트가 의도 한 것보다 훨씬 더 큰했다 경우, 행 항목이 아래로 밀려 때문에, 대신에이 같이 찾고, 휴식 것이 :"clear : both;"를 추가하는 이유는 무엇입니까? 떠 다니는 항목 행이 콘텐츠 푸시 오버와 관련된 문제를 해결하면?

How it looks without clear: both;

:

How the layout should look

은 다음과 같습니다

코드의 존재 :

<ul class="layout"> 
<li> 
    <article> 
     <div class="thumb"> 
      <img class="img" alt="" src=""> 
     </div> 
     <div class="post-info"> 
      <h3>Hey there! You should make me bigger to break the layout.</h3> 
      <p>Some text here...</p> 
     </div> 
    </article> 
</li> 
<li> 
    <article> 
     <div class="thumb"> 
      <img class="img" alt="" src=""> 
     </div> 
     <div class="post-info"> 
      <h3>Hey there! You should make me bigger to break the layout.</h3> 
      <p>Some text here...</p> 
     </div> 
    </article> 
</li> 
<li> 
    <article> 
     <div class="thumb"> 
      <img class="img" alt="" src=""> 
     </div> 
     <div class="post-info"> 
      <h3>Hey there! You should make me bigger to break the layout.</h3> 
      <p>Some text here...</p> 
     </div> 
    </article> 
</li> 
<li> 
    <article> 
     <div class="thumb"> 
      <img class="img" alt="" src=""> 
     </div> 
     <div class="post-info"> 
      <h3>Hey there! You should make me bigger to break the layout.</h3> 
      <p>Some text here...</p> 
     </div> 
    </article> 
</li> 

CSS :

,536,
.layout { 
    list-style: none; 
} 

.layout > li { 
    vertical-align: top; 
    width: 49%; 
} 

.layout > li:nth-child(odd) { 
    margin-right: 1%; 
    float: left; 
} 

.layout > li:nth-child(even) { 
    margin-left: 1%; 
    float: right; 
} 

어느 것이 효과가 있지만 길게 만들면 깨질 수 있습니다.

우리는이 라인을 추가하는 경우 :

.layout > li:nth-child(even) + li { 
    clear: both; 
} 

그것은 상관없이 내가 던질 것, 바로 완벽하게 작동하지 않습니다.

어째서?

+0

을 포함하는 이미지를 편집 할 수있는 명성을 가진 사람을 감사하겠습니다. –

+0

'float'을 추가하면 컨텍스트가 종료되었다고 말하는 * clear *되어야하는 새로운 * 블록 형식화 컨텍스트가 생성됩니다. [this] (https://developer.mozilla.org/en-US/docs/)를 참조하십시오. 웹/가이드/CSS/Block_formatting_context) – kukkuz

+0

@kukkuz 이해합니다! 그러나 나는 어떻게 떠돌아 다닐 수 있습니까? 왼쪽과 맑은 것 : 둘다; 동일한 항목에 대해 왜 3 단 (첫 번째 행) 요소를 모두 지우면 정확히 작동합니까? –

답변

0

케이스의 모든 요소에 float: left 만 사용하면됩니다.

.layout { 
 
    list-style: none; 
 
} 
 
.layout>li { 
 
    vertical-align: top; 
 
    width: 49%; 
 
    float: left; 
 
} 
 

 
.layout>li:nth-child(odd) { 
 
    margin-right: 1%; 
 
} 
 

 
.layout>li:nth-child(even) { 
 
    margin-left: 1%; 
 
}
<ul class="layout"> 
 
    <li> 
 
    <article> 
 
     <div class="thumb"> 
 
     <img class="img" alt="" src=""> 
 
     </div> 
 
     <div class="post-info"> 
 
     <h3>Hey there! You should make me bigger to break the layout.</h3> 
 
     <p>Some text here...</p> 
 
     </div> 
 
    </article> 
 
    </li> 
 
    <li> 
 
    <article> 
 
     <div class="thumb"> 
 
     <img class="img" alt="" src=""> 
 
     </div> 
 
     <div class="post-info"> 
 
     <h3>Hey there! You should make me bigger to break the layout.</h3> 
 
     <p>Some text here...</p> 
 
     </div> 
 
    </article> 
 
    </li> 
 
    <li> 
 
    <article> 
 
     <div class="thumb"> 
 
     <img class="img" alt="" src=""> 
 
     </div> 
 
     <div class="post-info"> 
 
     <h3>Hey there! You should make me bigger to break the layout.</h3> 
 
     <p>Some text here...</p> 
 
     </div> 
 
    </article> 
 
    </li> 
 
    <li> 
 
    <article> 
 
     <div class="thumb"> 
 
     <img class="img" alt="" src=""> 
 
     </div> 
 
     <div class="post-info"> 
 
     <h3>Hey there! You should make me bigger to break the layout.</h3> 
 
     <p>Some text here...</p> 
 
     </div> 
 
    </article> 
 
    </li>
명확의

0

응용 프로그램, 그렇지 않으면 인해 이전 또는 상속 스타일에 적용되었다했을 현재 컨테이너의 수레을 지 웁니다.

이제 이러한 요소에 적용되는 레이아웃은 고유 한 레이아웃입니다. 그래서 원하는대로 작동합니다. 레이아웃에 대한

https://developer.mozilla.org/en/docs/Web/CSS/clear

, 당신은 다음 왼쪽으로 BOX2, 왼쪽에 상자 하나를 떠. 그런 다음 명확하게 적용하십시오 : 두 가지 모두 부동 적용을 중지하려는 경우입니다. 그런 다음, 다음 행 상자의 경우 왼쪽으로 배치하기 때문에 다시 부동 상자를 적용합니다.

"When you apply clear:both to an element, it basically says "stop floating here" — this element and those after it in the source will not float, unless you apply a new float declaration to another element later on."