前端css基础之浮动属性

浮动属性 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        div{
            width: 100px;
            height: 100px;
        }

        .red{

            background-color: red;
            float: left;
        }
        .green{
            width: 500px;
            background-color: green;
            /* float: left; */
        }
        .blue{
            background-color: blue;
            /* float: left; */
        }
    </style>
</head>
<body>
    <div class="red"></div>
    <div class="green"></div>
    <div class="blue"></div>
</body>
</html>

如果三个方框都设置浮动属性,则就并排显示 

 设置多个div浮动时,一行排不满就换另一行

 右浮动

 见缝插针原则

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<!-- 见缝插针原则 -->
    <style>
        div{
            width: 100px;
            height: 100px;
        }

        .red{
            width: 800px;
            height: 800px;
            background-color: red;
            float: left;
        }
        .green{
            width: 600px;
            height: 600px;
            background-color: green;
            float: left;
        }
        .blue{
            width: 300px;
            height: 300px;
            background-color: blue;
            float: left;
        }
    </style>
</head>
<body>
    <div class="red"></div>
    <div class="green"></div>
    <div class="blue"></div>
</body>
</html>

文字浮动中文字会被挤出来

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        div{
            width: 100px;
            height: 100px;
        }

        .red{

            background-color: red;
            float: left;
        }
        .green{
            width: 300px;
            height: 300px;
            background-color: green;
            /* float: left; */
        }
        .blue{
            background-color: blue;
            /* float: left; */
        }
    </style>
</head>
<body>
    <div class="red"></div>
    <div class="green">大家好你好</div>
    <div class="blue"></div>
</body>
</html>

 总结


清浮动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .box1,.box2{
            width: 200px;
            height: 200px;
            float: left;
        }

        .box1{
            background-color: yellow;
        }
        .box2{
            background-color: blue;
        }
        .box{
            width: 300px;
            height: 300px;
            background-color: red;
            clear: left;
        }
        .container{
            overflow: hidden;
        }
        /* 
        1.写固定高度 
        2.清浮动  clear:none/right/left/both
        3.当前浮动元素后补一个div,不设置宽高,clear:both
        4.overflow;hidden
        */
    </style>
</head>
<body>
    <div class="container">
        <div class="box1"></div>
        <div class="box2"></div>
        <!-- <div style="clear: both;"></div> -->
    </div>

    <div class="box"></div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值