码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-12-30 12:06:47      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:block   sla   20px   col   img   分享图片   css   splay   body   

float的设计初衷仅仅是:为了文字环绕效果

 

float的包裹与破坏

包裹:收缩、坚挺、隔绝(BFC)

破坏:父元素高度塌陷

技术分享图片
<!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破坏性的影响(清除浮动)?

1、底部插入clear:both;

技术分享图片
<!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

2、父元素BFC/haslayout

技术分享图片
<!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的特性

1、元素block块状化(砖头化);

2、破坏性造成的紧密排列特性(去空格化)。

 

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

标签:block   sla   20px   col   img   分享图片   css   splay   body   

原文地址:https://www.cnblogs.com/fengxiongZz/p/8148673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!