The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks


          Div即父容器不根據內容自適應高度,我們看下面的代碼:
          <div id="main">
          <div id="content"></div>
          </div>
          當Content內容多時,即使main設置了高度100%或auto。在不同瀏覽器下還是不能完好的自動伸展。內容的高度比較高了,但容器main的高度還是不能撐開。

          我們可以通過三種方法來解決這個問題。 
          1增加一個清除浮動,讓父容器知道高度。請注意,清除浮動的容器中有一個空格。
          <div id="main">
          <div id="content"></div>
          <div style="font: 0px/0px sans-serif;clear: both;display: block"> </div>
          </div>

          2增加一個容器,在代碼中存在,但在視覺中不可見。
          <div id="main">
          <div id="content"></div>
          <div style="height:1px; margin-top:-1px;clear: both;overflow:hidden;"></div>
          </div>

          3增加一個BR并設置樣式為clear:both。
          <div id="main">
          <div id="content"></div>
          <br style="clear:both;" />
          </div>

          補充:
          <div id="main">
          <div id="content">
            <p>demo1</p>
            <p>demo2</p>
            <p>demo3</p>
          </div>
          </div>
          #main { border:1px solid #999999; background-color:#CCCCCC; height:100%; overflow:hidden;}
          #content { float:left;}

          以上三個方法都不是最好的解決方法,因為在程序代碼觀念中是提倡盡量不要添加無意義的標簽代碼

          介紹我的解決方法是直接在最外層div加以下樣式
          #main {
          height:100%;
          overflow:hidden;
          }
          posted on 2010-07-20 10:14 Eric_jiang 閱讀(438) 評論(1)  編輯  收藏 所屬分類: HTML

          Feedback

          # re: 解決Div自適應高度的方法 2013-10-28 18:04 ffff
          反反復復反反復復鳳飛飛  回復  更多評論
            

          主站蜘蛛池模板: 凤台县| 文成县| 齐河县| 新源县| 西林县| 桦南县| 八宿县| 阳城县| 沅江市| 林周县| 崇明县| 友谊县| 濮阳市| 万盛区| 齐河县| 于都县| 鸡西市| 江源县| 滦南县| 余姚市| 辉南县| 扶余县| 昌黎县| 兴国县| 濮阳县| 长汀县| 呼玛县| 同心县| 罗定市| 农安县| 黄陵县| 新乡县| 久治县| 安图县| 扶沟县| 青河县| 巴林左旗| 合川市| 塔城市| 崇义县| 临沂市|