blog/docs/code/html/css-float.md

45 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: 浮动
date: 2022-01-09 00:19:44
tags:
- CSS
categories: CSS
author: Anges黎梦
---
## 浮动特性
1、浮动元素有左浮动(float:left)和右浮动(float:right)两种
2、浮动的元素会向左或向右浮动碰到父元素边界、其他元素才停下来
3、相邻浮动的块元素可以并在一行超出父级宽度就换行
4、浮动让行内元素或块元素转化为有浮动特性的行内块元素(此时不会有行内块元素间隙问题)
5、父元素如果没有设置尺寸(一般是高度不设置),父元素内整体浮动的子元素无法撑开父元素,父元素需要清除浮动
## 清除浮动
- 父级上增加属性overflowhidden
- 在最后一个子元素的后面加一个空的div给它样式属性 clear:both不推荐
- 使用成熟的清浮动样式类clearfix
```css
.clearfix:after,.clearfix:before{ content: "";display: table;}
.clearfix:after{ clear:both;}
.clearfix{zoom:1;}
```
清除浮动的使用方法:
```css
.con2{... overflow:hidden}
或者
<div class="con2 clearfix">
```