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 閱讀(437) 評論(1)  編輯  收藏 所屬分類: HTML

          Feedback

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

          主站蜘蛛池模板: 泰顺县| 北安市| 瓮安县| 青浦区| 周口市| 天津市| 华亭县| 河津市| 玛纳斯县| 广水市| 勐海县| 康乐县| 余庆县| 仁化县| 上林县| 陆河县| 思茅市| 平远县| 阳谷县| 五常市| 卓尼县| 五指山市| 广德县| 白城市| 福海县| 甘泉县| 新田县| 棋牌| 措勤县| 黎川县| 班戈县| 鄄城县| 西充县| 玉田县| 亳州市| 繁昌县| 滕州市| 荥阳市| 无为县| 浙江省| 上杭县|