Float浮动

Float浮动

CSSfloat属性会使元素浮动,使元素向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。

实例

  • 元素的水平方向浮动,意味着元素只能左右移动而不能上下移动。
  • 使用float意味着使用块布局,其会在display非块级元素情况下修改display值的计算值。
  • 一个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。
  • 浮动元素会脱离文档流但不会脱离文本流,当浮动时其不会影响周围的盒子模型,但是文字会环绕在浮动元素周围,可以认为文档流与文字流是分层结构而浮动元素盒子与文字元素处于同一层。
  • 文档流,指的是盒子元素排版布局过程中,元素会自动从左往右,从上往下的流式排列。
  • 文本流,指的是文字元素排版布局过程中,元素会自动从左往右,从上往下的流式排列。
  • 脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离文档流的元素不存在而进行定位。
<!DOCTYPE html>
<html>
<head>
    <title>Float</title>
    <style type="text/css">
        body > div{
            border: 1px solid #eee;
            padding: 5px 0;
            margin: 5px 0;
        }
        .t1{
            margin: 0 5px;
            float: left;
            background-color: #EEE;
        }
    </style>
</head>
<body>
    <div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>
    </div>
</body>
</html>

文字环绕效果

可以认为文档流与文字流是分层结构而浮动元素盒子与文字元素处于同一层,当元素浮动时,其脱离文档流不脱离文本流,所以浮动元素的渲染与文字的渲染是一并渲染的,浮动元素会将文字元素挤开,呈现文字环绕浮动元素的效果。

<!DOCTYPE html>
<html>
<head>
    <title>Float</title>
    <style type="text/css">
        body > div{
            border: 1px solid #eee;
            padding: 5px 0;
            margin: 5px 0;
        }
        .t1{
            margin: 0 5px;
            float: left;
            background-color: #EEE;
            height: 100px;
            width: 100px;
        }
    </style>
</head>
<body>
    <div>
        <div class="t1">Float</div>
        <div>123</div>
        <div>123</div>
        <div>123</div>
        <div>123</div>
        <div>123</div>
        <div>123</div>
        <div>123</div>
    </div>
</body>
</html>

虽然float最初的设计就是用来实现文字环绕效果的,在某些使用float的布局下若是不想触发文字环绕效果,可以通过触发BFC来避免文字环绕。

<!DOCTYPE html>
<html>
<head>
    <title>Float</title>
    <style type="text/css">
        body > div{
            border: 1px solid #eee;
            padding: 5px 0;
            margin: 5px 0;
        }
        .t1{
            margin: 0 5px;
            float: left;
            background-color: #EEE;
            height: 100px;
            width: 100px;
        }
    </style>
</head>
<body>
    <div>
        <div class="t1">Float</div>
        <div style="overflow: auto;">
            <div>123</div>
            <div>123</div>
            <div>123</div>
            <div>123</div>
            <div>123</div>
            <div>123</div>
            <div>123</div>
        </div>
    </div>
</body>
</html>

清除浮动

使用浮动可能会导致一些负面影响,由于脱离文档流,无法撑起父元素导致背景以及边框无法正常显示、与其他元素重叠、下层浮动依旧会在上层浮动元素的基础上进行浮动等问题,此时就需要清除浮动。

使用clear属性

通过CSSclear: both;清除浮动。

<!DOCTYPE html>
<html>
<head>
    <title>Float</title>
    <style type="text/css">
        .container{
            border: 1px solid #eee;
            padding: 5px 0;
            margin: 5px 0;
        }
        .t1{
            margin: 0 5px;
            float: left;
            background-color: #EEE;
            height: 100px;
            width: 100px;
        }
        .clear{
            clear: both;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>

        <!-- 此处不清除浮动会产生负面效果 -->

    </div>


    <div class="container">
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>
        
        <!-- 清除浮动 -->
        <div class="clear"></div>

    </div>

    
    <!-- 若是在此处清除浮动也是可以的 但是会无法撑起容器高度 -->
    
    <div class="container">
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>

        <!-- 清除浮动 -->
        <div class="clear"></div>

    </div>
</body>
</html>

配合::after与clear属性

通过使用伪元素::after配合clear属性进行浮动清除。

<!DOCTYPE html>
<html>
<head>
    <title>Float</title>
    <style type="text/css">
        .container{
            border: 1px solid #eee;
            padding: 5px 0;
            margin: 5px 0;
        }
        .t1{
            margin: 0 5px;
            float: left;
            background-color: #EEE;
            height: 100px;
            width: 100px;
        }
        .container::after{
            clear: both;
            content:"";
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>
    </div>

    <div class="container">
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>
    </div>

</body>
</html>

触发BFC

触发浮动容器的BFC来清除浮动。

<!DOCTYPE html>
<html>
<head>
    <title>Float</title>
    <style type="text/css">
        .container{
            border: 1px solid #eee;
            padding: 5px 0;
            margin: 5px 0;
            overflow: auto; /* 触发BFC */
        }
        /* BFC 块级格式化上下文 https://github.com/WindrunnerMax/EveryDay/blob/master/CSS/%E5%9D%97%E7%BA%A7%E6%A0%BC%E5%BC%8F%E5%8C%96%E4%B8%8A%E4%B8%8B%E6%96%87.md */
        .t1{
            margin: 0 5px;
            float: left;
            background-color: #EEE;
            height: 100px;
            width: 100px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>
    </div>

    <div class="container">
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div class="t1">Float</div>
        <div style="height: 10px;background-color: blue;"></div>
    </div>

</body>
</html>

每日一题

https://github.com/WindrunnerMax/EveryDay

参考

https://www.cnblogs.com/lingdu87/p/7770752.html
https://developer.mozilla.org/zh-CN/docs/CSS/float
https://www.w3school.com.cn/css/css_positioning_floating.asp

相关推荐

  1. Float浮动

    2024-06-09 11:38:04       30 阅读
  2. CSS中的Float浮动

    2024-06-09 11:38:04       29 阅读
  3. CSS的浮动(float)布局效果

    2024-06-09 11:38:04       45 阅读
  4. CSS中的Float(浮动):深入解析与运用技巧

    2024-06-09 11:38:04       32 阅读
  5. CSS中浮动float带来的高度塌陷问题及4种解决方案

    2024-06-09 11:38:04       55 阅读
  6. css<span style='color:red;'>浮动</span>

    css浮动

    2024-06-09 11:38:04      55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-09 11:38:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-09 11:38:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-09 11:38:04       87 阅读
  4. Python语言-面向对象

    2024-06-09 11:38:04       96 阅读

热门阅读

  1. Android 日志实时输出

    2024-06-09 11:38:04       33 阅读
  2. 力扣1248.统计优美子数组

    2024-06-09 11:38:04       28 阅读
  3. 定位器追踪器怎么连接手机

    2024-06-09 11:38:04       23 阅读
  4. PySpark教程 (一)概述pyspark

    2024-06-09 11:38:04       27 阅读
  5. 前端面试题日常练-day58 【面试题】

    2024-06-09 11:38:04       31 阅读
  6. 前端怎么实现跨域请求?

    2024-06-09 11:38:04       34 阅读
  7. 达梦数据库(DMDB)基本使用

    2024-06-09 11:38:04       29 阅读
  8. 【嵌入式DIY实例】-OLED显示LM35传感器数据

    2024-06-09 11:38:04       26 阅读
  9. MCU的环形FIFO

    2024-06-09 11:38:04       25 阅读
  10. Elixir学习笔记——递归

    2024-06-09 11:38:04       27 阅读
  11. 数据库表中创建字段查询出来却为NULL?

    2024-06-09 11:38:04       24 阅读