float浮动
float 属性定义元素在哪个方向浮动,浮动元素会生成一个块级框,直到该块级框的外边缘碰到包含框或者其他的浮动框为止。
Float(浮动),往往是用于图像,但它在布局时一样非常有用

left
right
none
inherit

特性:加浮动的元素,会脱离文档流,会延迟父容器靠左或靠右排列,如果之前已经有浮动的元素,会挨着浮动的元素进行排列。

<!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>
        body{border: 1px black solid;}
        #box1{width: 100px;height: 100px;background-color: brown;float: left;/* float: right; */}
        #box2{width: 200px;height: 200px;background-color: green;}
    </style>
</head>
<body>
    <div id="box1"></div>
     <div id="box2"></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>
        body{border: 1px black solid;}
        #box1{width: 100px;height: 100px;background-color: brown;float: left;/* float: right; */}
        #box2{width: 200px;height: 200px;background-color: green;float: left;}
    </style>
</head>
<body>
    <div id="box1"></div>
     <div id="box2"></div>
 
</body>
</html>

在这里插入图片描述
float注意点
只会影响后面的元素

<!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>
        body{border: 1px black solid;}
        #box1{width: 100px;height: 100px;background-color: brown;/* float: right; */}
        #box2{width: 200px;height: 200px;background-color: green;float: left;}
        #box3{width: 300px;height: 300px;background-color: blue;}
    </style>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="box3"></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>
        body{border: 1px black solid;}
        #box1{width: 100px;height: 100px;background-color: brown;/* float: right; */}
        #box2{width: 200px;height: 200px;background-color: green;float: left;}
        #box3{width: 300px;height: 300px;background-color: blue;}
    </style>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="box3">晏青篁2晏青篁3晏青篁4晏青篁5晏青篁6晏青篁7晏青篁8晏青篁</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>
        body{border: 1px black solid;}
        #box1{width: 100px;height: 100px;background-color: brown;/* float: right; */}
        #box2{width: 200px;height: 200px;background-color: green;float: left;}
        #box3{width: 300px;height: 300px;background-color: blue;}
        #box4{background-color:yellow;float: left;}
    </style>
</head>
<body>
    <div id="box1"></div>
    <div id="box2"></div>
    <div id="box3">晏青篁2晏青篁3晏青篁4晏青篁5晏青篁6晏青篁7晏青篁8晏青篁</div>
    <div id="box4">默认宽根据内容决定</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>
        ul{margin: 0;padding: 0;list-style: none;width: 300px;height: 300px;border: 1px blue solid;}
        li{width: 100px;height: 100px;background-color: red;border: 1px yellow solid;box-sizing: border-box  ;float: left;}
    </style>
</head>
<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
</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>
        span:last-of-type{float: right;}
    </style>
</head>
<body>
    <span>aaaa</span><span>bbbb</span>
</body>
</html>

在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐