float

本文详细介绍了CSS中的float属性,包括left和right两个方向的浮动效果。float会使元素脱离正常文档流,并在指定方向上尽可能靠近其容器边框。当多个浮动元素相邻时,它们会按顺序排列。同时,float还会影响后续元素的位置,只对后面元素起作用。文章通过实例展示了如何使用float实现换行排列和内容排列。此外,还提到内联元素也可使用float,改变其默认布局。
摘要由CSDN通过智能技术生成

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>

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值