• 解决Div自适应高度的方法


     

    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;

     

    就是将父级div的css设置为{height:100%; overflow: hidden;}

     

  • 相关阅读:
    用SQL SERVER取分组数据第一条:查出每个班级的成绩第一名
    [转]spring beans.xml
    [转]为什么要使用框架
    MySQL 5.6 for Windows 解压缩版配置安装
    [转]hql 语法与详细解释
    [转]slf4j + log4j原理实现及源码分析
    [转]最详细的Log4j使用教程
    yii2 checkbox 的使用实例
    Magento Order 状态详解
    yii2 设置多个入口文件
  • 原文地址:https://www.cnblogs.com/Liao/p/2158374.html
Copyright © 2020-2023  润新知