猿问

怎样进行浮动的清除?

xiaoxiaoxiao遥
浏览 1352回答 3
3回答

西兰花伟大炮

<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">     <meta http-equiv="X-UA-Compatible" content="ie=edge">     <title>Document</title>     <style>         .container{             height: auto;             width: 400px;             margin: 100px auto;             border: 2px dotted red;         }         .item{             float: left;             width: 50px;             height: 50px;             background: yellow;             margin-right: 10px;         }         /*.clearfix::after{             content: "";             height: 0;             clear: both;             display: block;         }*/         .last{             clear: both;         }     </style> </head> <body>     <div class="container clearfix" >         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="item"></div>         <div class="last"></div>     </div> </body> </html>两种方法,一种伪类,一种是在末尾添加空元素,欢迎采纳

frece

<!DOCTYPE html> <html>  <head>     <meta charset="UTF-8">     <title>Document</title>     <style>      .fl{float: left;}       .fr{float: right;}      .square{width: 200px; height: 200px; border: 1px solid #0000ff;}      .clear{clear: both;}     </style> </head>  <body> <div> <div class="square fl">square1</div> <div class="square fr">square2</div> <div class="clear"></div> </div> <label>test text</label> </body>  </html>大型项目里都会定义一个 .clear 的样式  需要的时候直接用,上边代码你可以把<div class="clear"></div>去掉看看区别一般情况下, 有浮动必有clear
随时随地看视频慕课网APP
我要回答