《图解CSS》浮动与清除

简介: css中的块级元素在页面中是独占一行的,自上而下排列,也就是我们所说的流,通常我们称之为文档流或标准流。

![浮动与清除概要](https://upload-images.jianshu.io/upload_images/2789632-0e3470674bda8a09.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#### 文档流

css中的块级元素在页面中是独占一行的,自上而下排列,也就是我们所说的流,通常我们称之为文档流或标准流。

示例:

```html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <meta name="viewport" content="width=device-width, initial-scale=1.0">

   <title>Document</title>

</head>

<body>

<html>

<head>

    <style>

        div {width:100px;height:100px;}

    </style>

</head>

<body style="background-color: eee">

    <div style="background-color:red">div1</div>

    <div style="background-color:green">div2</div>

    <div style="background-color:blue">div3</div>

</body>

</html>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-08d92390c0bb68ef.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

#### 清除(clear)

###### 语法

```css

  clear : none | left | right | both

  取值

  none  :  默认值。允许两边都可以有浮动对象

  left   :  不允许左边有浮动对象

  right  :  不允许右边有浮动对象

  both  :  不允许有浮动对象

```

###### 定义

clear 属性规定元素的哪一侧不允许其他浮动元素。

#### 浮动(float)

###### 语法:

```css

float: left; // 元素向左浮动。

float: right; // 元素向右浮动。

float: none; // 默认值。元素不浮动,并会显示在其在文本中出现的位置。

float: inherit; // 规定应该从父元素继承 float 属性的值。

```

###### 定义:

**浮动元素脱离文档流,按照指定的方向(左或右发生移动),直到它的外边缘碰到父元素或者另一个浮动元素为止。**

###### 历史:

起初,W3C规定出来的浮动(float)属性的主要目的,是为了实现文本绕排图片的效果,如下示例:

```html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <meta name="viewport" content="width=device-width, initial-scale=1.0">

   <title>Document</title>

   <style>

       img {

           width: 200px;

           height: 200px;

           display: inline;

           float: left;

       }

       p {

           color: red;

       }

   </style>

</head>

<body>

   <img src="../img/示例图片.jpg" alt="">

   <p>文字文字文字文字文字文字文字文

       文字文字文字字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字

       文字文字文字文字文字文字文字文字</p>

</body>

</html>

```

不添加图片浮动的效果:

![](https://upload-images.jianshu.io/upload_images/2789632-6230bb6beb5e5707.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

添加浮动的效果:

![](https://upload-images.jianshu.io/upload_images/2789632-2002f5d339783d87.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

###### 发展:

后来有人用它来做布局,发现不错,这个属性也成了创建多栏布局最简单的方式。

示例:

```html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <meta name="viewport" content="width=device-width, initial-scale=1.0">

   <title>Document</title>

</head>

<body>

<html>

<head>

    <style>

        div {width:100px;height:100px;}

    </style>

</head>

<body style="background-color: eee">

    <div style="background-color:red; float:left;">div1</div>

    <div style="background-color:green; float:left;">div2</div>

    <div style="background-color:blue;float:right;">div3</div>

</body>

</html>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-dc6040fa2c8d8379.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

###### 浮动元素脱离了常规的文档流之后,原来紧跟其后的元素就会在空间允许的情况下,向上提升到与浮动元素平齐平坐。

**问题一:遮盖问题**:向上提升的元素会被浮动的元素盖住,因为浮动的元素已经脱离了标准流,浮动的元素已经和标准流不在同一层次上了。如下实例:

```html

<!DOCTYPE html>

<html lang="en">

<head>

   <meta charset="UTF-8">

   <meta name="viewport" content="width=device-width, initial-scale=1.0">

   <title>Document</title>

</head>

<body>

<html>

<head>

    <style>

        div {width:100px;height:100px;}

    </style>

</head>

<body style="background-color: eee">

    <div style="background-color:red; float:left;">div1</div>

    <div style="background-color:green;">div2</div>

    <div style="background-color:blue;">div3</div>

</body>

</html>

</body>

</html>

```

![](https://upload-images.jianshu.io/upload_images/2789632-4df87c5d8e2f9073.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

由上图我们可以看出,第二个div会被第一个div给盖住,但是里面文字会被挤到下一行,而且**好像**是第二个div隐形存在于下一行。

**解决办法**

利用clear属性,清除浮动造成的影响,注意,clear使用在非浮动元素上,而非浮动元素上面。

**clear属性不允许被清除浮动的元素的左边/右边挨着浮动元素,底层原理是在被清除浮动的元素上边或者下边添加足够的清除空间。**

实例:

![](https://upload-images.jianshu.io/upload_images/2789632-6d96bc337175ce78.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

**问题二:父元素高度塌陷问题**:由于浮动元素脱离的文档流,其父元素感知不到子元素的存在,高度便会消失。如下实例:

![](https://upload-images.jianshu.io/upload_images/2789632-4b76dfe438a6bc08.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

**解决办法一:BFC清除浮动**

在父元素添加overflow:auto或overflow:hidden

父元素的这个属性设置为auto或者是hidden,父元素就会扩展包含浮动,这个方法有着比较好的语义性,因为它不需要额外的元素,但是,要记住一点,overflow属性不是为了清除浮动而定义的,要小心不要覆盖住内容或者触发了不需要的滚动条。

![](https://upload-images.jianshu.io/upload_images/2789632-bb8a589fc67923ae.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

**解决办法二:clearfix**

clearfix的方式清除浮动

![](https://upload-images.jianshu.io/upload_images/2789632-d251476d8b680a71.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

通用方案:

```css

// 现代浏览器clearfix方案,不支持IE6/7

.clearfix:after {

   display: table;

   content: " ";

   clear: both;

}

// 全浏览器通用的clearfix方案

// 引入了zoom以支持IE6/7

.clearfix:after {

   display: table;

   content: " ";

   clear: both;

}

.clearfix{

   *zoom: 1;

}

// 全浏览器通用的clearfix方案【推荐】

// 引入了zoom以支持IE6/7

// 同时加入:before以解决现代浏览器上边距折叠的问题

.clearfix:before,

.clearfix:after {

   display: table;

   content: " ";

}

.clearfix:after {

   clear: both;

}

.clearfix{

   *zoom: 1;

}

```

#### 浮动使用场景

###### 1. 文本绕排图片

![](https://upload-images.jianshu.io/upload_images/2789632-9983bdc5dfe9c132.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

###### 2.页面布局

![](https://upload-images.jianshu.io/upload_images/2789632-92c730bd8f367f0a.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

###### 3.多元素内联排列(推荐inline-block)

![](https://upload-images.jianshu.io/upload_images/2789632-bd7e5f8eef59d25d.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

参考地址:

https://www.cnblogs.com/guanghe/p/10070574.html

https://www.jianshu.com/p/09bd5873bed4

https://blog.csdn.net/qq_39406353/article/details/104455345

http://www.imooc.com/learn/121

https://www.w3school.com.cn/cssref/pr_class_float.asp

https://zhuanlan.zhihu.com/p/81091987

https://www.imooc.com/article/283829

https://blog.csdn.net/chenxi_li/article/details/95192904

目录
相关文章
|
22天前
|
前端开发
【前端|CSS系列第3篇】CSS盒模型、浮动及定位
【前端|CSS系列第3篇】CSS盒模型、浮动及定位
|
2天前
|
前端开发 UED 容器
纯CSS画浮动卡通蓝天白云草坪动画效果
【6月更文挑战第1天】网页设计中,通过创新技巧,可以使用HTML和CSS模拟云朵漂浮和草地动画效果,提升用户体验。背景实现采用线性渐变动画,使背景颜色从浅青至白平滑过渡并循环移动。云朵效果通过多个不同大小和位置的`<div>`元素,结合CSS的`position: absolute;`和`@keyframes`动画实现浮动。草的动画则是通过三角形形状及`::before`和`::after`伪元素创造,配合不同动画速度和角度模拟自然摆动。完整代码可在提供的链接中下载。
16 1
纯CSS画浮动卡通蓝天白云草坪动画效果
尚硅谷html5+css3(4)浮动
尚硅谷html5+css3(4)浮动
|
14天前
|
人工智能 前端开发 容器
CSS 浮动
CSS 浮动
24 1
|
22天前
|
前端开发 开发者 容器
【专栏:CSS基础篇】CSS定位与布局:从静态到浮动、定位
【4月更文挑战第30天】本文介绍了CSS定位与布局的基础,包括静态、相对、绝对、固定定位以及浮动。静态定位遵循HTML顺序,相对定位可在正常位置基础上偏移,不占用额外空间。绝对定位基于最近已定位祖先元素定位,脱离文档流。固定定位相对于浏览器窗口定位,无视页面滚动。浮动用于文字环绕图片等,可能导致父元素高度塌陷。Flexbox布局提供更灵活的元素排列和对齐方式,适配各种复杂布局需求。理解并掌握这些布局技术能提升网页设计的灵活性和响应性。
|
22天前
|
XML 前端开发 JavaScript
CSS中动画、过渡、定位、浮动的作用
CSS中动画、过渡、定位、浮动的作用
|
22天前
|
前端开发 JavaScript 定位技术
css3浮动定位
css3浮动定位
48 0
|
22天前
|
前端开发
【CSS】<Cascading Style Sheets>元素浮动&元素定位
【1月更文挑战第17天】【CSS】<Cascading Style Sheets>元素浮动&元素定位
|
22天前
|
前端开发 容器
CSS:浮动
CSS:浮动
43 0
|
22天前
|
前端开发
CSS float(浮动)
CSS float(浮动)
33 2