子标签float后溢出父标签问题的解决

子标签float后溢出父标签的解决

1 第一种方法:给父标签加overflow:auto;

1.1 IE7, IE8, FF

在父标签加一句:

overflow: auto;

1.2 IE6

1.2.1 css hack?

later

果然是: 针对IE7、IE8和火狐,可以给父标签加上overflow:auto;样式,而加上这个样式后 ,IE6并没有什么变化,我们都知道,IE6在解释height的时候,是按最小高度解释的, 只要设定了height为固定数值,超出这个数值后,IE6可以自动适应,由以上分析, 源码样式部分修改如下:

ul {list-style:none;margin:0;padding:0;}                                      
#div_01 {background:red;overflow:auto;}                                       
*html #div_01 {height:1px;overflow:visible;}                                  
#div_01 div {float:left;padding-left:10px;}                                   
#div_02 {width:300px;height:100px;background:blue;}                           

其中,*html是筛选出IE6专属css的前缀,见:

.main{ float:left;#float:none;float:none;
html*.main{ float:left;#float:none;float:none; }
*+html .main{ float:left;#float:none;float:none; }
*html .main{ float:left;#float:none;float:none; }

第1行给Firefox以及其他浏览器看 第2行给safari/IE6/iE7看,如果safari/IE6/iE7 视觉效果不统一 , 就要在后面跟IE6/IE7的定义 第4行给IE6以及更老的版本看

来自: 百度经验

2 第二种方法:加入平等div,再clear:both;

如果父标签除了float的子标签(无论几个),没有其他不float的标签,就会发生溢出,因为父标签不知道如何定位自己,这时加入一个空的clear:both;标签即可。

见:

/*CSS file*/
<style type="text/css">
<!--
.clear-float {
clear: both;
}
-->
</style>

/*HTML file*/
<body>
<div id="parent-div">
<div id="child-div"></div>
<div id="child-div"></div>
<div class="clear-float"></div>
</div>
</body>

原文:子标签div使用float,父标签div如何自适应高度。

3 第三种方法:父标签也float,父标签的父标签(爷标签?)overflow:auto;

见:

/* CSS style part*/
<style type="text/css">
/*注意,最“父”container没必要float也没overflow:auto;*/
#container {
    border: 4px solid gray;
    margin: 0 auto;
    width: 800px;
}

#left {
    overflow: hidden;
    width: 550px;
}

#leftre {
    float: left;
    left: 2%;
    position: relative;
    width: 47%;
}

#rightre {
    float: right;
    position: relative;
    right: 2%;
    width: 47%;
}

.individual {
    background-color: #E1D697;
    border: 2px solid gray;
    float: left;
    font-size: 8pt;
    font-weight: bold;
    padding: 8px;
    width: 240px;
}

.information {
    float: left;
    margin-bottom: 3em;
    width: 240px;
}




</style>

/*HTML part*/
<div id="container">
    <div id="left">
        <div id="review">
                        <div id="leftre">
                                <p class="individual">
                                        <q>Ditching the cheeky, self-aware wink that helped to excuse the concept's inherent corniness, the movie attempts to look polished and 'cool,' but the been-there animation can't compete with the then-cutting-edge puppetry of the 1990 live-action movie.</q>
                                </p>
                                <p class="information">
                                        Peter Debruge <br>
                                Variety
                                </p>
                                <p class="individual">
                                        <q>TMNT is a fun, action-filled adventure that will satisfy longtime fans and generate a legion of new ones.</q>
                                </p>
                                <p class="information">
                                        Todd Gilchrist <br>
                                        IGN Movies
                                </p>
                        </div>

                        <div id="rightre">
                                <p class="individual">
                                        <q>The striking use of image and motion allows each sequence to leave an impression. It's an accomplished restart to this franchise.</q>
                                </p>
                                <p class="information">
                                        Mark Palermo <br>
                                        Coast (Halifax, Nova Scotia)
                                </p>
                                <p class="individual">          
                                        <q>The script feels like it was computer generated. This mechanical presentation lacks the cheesy charm of the three live action films.</q>
                                </p>
                                <p class="information">
                                        Steve Rhodes <br>
                                        Internet Reviews
                                </p>
                        </div> /* end of div-"rightre" */
                </div>/* end of div-"review" */
        </div>/* end of div-left */
</div>/* end of div-container */
### CSS Float 布局中父元素高度塌陷的解决方案 在CSS布局中,`float`属性常用于实现复杂的页面布局。然而,当子元素应用了浮动后,可能会导致其父元素的高度塌陷问题。以下是几种有效的解决方案: #### 方法一:使用清除浮动(Clearfix) 通过定义一个通用的clearfix样式,可以在容器上应用该样式以解决高度塌陷问题。这种方式无需修改HTML结构,仅需添加额外的CSS规则。 ```css .clearfix::after { content: ""; display: block; clear: both; } ``` 将上述样式应用于父元素即可解决问题[^1]。 #### 方法二:设置 `overflow` 属性 给父元素设置 `overflow:hidden` 或者 `overflow:auto` 可以触发块格式化上下文(Block Formatting Context, BFC),从而使父元素能够正确包裹浮动的子元素。这种方法简单易用,不会引入多余的标记或伪元素。 ```css .parent-element { overflow: hidden; /* 或 auto */ } ``` 需要注意的是,`overflow` 的值应根据实际需求选择,以免意外隐藏溢出的内容[^2]。 #### 方法三:使用 `clear:both` 清除浮动 在最后一个浮动子元素后面插入一个新的块级元素,并为其指定 `clear:both` 样式。虽然这种方法有效,但它会在HTML中增加不必要的标签,因此并不推荐作为首选方案。 ```html <div style="clear:both;"></div> ``` #### 方法四:伪类 ::after 实现清除浮动 利用伪类 `::after` 创建一个虚拟内容并对其施加 `clear:both` 样式,这是现代浏览器广泛支持的一种优雅方式。 ```css .parent-element::after { content: ""; display: table; clear: both; } ``` 此方法类似于 clearfix 技术,但更加简洁明了[^3]。 #### 方法五:采用 Flexbox 或 Grid 布局替代传统 float 随着CSS的发展,Flexbox 和 Grid 提供了更为强大和灵活的布局能力,可以从根本上规避由 float 导致的各种复杂情况。对于新项目而言,建议优先考虑这两种现代化的技术栈。 ```css .container { display: flex; } /* 或 */ .container { display: grid; } ``` 以上每一种方法都有各自的适用场景,在具体实施过程中可以根据实际情况选取最合适的策略[^4]。 --- 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值