2017-12-07 12 views
1

私のコードに間違いがありますか?アスペクト比をpadding-bottomにするのではなく、内側のdiv(2つ)が外側のdivを埋めるのはなぜですか?アスペクト比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:beforeフロートをクリアしていないので、それはだ - :after(他のすべての要素の後に)要素

の最後に追加 - 2は、後に one: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

もう一度ピートに感謝しますが、なぜ2番目のdivが底にあるのですか? – goyixeda

+0

これは、赤を下にしたい場合には、あなたの比率に対してbeforeを使用したためです。pseduo要素ではなく、「one」に直接パディングを入れてください。 – Pete

+0

よかった。それで周りには道がないので、内側のdivは上にきれいに座っていますか?私は:afterのために:beforeを変更するので、それはトップに行きますが、divの残りの部分は表示されません – goyixeda

関連する問題