CSS深刻理解之float(HTML/CSS)

float的設計初衷僅僅是:爲了文字環繞效果html

 

float的包裹與破壞ide

包裹:收縮、堅挺、隔絕(BFC)spa

破壞:父元素高度塌陷設計

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Layout</title>
    <style>
      .box{
          border: 1px solid red;
          width: 300px;
          margin: 20px auto;
      }
      .left{
          float: left;
          height: 100px;
          width: 50px;
          border: 1px solid black;
      }
      .right{
          float: right;
          height: 100px;
          width: 50px;
          border: 1px solid green;
      }
    </style>
  </head>
  <body>
    <div class="box">
        <div class="left"></div>
        <div class="right"></div>
    </div>
  </body>
</html>
View Code

 

如何下降float破壞性的影響(清除浮動)?code

一、底部插入clear:both;htm

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Layout</title>
    <style>
      .box {
        border: 1px solid red;
        width: 300px;
        margin: 20px auto;
      }
      .clearfix {
        clear: both;
      }
      .left {
        float: left;
        height: 100px;
        width: 50px;
        border: 1px solid black;
      }
      .right {
        float: right;
        height: 100px;
        width: 50px;
        border: 1px solid green;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="left"></div>
      <div class="right"></div>
      <div class="clearfix"></div>
    </div>
  </body>
</html>
View Code
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Layout</title>
    <style>
      .box {
        border: 1px solid red;
        width: 300px;
        margin: 20px auto;
      }
      .box:after {
        content: '';
        display: block;
        height: 0;
        overflow: hidden;
        clear: both;
      }
      .left {
        float: left;
        height: 100px;
        width: 50px;
        border: 1px solid black;
      }
      .right {
        float: right;
        height: 100px;
        width: 50px;
        border: 1px solid green;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>
View Code
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Layout</title>
    <style>
      .box {
        border: 1px solid red;
        width: 300px;
        margin: 20px auto;
      }
      .box:after {
        content: '';
        display: table;
        clear: both;
      }
      .left {
        float: left;
        height: 100px;
        width: 50px;
        border: 1px solid black;
      }
      .right {
        float: right;
        height: 100px;
        width: 50px;
        border: 1px solid green;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>
View Code

二、父元素BFC/haslayoutblog

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Layout</title>
    <style>
      .box {
        border: 1px solid red;
        width: 300px;
        margin: 20px auto;
        overflow: hidden;
      }
      .left {
        float: left;
        height: 100px;
        width: 50px;
        border: 1px solid black;
      }
      .right {
        float: right;
        height: 100px;
        width: 50px;
        border: 1px solid green;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="left"></div>
      <div class="right"></div>
    </div>
  </body>
</html>
View Code

 

float的特性utf-8

一、元素block塊狀化(磚頭化);it

二、破壞性形成的緊密排列特性(去空格化)。event

相關文章
相關標籤/搜索