# Css 浮动(float)

#### 什么是浮动?

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列。

Float(浮动),往往是用于图像,但它在布局时一样非常有用。

**浮动,会使元素向左或向右移动,其周围的元素也会重新排列**

例:

```
<!doctype html>

<html>
<head>
<title>css浮动</title>
<meta charset="utf-8"/>

<style>

.box1{
width:200px;
height:200px;
background:red;
margin-bottom:10px;
}

.box2{
width:300px;
height:300px;
background:green;
margin-bottom:10px;
}

.box3{
width:400px;
height:400px;
background:blue;
margin-bottom:10px;
}

</style>
</head>

<body>

<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>

</body>

</html>

```

图为:

 

css 浮动(float) 随笔 第1张


浮动时:

```

.box1{
width:200px;
height:200px;
background:red;
margin-bottom:10px;
float:left;
}

```

图为:

 

css 浮动(float) 随笔 第2张


**彼此相邻的浮动元素**

如果把几个浮动的元素放到一起,如果有空间的话,它们将彼此相邻。

```

.box1{
width:200px;
height:200px;
background:red;
margin-bottom:10px;

float:left;
}

.box2{
width:300px;
height:300px;
background:green;
margin-bottom:10px;

float:left;
}

.box3{
width:400px;
height:400px;
background:blue;
margin-bottom:10px;

float:left;
}

```

图为:

 

css 浮动(float) 随笔 第3张

 

**清除浮动 - 使用 clear**

元素浮动之后,周围的元素会重新排列,为了避免这种情况,使用 clear 属性。

clear 属性指定元素两侧不能出现浮动元素。

 

```
.box4{
clear:both;
}

```

 

###如有不足,请多指教!###

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄