CSS的浮动

块元素:独占一行,可以设置宽高,如果不设置宽度默认宽度是屏幕宽度。

行内元素:与其他元素同行显示,不可以设置宽高。宽高就是文字或者图片的宽高。

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
        div{
            width: 300px;
            height: 200px;
            background-color: blue;
        }
        span{
            width: 300px;
            height: 200px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div>
        这是块级元素1
    </div>
    <div>
        这是块级元素2
    </div>
    <span>
        这是行内元素1
    </span>
    <span>
        这是行内元素2
    </span>
</body>

</html>
  • 浮动:
    会使元素向左或者向右一定,只能左右,不能上下。
    浮动元素碰到包含框或者另一个浮动框,浮动停止。
    浮动元素之后的元素将围绕她,之前的不受影响。
    浮动元素会脱离标准流。

  • 浮动的用法
    float:left 靠左浮动
    float:right 靠右浮动
    float:none 不使用浮动

  • 使用浮动后产生的问题: 父元素会失去它的高度

没有清除浮动
在这里插入图片描述
清除浮动之后
在这里插入图片描述

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
        .container{
            width: 500px;
            /* height: 500px; */
            border: 2px solid #333;
        }
        .box01{
            width: 100px;
            height: 100px;
            background-color: blue;
            color: #fff;
            float: left;
        }
        .box02{
            width: 100px;
            height: 100px;
            background-color: green;
            color: #fff;
            /* float: left; */
        }
        .box03{
            width: 100px;
            height: 100px;
            background-color: red;
            color: #fff;
            /* float: left; */
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="box01"></div>
        <div class="box02"></div>
        <div class="box03"></div>
    </div>
</body>
</html>
  • 如何清除浮动?
    清除浮动: clear:none|left|right|both;
    设置了float的元素会影响其他相邻元素,需要使用clear清除浮动,clear只会影响自身,不会对其他相邻元素造成影响。

clear:both 用的比较多

clear:left 来清除浮动

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
        .container{
            width: 500px;
            /* height: 500px; */
            border: 2px solid #333;
        }
        .box01{
            width: 100px;
            height: 100px;
            background-color: blue;
            color: #fff;
            float: left;
            clear: left;
        }
        .box02{
            width: 100px;
            height: 100px;
            background-color: green;
            color: #fff;
            float: left;
            clear: left;
        }
        .box03{
            width: 100px;
            height: 100px;
            background-color: red;
            color: #fff;
            float: left;
            clear:left;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="box01"></div>
        <div class="box02"></div>
        <div class="box03"></div>
    </div>
</body>

</html>

清除浮动的常用方法:
方法1:在浮动元素后使用一个空元素

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
        .clear{
            clear: both;
        }
        .container{
            width: 500px;
            /* height: 500px; */
            border: 2px solid #333;
        }
        .box01{
            width: 100px;
            height: 100px;
            background-color: blue;
            color: #fff;
            float: left;
        }
        .box02{
            width: 100px;
            height: 100px;
            background-color: green;
            color: #fff;
            float: left;
        }
        .box03{
            width: 100px;
            height: 100px;
            background-color: red;
            color: #fff;
            float: left;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="box01"></div>
        <div class="box02"></div>
        <div class="box03"></div>
        <div class="clear"></div>
    </div>
</body>
</html>

方法2: 给浮动元素的容器添加一个overflow:hidden;

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
        .container{
            width: 500px;
            /* height: 500px; */
            border: 2px solid #333;
            overflow: hidden;
        }
        .box01{
            width: 100px;
            height: 100px;
            background-color: blue;
            color: #fff;
            float: left;
        }
        .box02{
            width: 100px;
            height: 100px;
            background-color: green;
            color: #fff;
            float: left;
        }
        .box03{
            width: 100px;
            height: 100px;
            background-color: red;
            color: #fff;
            float: left;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="box01"></div>
        <div class="box02"></div>
        <div class="box03"></div>
    </div>
</body>
</html>

方法3:使用css3的 :after 伪元素。

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
    
        .clearfix:after{
            content:'.';
            height: 0;
            display: block;
            visibility: hidden;
            clear: both;
        }
        .container{
            width: 500px;
            border: 2px solid #333;
        }
        .box01{
            width: 100px;
            height: 100px;
            background-color: blue;
            color: #fff;
            float: left;
        }
        .box02{
            width: 100px;
            height: 100px;
            background-color: green;
            color: #fff;
            float: left;
        }
        .box03{
            width: 100px;
            height: 100px;
            background-color: red;
            color: #fff;
            float: left;
        }

    </style>
</head>

<body>
    <div class="container clearfix">
        <div class="box01"></div>
        <div class="box02"></div>
        <div class="box03"></div>
    </div>
</body>
</html>

方法4: 给父级别元素也添加float:left; 也可以消除浮动,但是会产生新的浮动问题,不推荐使用。

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
        .container{
            width: 500px;
            /* height: 500px; */
            border: 2px solid #333;
            float: left;
        }
        .box01{
            width: 100px;
            height: 100px;
            background-color: blue;
            color: #fff;
            float: left;
        }
        .box02{
            width: 100px;
            height: 100px;
            background-color: green;
            color: #fff;
            float: left;
        }
        .box03{
            width: 100px;
            height: 100px;
            background-color: red;
            color: #fff;
            float: left;
        }

    </style>
</head>

<body>
    <div class="container">
        <div class="box01"></div>
        <div class="box02"></div>
        <div class="box03"></div>
    </div>
</body>
</html>

方法5:给父级别元素定义height,只适合高度固定的布局。

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS文字样式</title>
    <style type="text/css">
        .container{
            width: 500px;
            height: 500px;
            border: 2px solid #333;
        }
        .box01{
            width: 100px;
            height: 100px;
            background-color: blue;
            color: #fff;
            float: left;
       
        }
        .box02{
            width: 100px;
            height: 100px;
            background-color: green;
            color: #fff;
            float: left;
         
        }
        .box03{
            width: 100px;
            height: 100px;
            background-color: red;
            color: #fff;
            float: left;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box01"></div>
        <div class="box02"></div>
        <div class="box03"></div>
    </div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值