【CSS】div父容器根據子容器大小自適應

來源:互聯網
上載者:User

標籤:style   blog   ar   color   sp   on   div   問題   log   

Div即父容器不根據內容自動調節高度,我們看下面的代碼:

 

1 <div id="main">  2 3 <div id="content"></div>  4 5 </div>  

 

 

 當Content內容多時,即使main設定了高度100%或auto。在不同瀏覽器下還是不能完好的自動伸展。內容的高度比較高了,但容器main的高度還是不能撐開。


  我們可以通過三種方法來解決這個問題。

  一,增加一個清除浮動,讓父容器知道高度。請注意,清除浮動的容器中有一個空格。

1 <div id="main">  2 3 <div id="content"></div>  4 5 <div style="font: 0px/0px sans-serif;clear: both;display: block"> </div>  6 7 </div>   

 

   二,增加一個容器,在代碼中存在,但在視覺中不可見。

1 <div id="main">  2 3 <div id="content"></div>  4 5 <div style="height:1px; margin-top:-1px;clear: both;overflow:hidden;"></div>  6 7 </div>   

 

  三,增加一個BR並設定樣式為clear:both。

<div id="main">  <div id="content"></div>  <br style="clear:both;" />  </div>  

 

 

【CSS】div父容器根據子容器大小自適應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.