2011-09-20 17 views
6

私は、浮動小数点演算(例:オーバーフロー:隠しで解決する)を使用するときにdivの内容が崩壊することに対処しましたが、絶対/相対位置を学習しようとしていて、コンテナdivが折り畳まれている理由を把握できません。テストケース:divが相対位置/絶対位置で崩壊するのはなぜですか?

<html> 
    <head> 
    <style type="text/css"> 
     body { 
     background-color:#eee; 
     } 

     #content { 
     margin:0 auto; 
     position:relative; 
     border:1px solid red; 
     width:800px; 
     display:block; 
     background-color:white; 
     } 

     #header { 
     border:1px solid black; 
     background-color:#777; 
     color:white; 
     width:800px; 
     position:absolute; 
     left:0; 
     top:0; 
     } 

     #leftcol { 
     position:absolute; 
     border:1px solid black; 
     background-color:#ddd; 
     width:200px; 
     top:100px; 
     left:0; 
     } 

     #rightcol { 
     position:absolute; 
     top:100px; 
     left:205px; 
     border:1px solid black; 
     background-color:#ddd; 
     width:500px; 
     } 

    </style> 
    <title>CSS Positioning Example 1</title> 
    </head> 

    <body> 
    <div id="content"> 

     <div id="header"> 
     <h1>The Awesome Website</h1> 
     </div> 

     <div id="leftcol"> 
     <h2>About</h2> 
     <p> 
     This website is so awesome because it was made by someone 
     and that is really all there is to it. There. 
     </p> 
     </div> 

     <div id="rightcol"> 
     <p>This is where I'm going to put some real body text so that it goes 
     on and on for a while and so I can get a sense of what happens when 
     the text in the paragraph keeps going and the box containing it keeps 
     going on as well. 
     </p> 
     </div> 

    </div> 

    </body> 
</html> 

ここでは何が起こっていますか?赤いボーダーのコンテンツdivが他のdivを含んでいても崩壊するのはなぜですか?

+0

JSBin:http://jsbin.com/exukoc – Mohsen

答えて

8

すべてのコンテンツのスタイルがposition:absoluteであるためです。これは、それらの要素を流れから外して(レイアウト上)存在しないようにします。コンテンツの配置にはposition:relativeを使用することを検討してください。

+0

@sscirrusのスペルと文法の訂正については、こちらをご覧ください。遅いです。 :P –

+0

私は絶対配置された要素を使用することのポイントは、それらを相対的な位置の祖先の中に置くことだと思いましたか?位置ではない:その祖先に対する絶対的な相対位置? – mix

+0

@Joseph - もちろん!ニースの答え、そして私の+1。 – sscirrus

3

あなたは本当に赤いボーダーを持つdiv要素は、それの内容に展開されませんなぜあなたの質問は別に一覧

CSS Positioning 101

CSS Floats 101

でこれらの記事を読む必要があります。 Josephが言ったように、問題はドキュメントフローから要素を取り除くことです。要素を配置すると、要素の位置が親要素および兄弟要素から独立したものになります。

CSS float propertyを使用してコードを修正しました。見てくださいhere

これらの記事を読むことを強くお勧めします。

+0

thx。完全に助けられた。 – mix

関連する問題