2017-12-07 14 views
1

내 코드에 문제가 있습니까? 종횡비를 padding-bottom으로 유지하는 대신 내부 div (두 개)가 외부 div를 채우는 이유는 무엇입니까? 당신이 당신의 one:before 플로트를 취소하지 않기 때문에종횡비 div 다른 종횡비 내 div 의도하지 않음

.one { 
 
    position: relative; 
 
    background: #990000; 
 
    background-size: contain; 
 
} 
 

 
.one:before { 
 
    content: ""; 
 
    width: 1px; 
 
    margin-left: -1px; 
 
    float: left; 
 
    height: 0; 
 
    padding-bottom: 40%; 
 
} 
 

 
.one:after { 
 
    content: ""; 
 
    display: table; 
 
    clear: both; 
 
} 
 

 
.two { 
 
    position: relative; 
 
    top: 0; 
 
    background: #009900; 
 
} 
 

 
.two:before { 
 
    content: ""; 
 
    width: 1px; 
 
    margin-left: -1px; 
 
    float: left; 
 
    height: 0; 
 
    padding-bottom: 20%; 
 
} 
 

 
.two:after { 
 
    content: ""; 
 
    display: table; 
 
    clear: both; 
 
}
<div class="one"> 
 
    <div class="two"></div> 
 
</div>

https://jsfiddle.net/mhk99j8z/

+0

스 니펫을 추가해 주셔서 감사합니다. – goyixeda

답변

0

그것은이다 - one:after이 오면 당신이 후 - :after가 요소의 끝에 추가 (다른 모든 요소 후)

.one { 
 
    position: relative; 
 
    background: #990000; 
 
    background-size: contain; 
 
} 
 

 
.one:before { 
 
    content: ""; 
 
    width: 1px; 
 
    margin-left: -1px; 
 
    float: left; 
 
    height: 0; 
 
    padding-bottom: 40%; 
 
} 
 
    
 
.two { 
 
    clear:left;    /* remove one:after and clear on two */ 
 
    position: relative; 
 
    top: 0; 
 
    background: #009900; 
 
} 
 

 
.two:before { 
 
    content: ""; 
 
    width: 1px; 
 
    margin-left: -1px; 
 
    float: left; 
 
    height: 0; 
 
    padding-bottom: 20%; 
 
} 
 

 
.two:after { 
 
    content: ""; 
 
    display: table; 
 
    clear: both; 
 
}
<div class="one"> 
 
    <div class="two"></div> 
 
</div>

+0

Pete에게 감사 드려요.하지만 두 번째 div가 왜 바닥에 있습니까? – goyixeda

+0

그 이유는 당신이 하나의 비율에 대해 이전의 비율을 사용했기 때문입니다. 하단의 빨간색을 원한다면, pseduo 요소가 아닌 'one'에 직접 패딩을 넣으십시오. – Pete

+0

흠, 그래서 내용을 푸시하는 중입니다 ... 좋구나. 주위에는 아무 방법도 없으므로 내부 div는 정상에 잘 앉을 것입니다. 왜냐하면 : for : for : after는 다음과 같이 변경하기 때문에 맨 위로 이동하지만 나머지 div (하나)는 표시되지 않습니다 – goyixeda