经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
HTML连载48-清除浮动的其中两种方式 - 心悦君兮君不知-睿
来源:cnblogs  作者:心悦君兮君不知-睿  时间:2019/11/8 8:37:15  对本文有异议

一、清除浮动的方式一

给前面一个父元素设置高度,?注意:企业开发中能不写高度就不写高度

  1.  
  2. <!DOCTYPE html>
  3.  
  4. <html lang="en">
  5.  
  6. <head>
  7.  
  8. <meta charset="UTF-8">
  9.  
  10. <title>D131_ClearFloat</title>
  11.  
  12. <style>
  13. .smallbox1{
  14. width:100px;
  15. height:100px;
  16. background-color: red;
  17. boder:3px solid black;
  18. margin:5px;
  19. float:right;
  20. ?
  21. }
  22. .smallbox2{
  23. width:100px;
  24. height:100px;
  25. background-color: red;
  26. boder:3px solid black;
  27. margin:5px;
  28. ?
  29. }
  30. .smallbox3{
  31. width:100px;
  32. height:100px;
  33. background-color: red;
  34. boder:3px solid black;
  35. margin:5px;
  36. ?
  37. }
  38. .smallbox4{
  39. width:100px;
  40. height:100px;
  41. background-color: red;
  42. boder:3px solid black;
  43. margin:5px;
  44. ?
  45. }
  46. .smallbox5{
  47. width:100px;
  48. height:100px;
  49. background-color: red;
  50. boder:3px solid black;
  51. margin:5px;
  52. ?
  53. }
  54. .smallbox6{
  55. width:100px;
  56. height:100px;
  57. background-color: red;
  58. boder:3px solid black;
  59. margin:5px;
  60. ?
  61. }
  62. .bigbox1,.bigbox2{
  63.  
  64. /*width:400px;*/
  65.  
  66. /*width:400px;*/
  67. background-color: green;
  68. border:3px black solid;
  69.  
  70. }
  71.  
  72. </style>
  73.  
  74. </head>
  75.  
  76. <body>
  77.  
  78. <div class="bigbox1">
  79.  
  80. <div class="smallbox1"></div>
  81.  
  82. <div class="smallbox2"></div>
  83.  
  84. <div class="smallbox3"></div>
  85.  
  86. </div>
  87.  
  88. <div class="bigbox2">
  89.  
  90. <div class="smallbox4"></div>
  91.  
  92. <div class="smallbox5"></div>
  93.  
  94. <div class="smallbox6"></div>
  95.  
  96. </div>
  97.  
  98. </body>
  99.  
  100. </html>
  101.  

二、清除浮动的第二种方式

给后面的属性添加clear属性

clear属性取值:

none:默认取值,按照浮动元素的排序规则进行排序(左浮动找左浮动,右浮动找右浮动)

left:不要找前面的左浮动元素

right:不要找前面的右浮动元素

both:不要找前面的左浮动和有浮动元素

例如:我们不设置大盒子的宽高,小盒子会把大盒子撑起来,但是两个大盒子会因此而在一行上

 

  1. .smallbox1{
  2. width:100px;
  3. height: 100px;
  4. float:left;
  5. background-color: red;
  6. border:2px solid black;
  7. }
  8. .smallbox2{
  9. width:100px;
  10. height: 100px;
  11. float:left;
  12. background-color: red;
  13. border:2px solid black;
  14. }
  15. .smallbox3{
  16. width:100px;
  17. height: 100px;
  18. float:left;
  19. background-color:blue;
  20. border:2px solid black;
  21. }
  22. .smallbox4{
  23. width:100px;
  24. height: 100px;
  25. float:left;
  26. background-color: blue;
  27. border:2px solid black;
  28. }
  29. </style>
  30.  
  31. </head>
  32.  
  33. <body>
  34.  
  35. <div class="bigbox1">
  36.  
  37. <div class="smallbox1"></div>
  38.  
  39. <div class="smallbox2"></div>
  40.  
  41. </div>
  42.  
  43. <div class="bigbox2">
  44.  
  45. <div class="smallbox3"></div>
  46.  
  47. <div class="smallbox4"></div>
  48.  
  49. </div>
  50.  
  51. </body>

 

我们使用clear属性在第三个小盒子上,这样就可以另起一行了(第四个就不用,因为我们就想让第三个挨着第四个),只需要第三个小盒子的代码修改代码

  1. .smallbox3{
  2. clear:left;
  3. width:100px;
  4. height: 100px;
  5. float:left;
  6. background-color:blue;
  7. border:2px solid black;
  8. }

?注意点:margin属性失效了,不失效的方式我们下次再说。

三、源码:

D131_ClearFloat.html

D132_CLearAttribute.html

地址:

https://github.com/ruigege66/HTML_learning/blob/master/D131_ClearFloat.html

https://github.com/ruigege66/HTML_learning/blob/master/D132_CLearAttribute.html

2.CSDN:https://blog.csdn.net/weixin_44630050(心悦君兮君不知-睿)

3.博客园:https://www.cnblogs.com/ruigege0000/

4.欢迎关注微信公众号:傅里叶变换,个人账号,仅用于技术交流,后台回复“礼包”获取Java大数据学习视频礼包

 

 

原文链接:http://www.cnblogs.com/ruigege0000/p/11817220.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号