CSS - 浮动

2019-04-04  本文已影响0人  Hyso

浮动的特点

浮动前:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background-color: yellow;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</style>
</style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
</html>

浮动后:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            float: left;
            background-color: yellow;
    }

    .box2 {
        width: 100px;
        height: 100px;
            float: left;
        background-color: red;
    }
    </style>
</style>
</style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
</body>
</html>

左上角(float: left):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            background-color: yellow;
        }

        .box1 {
            width: 100px;
            height: 100px;
            float: left;
            background-color: red;
        }
    </style>
</style>
</style>
</head>
<body>
    <div class="box">
         <div class="box1"></div>
    </div>
</body>
</html>

右上角(float: right):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            width: 300px;
            height: 300px;
            background-color: yellow;
        }

        .box1 {
            width: 100px;
            height: 100px;
            float: right;
            background-color: red;
        }
    </style>
</style>
</style>
</head>
<body>
    <div class="box">
         <div class="box1"></div>
    </div>
</body>
</html>

浮动前:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            background-color: yellow;
        }
    </style>
</style>
</style>
</head>
<body>
    <div class="box">123</div>
</body>
</html>

浮动后:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            float: left;
            background-color: yellow;
        }
    </style>
</style>
</style>
</head>
<body>
    <div class="box">123</div>
</body>
</html>

浮动案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box1 {
            width: 100px;
            height: 100px;
            background-color: yellow;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: red;
        }

        .box3 {
             width: 100px;
            height: 100px;
            background-color: green;
        }
    </style>
</style>
</style>
</head>
<body>
    <div class="box1"></div>
    <div class="box2"></div>
    <div class="box3"></div>
</body>
</html>

解决方案:在3个元素外面套一个父元素,父元素向右浮动,3个子元素向左浮动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box_parent {
            float: right;
        }

        .box1 {
            width: 100px;
            height: 100px;
            float: left;
            background-color: yellow;
        }

        .box2 {
            width: 100px;
            height: 100px;
            float: left;
            background-color: red;
        }

        .box3 {
             width: 100px;
            height: 100px;
            float: left;
            background-color: green;
        }
    </style>
</style>
</style>
</head>
<body>
    <div class="box_parent">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
    </div>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读