当前位置:Gxlcms > html代码 > 实例介绍六种html清除浮动的方式,供参考

实例介绍六种html清除浮动的方式,供参考

时间:2021-07-01 10:21:17 帮助过:37人阅读

使用display:inline-block会出现的情况:

1.使块元素在一行显示
2.使内嵌支持宽高
3.换行被解析了
4.不设置的时候宽度由内容撑开
5.在IE6,7下步支持块标签
由于inline-block属性换行的时候被解析(有间隙)故解决方法使用浮动float:left/right
使用浮动时出现的情况:
1.使块元素在一行显示
2.使内嵌元素支持宽高
3.不设置不宽高的时候宽度由内容撑开
4.换行不被解析(故使用行内元素的时候清除间隙的方法可以使用浮动)
5.元素添加浮动,会脱离文档流,按照指定的一个方向移动,直到碰到父级的边界或者另一个浮动元素停止(文档流是文档中可显示对象在排列时所占用的位置)

清浮动的方法:
1.给父级也加浮动
(这种情况当父级margin:0 auto;时不居中)

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <style>
  7. .box{ width:300px;margin:0 auto;border:10px solid #000; float:left;}
  8. .p{ width:200px;height:200px;background:red;float:left;}
  9. /*
  10. 清浮动
  11. 1.给父级也加浮动(不居中了)
  12. */
  13. </style>
  14. </head>
  15. <body>
  16. <p class="box">
  17. <p class="p"></p>
  18. </p>
  19. </body>
  20. </html>

2.给父级加display:inline-block;(同方法1,不居中。只有IE6,7居中)

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <style>
  7. .box{ width:300px;margin:0 auto;border:10px solid #000; display:inline-block;}
  8. .p{ width:200px;height:200px;background:red;float:left;}
  9. /*
  10. 清浮动
  11. 1.给父级也加浮动
  12. 2.给父级加display:inline-block
  13. */
  14. </style>
  15. </head>
  16. <body>
  17. <p class="box">
  18. <p class="p"></p>
  19. </p>
  20. </body>
  21. </html>

3.在浮动元素下加<p class="clear"></p>

.clear{ height:0px;font-size:0;clear:both;}但是在ie6下,块元素有最小高度,即当height<19px时,默认为19px,解决方法:font-size:0;或overflow:hidden;

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <style>
  7. .box{ width:300px;margin:0 auto;border:10px solid #000;}
  8. .p{ width:200px;height:200px;background:red;float:left;}
  9. .clear{ height:0px;font-size:0;clear:both;}
  10. /*
  11. 清浮动
  12. 1.给父级也加浮动
  13. 2.给父级加display:inline-block
  14. 3.在浮动元素下加<p class="clear"></p>
  15. .clear{ height:0px;font-size:0;clear:both;}
  16. */
  17. </style>
  18. </head>
  19. <body>
  20. <p class="box">
  21. <p class="p"></p>
  22. <p class="clear"></p>
  23. </p>
  24. </body>
  25. </html>

4.在浮动元素下加<br clear="all">

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <style>
  7. .box{ width:300px;margin:0 auto;border:10px solid #000;}
  8. .p{ width:200px;height:200px;background:red;float:left;}
  9. /*
  10. 清浮动
  11. 1.给父级也加浮动
  12. 2.给父级加display:inline-block
  13. 3.在浮动元素下加<p class="clear"></p>
  14. .clear{ height:0px;font-size:0;clear:both;}
  15. 4.在浮动元素下加<br clear="all"/>
  16. */
  17. </style>
  18. </head>
  19. <body>
  20. <p class="box">
  21. <p class="p"></p>
  22. <br clear="all"/>
  23. </p>
  24. </body>
  25. </html>

5.给浮动元素父级加{zoom:1;}
:after{content:""; display:block;clear:both;}

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <style>
  7. .box{margin:0 auto;border:10px solid #000;}
  8. .p{ width:200px;height:200px;background:red;float:left;}
  9. .clear{zoom:1;}
  10. .clear:after{content:""; display:block;clear:both;}
  11. /*
  12. 清浮动
  13. 1.给父级也加浮动
  14. 2.给父级加display:inline-block
  15. 3.在浮动元素下加<p class="clear"></p>
  16. .clear{ height:0px;font-size:0;clear:both;}
  17. 4.在浮动元素下加<br clear="all"/>
  18. 5. 给浮动元素的父级加{zoom:1;}
  19. :after{content:""; display:block;clear:both;}
  20. **在IE6,7下浮动元素的父级有宽度就不用清浮动
  21. haslayout 根据元素内容的大小 或者父级的父级的大小来重新的计算元素的宽高
  22. display: inline-block
  23. height: (任何值除了auto)
  24. float: (left 或 right)
  25. width: (任何值除了auto)
  26. zoom: (除 normal 外任意值)
  27. */
  28. </style>
  29. </head>
  30. <body>
  31. <p class="box clear">
  32. <p class="p"></p>
  33. </p>
  34. </body>
  35. </html>

6.给浮动元素父级加overflow:auto;

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>无标题文档</title>
  6. <style>
  7. .box{ width:300px;border:1px solid #000;overflow:auto;}
  8. .p1{ width:260px;height:400px;background:Red;float:left;}
  9. </style>
  10. </head>
  11. <body>
  12. <p class="box">
  13. <p class="p1"></p>
  14. </p>
  15. </body>
  16. </html>

以上就是实例介绍六种html清除浮动的方式,供参考的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行