实例介绍六种html清除浮动的方式,供参考
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了实例介绍六种html清除浮动的方式,供参考,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4061字,纯文字阅读大概需要6分钟。
内容图文
![实例介绍六种html清除浮动的方式,供参考](/upload/InfoBanner/zyjiaocheng/393/09a8851498504a8e8e0724a8c9e84b49.jpg)
1.使块元素在一行显示
2.使内嵌支持宽高
3.换行被解析了
4.不设置的时候宽度由内容撑开
5.在IE6,7下步支持块标签
由于inline-block属性换行的时候被解析(有间隙)故解决方法使用浮动float:left/right
使用浮动时出现的情况:
1.使块元素在一行显示
2.使内嵌元素支持宽高
3.不设置不宽高的时候宽度由内容撑开
4.换行不被解析(故使用行内元素的时候清除间隙的方法可以使用浮动)
5.元素添加浮动,会脱离文档流,按照指定的一个方向移动,直到碰到父级的边界或者另一个浮动元素停止(文档流是文档中可显示对象在排列时所占用的位置)
清浮动的方法:
1.给父级也加浮动(这种情况当父级margin:0 auto;时不居中)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000; float:left;} .p{ width:200px;height:200px;background:red;float:left;} /* 清浮动 1.给父级也加浮动(不居中了) */ </style> </head> <body> <p class="box"> <p class="p"></p> </p> </body> </html>
2.给父级加display:inline-block;(同方法1,不居中。只有IE6,7居中)
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000; display:inline-block;} .p{ width:200px;height:200px;background:red;float:left;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block */ </style> </head> <body> <p class="box"> <p class="p"></p> </p> </body> </html>
3.在浮动元素下加<p class="clear"></p>
.clear{ height:0px;font-size:0;clear:both;}但是在ie6下,块元素有最小高度,即当height<19px时,默认为19px,解决方法:font-size:0;或overflow:hidden;
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000;} .p{ width:200px;height:200px;background:red;float:left;} .clear{ height:0px;font-size:0;clear:both;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block 3.在浮动元素下加<p class="clear"></p> .clear{ height:0px;font-size:0;clear:both;} */ </style> </head> <body> <p class="box"> <p class="p"></p> <p class="clear"></p> </p> </body> </html>
4.在浮动元素下加<br clear="all">
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;margin:0 auto;border:10px solid #000;} .p{ width:200px;height:200px;background:red;float:left;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block 3.在浮动元素下加<p class="clear"></p> .clear{ height:0px;font-size:0;clear:both;} 4.在浮动元素下加<br clear="all"/> */ </style> </head> <body> <p class="box"> <p class="p"></p> <br clear="all"/> </p> </body> </html>
5.给浮动元素父级加{zoom:1;}
:after{content:""; display:block;clear:both;}
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{margin:0 auto;border:10px solid #000;} .p{ width:200px;height:200px;background:red;float:left;} .clear{zoom:1;} .clear:after{content:""; display:block;clear:both;} /* 清浮动 1.给父级也加浮动 2.给父级加display:inline-block 3.在浮动元素下加<p class="clear"></p> .clear{ height:0px;font-size:0;clear:both;} 4.在浮动元素下加<br clear="all"/> 5. 给浮动元素的父级加{zoom:1;} :after{content:""; display:block;clear:both;} **在IE6,7下浮动元素的父级有宽度就不用清浮动 haslayout 根据元素内容的大小 或者父级的父级的大小来重新的计算元素的宽高 display: inline-block height: (任何值除了auto) float: (left 或 right) width: (任何值除了auto) zoom: (除 normal 外任意值) */ </style> </head> <body> <p class="box clear"> <p class="p"></p> </p> </body> </html>
6.给浮动元素父级加overflow:auto;
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> <style> .box{ width:300px;border:1px solid #000;overflow:auto;} .p1{ width:260px;height:400px;background:Red;float:left;} </style> </head> <body> <p class="box"> <p class="p1"></p> </p> </body> </html>
以上就是实例介绍六种html清除浮动的方式,供参考的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的实例介绍六种html清除浮动的方式,供参考全部内容,希望文章能够帮你解决实例介绍六种html清除浮动的方式,供参考所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。