css清除浮动 clear float

来源:互联网 发布:淘宝通知降权了怎么办 编辑:程序博客网 时间:2024/04/30 11:14
clear float1.父元素添加overflow:hidden第一个方法很简单,缺点是不太直,即父元素用overflow:hidden,以强制它包元素。eg.<section>    <img src = "images/rubber_duck2.jpg" />    <p>It's fun to float.</p></section><footer> Here is the footer element that runs across the bottom of the page.</footer>section{border:1px solid blue; margin:0 0 10px 0; overflow:hidden;}img{float:left;}p{border:1px solid red;}overflow:hidden;它能可靠的迫使父元素包含其浮的子元素2.同父元素section{border:1px solid blue; float:left; width:100%;}img{float:left;}footer{border:1px solid red; clear:left;}浮section以后,不管其子元素是否浮,它都会紧紧的包住它的子元素。因此需要用width:100%再让section与浏览器容器同。另外,由于section在也浮了,所以footer会努力往上到它旁去。了强制footer依然待在section下方,要用clear:left。被清除的元素不会被提升到浮元素的旁。3.添加非浮的清除元素<section>    <img src = "images/rubber_duck2.jpg" />    <p>It's fun to float.</p>    <div class = "clear_me"></div></section><footer> Here is the footer element that runs across the bottom of the page.</footer>section{border:1px solid blue; float:left; width:100%;}img{float:left;}.clear_me{clear:left;}footer{border:1px solid red; clear:left;}这样,浮的元素被父元素包住了。如果你特不想添加性元素,我再告你一个用css来添加个清除元素的方法。首先,要section添加一个。<section class="clearfix">    <img src = "images/rubber_duck2.jpg" />    <p>It's fun to float.</p></section><footer> Here is the footer element that runs across the bottom of the page.</footer>然后,再使用个神奇的clearfix的规则!.clearfix:after{    content:".";    display:block;    height:0;    visibility:hidden;    clear:both;}个clearfix规则也叫元素清除法,它最早是由程序Tony Aslett明的,它只添加了一个清除的包含句点作非浮元素(必有内容,而句点是最小的内容)。规则中的其他声明是了确保元素没有高度,而且在面上不可。此外,没有父元素如何清除浮<section class="clearfix">    <img src = "images/rubber_duck2.jpg" />    <p class="clearfix">It's fun to float.</p>    <img src = "images/rubber_duck2.jpg" />    <p class="clearfix">It's fun to float.</p>    <img src = "images/rubber_duck2.jpg" />    <p class="clearfix">It's fun to float.</p></section><footer> Here is the footer element that runs across the bottom of the page.</footer>每一个段落都加上clearfix这样将来哪个段落的文本高度低于片了,面布局都不会被破坏
1 0
原创粉丝点击