2017-08-29 4 views
0

私は2つのセクションを持っています。最初のセクションは相対的な位置を使用していて、子供がオーバーレイされている2つの絶対的な子を含んでいます。 2番目のセクションにはタイトルが含まれています。相対フローの親をドキュメントフローに保持するにはどうすればよいですか?

2番目のセクションが下に表示されるように、1番目のセクションの相対的な位置を維持したいと考えています。ポジションアブソリュートはドキュメントフローの外にある要素を取ることを理解していますが、相対的な親であってもこの場合ですか?

どうすれば親を流し続けることができますか?

.parent { 
 
    position: relative; 
 
} 
 
.child { 
 
    color: white; 
 
    position: absolute; 
 
    width: 100%; 
 
} 
 
.child1 { 
 
    background-color: red; 
 
    z-index: 1; 
 
} 
 

 
.child2 { 
 
    background-color: blue; 
 
    z-index: 0; 
 
}
<div class="parent"> 
 
    <div class="child child1">block1</div> 
 
    <div class="child child2">block2</div> 
 
</div> 
 

 
<div class="text"> 
 
    <h1>block below</h1> 
 
</div>

答えて

0

あなたはただその子は場所を取ることができるように、親要素の大きさを設定する必要があります。絶対配置された子は通常のフローから外されるので、親divには何も含まれていないので、それは「消える」ことを意味します。すなわち:

.parent { 
    position: relative; 
    width: 200px; 
    height: 200px; 
    background-color: Wheat; 
} 

とスニペット:

.parent { 
 
    position: relative; 
 
    width: 200px; 
 
    height: 200px; 
 
    background-color: Wheat; 
 
} 
 
.child { 
 
    color: white; 
 
    position: absolute; 
 
    width: 100%; 
 
} 
 
.child1 { 
 
    background-color: red; 
 
    z-index: 1; 
 
} 
 

 
.child2 { 
 
    background-color: blue; 
 
    z-index: 0; 
 
}
<div class="parent"> 
 
    <div class="child child1">block1</div> 
 
    <div class="child child2">block2</div> 
 
</div> 
 

 
<div class="text"> 
 
    <h1>block below</h1> 
 
</div>

あなたは、通常のフローを終了していない親のdivが必要な場合は、それは静的である必要があります。

.child { 
 
    color: white; 
 
    position: relative; 
 
    width: 100%; 
 
} 
 
.child1 { 
 
    background-color: red; 
 
    z-index: 1; 
 
    position: absolute; 
 
} 
 

 
.child2 { 
 
    background-color: blue; 
 
    z-index: 0; 
 
}
<div class="parent"> 
 
    <div class="child child1">block1</div> 
 
    <div class="child child2">block2</div> 
 
</div> 
 

 
<div class="text"> 
 
    <h1>block below</h1> 
 
</div>

+0

あなたの回答に感謝します。私の問題は、私のサイトが応答性であるためにpxを使用できないということです。 – Sai

+0

もちろん、親の位置を静的に設定する必要があります(つまり、位置はまったくありません)。その後、フローに戻ってくるので、機能します。私はコードスニペットを更新しています。 – Tedds

+0

私はこのプロジェクトを私のプロジェクトで実装しようとしましたが、最初の子はまだフローから親を移動して、タブでそれを実装しようとしています – Sai

関連する問題