CSS 之 float

作者: JimmyChung | 来源:发表于2016-02-26 21:46 被阅读157次

想必只要是写过CSS的同学,对float属性肯定不陌生。float是在CSS很常用的属性之一了。但有的时候,常用容易变成滥用,哪里都来一个float,也就造就了一些坑。

flaot属性与display属性

有的时候看回自己刚入行时写的CSS,在浮动块的相关代码时总是有类似的代码片段

div {
    width: 200px;
    height: 200px;
    display: inline-block;
    float: left; 
}

不知道大家觉没觉得上面的代码有问题。答案是明确的,而问题在于display的设置上。

设置了float属性值为非none的元素,display的属性值都会变成block,无论该元素原先的display属性值是什么

下面我们来看看证据,请看代码:

  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        div{
            background-color: black;
            color: white;
            display: inline;
              //同理,可把display的值改成inline-block,block
            float: left;
        }
    </style>
  </head>
  <body>
    <div>hello</div>
  </body>
  </html>

下面是浏览器计算的结果,display的值始终为block

  background-color: rgb(0, 0, 0);
  color: rgb(255, 255, 255);
  display: block;
  float: left;
  height: 16px;
  width: 37.046875px;

所以在元素设定float属性的时候,不必去重复设定display的值

浮动清除

为什么要清除浮动
让元素浮动会导致其父族元素的高度崩塌,从而会导致一些图文围绕的问题(如果原意并非是要图文围绕)。所以清除浮动事实上就是让父元素拥有高度。

清除浮动常用的三种方式:

  • 父元素overflow:hidden
  • 插入clear-fix div
  • :after方法

父元素overflow:hidden

  • 代码:

    <!DOCTYPE html>
      <html lang="en">
          <head>
            <meta charset="UTF-8">
            <title>Document</title>
            <style type="text/css">
              .wrap{
                overflow: hidden;
              }
              .left{
                width: 200px;
                height: 200px;
                background-color:  #42b983;
                float: left;
                border: 1px solid #bbb;
              }
            </style>
          </head>
          <body>
            <div class="wrap">
              <div class="left"></div>
            </div>
            <p>this is the example text this is the example text this is the example text this is the example text this is the example text this is the example text</p>
          </body>
      </html>
    
  • 概述:为浮动元素的父元素设置overflow:hidden.使父元素扩展至浮动元素的高度(浏览器会自动计算浮动元素的最高高度作为父元素高度).不能同时再设置height.

插入clear-fix div

  • 代码:

    <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Document</title>
          <style type="text/css">
              .clear-fix{
                  clear: both;
              }
    
              .left{
                  width: 200px;
                  height: 200px;
                  background-color:  #42b983;
                  float: left;
                  border: 1px solid #bbb;
              }
          </style>
      </head>
      <body>
              <div class="left"></div>
              <div class="clear-fix"></div>
          <p>this is the example text this is the example text this is the example text this is the example text this is the example text this is the example text</p>
      </body>
      </html>
    
  • 概述:在浮动元素后添加一个clear-fix的div块元素,为该元素设置clear:both,以达到清除浮动的作用。此方法兼容性较好。但是在使用浮动较多的情况下,会产生较多额外的div元素,使得html难以维护。

:after方法

  • 代码:

     <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Document</title>
          <style type="text/css">
              .wrap:after{
                  display: block;
                  content: "";
                  clear: both;
                  visibility: hidden;
                  height: 0;
              }
              .left{
                  width: 200px;
                  height: 200px;
                  background-color:  #42b983;
                  float: left;
                  border: 1px solid #bbb;
              }
          </style>
      </head>
      <body>
          <div class="wrap">
              <div class="left"></div>
          </div>
          <p>this is the example text this is the example text this is the example text this is the example text this is the example text this is the example text</p>
      </body>
      </html>
    
  • 概述:这个方法与clear-fix方法相似。但不用添加额外的div。在浮动元素的父元素中使用after伪类,相当于在父元素的后面添加一个div元素用于清除浮动,但是它是不可见的。较多大型网站使用该方法清除浮动。

相关文章

  • css之float

    float - 浮动 原始意义:用来实现让文字环绕图片而已 特性:包裹 于 破坏 辟开浮动的“破坏性”。浮动就是个...

  • CSS之float

    float 属性定义元素在哪个方向浮动。在CSS中,任何元素都可以浮动。浮动元素会生成一个块级框/Block,而不...

  • CSS 之 float

    想必只要是写过CSS的同学,对float属性肯定不陌生。float是在CSS很常用的属性之一了。但有的时候,常用容...

  • 浮动和清除(闭合)浮动

    CSS之float and clear float 浮动和清除(闭合)浮动 目录 1.背景介绍 2.知识剖析 3....

  • CSS理解之Float

    1.Float的设计初衷仅仅是: 实现文字环绕效果,如下图所示: 明白了float的设计初衷,就可以明白float...

  • CSS之浮动float

    浮动 1.1 浮动的元素 在CSS中,浮动通过float属性实现。 取值: left | right | non...

  • Test10

    引用文章: 那些年我们一起清除过的浮动 CSS浮动float详解 Clear Float CSS float浮动的...

  • clearfix

    css中float left与float right的使用说明

  • CSS—11.22日更新

    CSS—浮动(内容多图,建议wifi下打开)新增对float定义部分的理解 ; CSS—盒子模型(a)新增对元素之...

  • CSS float

    网页布局的核心,利用 CSS 来摆放盒子的位置,如何把盒子摆放到合适的位置?CSS 定位分为三种,普通流(标准流)...

网友评论

  • 7091a52ac9e5:谢谢分享,又一次复习了关于清除浮动的知识。

本文标题:CSS 之 float

本文链接:https://www.haomeiwen.com/subject/yskwkttx.html