浮动
标准流
标准流也叫文档流,指的是标签在页面中默认的排布规则,例如:块元素独占一行,行内元素可以一行显示多个。
浮动
作用:让块元素水平排列。
属性名:float
属性值
-
left:左对齐
-
right:右对齐
特点:
- 浮动后的盒子顶对齐
- 浮动后的盒子具备行内块特点
- 浮动后的盒子脱标,不占用标准流
/* 特点:顶对齐:具备行内块显示模式特点;浮动的盒子会脱标 */
.div1 {
width: 200px;
height: 200px;
background-color: red;
float: left;
}
.div2 {
width: 400px;
height: 400px;
background-color: green;
}
<div class="div1">div1</div>
<div class="div2">div2</div>
版心:左右 右边8各个产品
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul li {
list-style: none;
}
.product {
margin: 50px auto;
width: 1226px;
height: 628px;
background-color: #00ffee;
}
.left {
float: left;
width: 243px;
height: 628px;
background-color: #f06b1f;
}
.right {
float: right;
width: 978px;
height: 628px;
background-color: #e600ff;
}
.right li {
float: left;
margin-bottom: 14px;
margin-right: 14px;
width: 234px;
height: 300px;
background-color: #b44d4d;
}
/* 第四个li和第8个li 去掉右侧的margin大小 */
.right li:nth-child(4n) {
margin-right: 0;
}
/* 细节:如果父级宽度不够,浮动的盒子会掉下来 */
<!-- 版心:左右 右边8各个产品 -->
<div class="product">
<div class="left"></div>
<div class="right">
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
清除浮动
场景:浮动元素会脱标,如果父级没有高度,子级无法撑开父级高度(可能导致页面布局错乱)
解决方法:清除浮动(清除浮动带来的影响)
方法一:额外标签法
在父元素内容的最后添加一个块级元素,设置CSS属性 clear:both
.product {
margin:0 auto;
width: 1226px;
/*height: 628px;*/
background-color: #00ffee;
}
.left {
float: left;
width: 243px;
height: 628px;
background-color: #f06b1f;
}
.right {
float: right;
width: 978px;
height: 628px;
background-color: #e600ff;
}
.other {
height: 300px;
background-color: #49e3fd;
}
.clearfix {
clear: both;
}
<div class="product">
<div class="left"></div>
<div class="right"></div>
<div class="clearfix"></div>
</div>
<div class="other"></div>
方法二:单伪元素法
.clearfix::after{
content:""
display: block;
clear: both;
}
<div class="product clearfix">
<div class="left"></div>
<div class="right"></div>
<div class="clearfix"></div>
</div>
<div class="other"></div>
方法三:双伪元素法(推荐)
.clearfix::before,
.clearfix::after{
content:"";
display: table;
}
.clearfix::after {
clear:both;
}
<div class="product clearfix">
<div class="left"></div>
<div class="right"></div>
<div class="clearfix"></div>
</div>
<div class="other"></div>
方法四: overflow
- 父元素添加 CSS 属性 overflow: hidden
.product {
margin:0 auto;
width: 1226px;
/*height: 628px;*/
background-color: #00ffee;
overflow: hidden;
}
<div class="product clearfix">
<div class="left"></div>
<div class="right"></div>
<div class="clearfix"></div>
</div>
<div class="other"></div>
拓展:浮动本质作用是实现图文混排效果
Flex
Flex 布局也叫弹性布局, 是浏览器提倡的布局模型,非常适合结构化布局,提供了强大的空间分布和对齐能力。
Flex模型不会产生浮动布局中脱标现象,布局网页更简单、更灵活。
Flex-组成
设置方式:给父元素设置 display: flex,自动挤压或拉伸
组成部分:
- 弹性容器
- 弹性盒子
- 主轴:默认在水平方向
- 侧轴/交叉轴:默认在垂直方向

/* 弹性容器 */
.box {
display: flex;
height: 300px;
border: 1px solid red;
}
/* 弹性盒子:沿着主轴方向排列*/
.box div {
list-style: none;
width: 100px;
height: 100px;
background-color: orange;
}
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
Flex 布局
| 描述 | 属性 |
|---|---|
| 创建 flex 容器 | display:flex |
| 主轴对齐方式 | justify-content |
| 侧轴对齐方式 | align-items |
| 某个弹性盒子侧轴对齐方式 | align-self |
| 修改主轴方向 | flex-direction |
| 弹性伸缩比 | flex |
| 弹性盒子换行 | flex-wrap |
| 行对齐方式 | align-content |
主轴对齐方式
属性值: justify-content
| 属性名 | 效果 |
|---|---|
| flex-start | 默认值,弹性盒子从起点开始依次排列 |
| flex-end | 弹性盒子从终点开始依次排列 |
| center | 弹性盒子沿主轴居中排列 |
| space-between | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子之间 |
| space-around | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子两侧 |
| space-evenly | 弹性盒子沿主轴均匀排列,弹性盒子与容器之间间距相等 |
/* 弹性容器 */
.box {
display: flex;
/* 从左侧开始排列 */
/*justify-content: start;*/
/* 从右侧开始排列 */
/*justify-content: end;*/
/* 居中 */
/*justify-content: center;*/
/* 父级剩余的尺寸分配成间距 */
/* 弹性盒子之间的间距相等 */
/*justify-content: space-between;*/
/* 间距出现在弹性盒子两侧 */
/* 视觉效果:弹性盒子之间的间距是两端间距的2倍 */
/*justify-content: space-around;*/
/* 各个间距都相等 */
justify-content: space-evenly;
height: 300px;
border: 1px solid red;
}
侧轴对齐方式
属性名
-
align-items: 当前弹性容器内所有弹性盒子的侧轴对齐方式(给弹性容器设置)
-
align-self: 单独控制某个弹性盒子的侧轴对齐方式(给弹性盒子设置)
属性值 效果 stretch 弹性盒子沿着侧轴线被拉伸至铺满容器(弹性盒子没有设置侧轴方向尺寸则默认拉伸) center 弹性盒子沿侧轴居中排列 flex-start 弹性盒子从起点开始依次排列 flex-end 弹性盒子从终点开始依次排列
/* 弹性容器 */
.box {
display: flex;
/* 弹性盒子在侧轴方向没有尺寸才能拉伸 */
/*align-items: stretch;*/
/*居中*/
/*align-items: center;*/
/*从左上开始*/
/*align-items: flex-start;*/
/*从左下开始*/
align-items: flex-end;
height: 300px;
border: 1px solid red;
}
/*第二个div 侧轴居中对齐*/
.box div:nth-child(2){
align-self: center;
}
/* 弹性盒子:沿着主轴方向排列*/
.box div {
list-style: none;
width: 100px;
height: 100px;
background-color: orange;
}
修改主轴方向
主轴默认在水平方向,侧轴默认在垂直方向
属性名:flex-direction
属性值
| 属性值 | 效果 |
|---|---|
| row | 水平方向,从左向右(默认) |
| column | 垂直方向,从上向下 |
| row-reverse | 水平方向,从右向左 |
| column-reverse | 垂查方向,从下向上 |
.model {
display: flex;
/*修改主轴方向 垂直方向:侧轴自动变换到水平方向*/
flex-direction: column;
/*侧轴在水平,视觉效果:水平居中*/
align-items: center;
/*主轴在垂直方向 视觉效果:垂直居中*/
justify-content: center;
width: 200px;
height: 200px;
background-color: #da3838;
}
img {
width: 40px;
height: 40px;
}
<div class="model">
<img src="./image/img.png" alt="">
<div>媒体</div>
</div>
弹性伸缩比
作用:控制弹性盒子的主轴方向的尺寸。
属性名:flex
属性值: 整数数字 表示占用父级剩余尺寸的份数
.box {
display: flex;
flex-direction: column;
height: 300px;
border: 1px solid red;
}
.box div:nth-child(1) {
width: 100px;
background-color: #b41616;
}
.box div:nth-child(2) {
flex: 1;
background-color: #b41616;
}
.box div:nth-child(3) {
flex: 2;
background-color: #1c6ad0;
}
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
弹性盒子换行
弹性盒子可以自动挤压或拉伸,默认情况下,所有弹性盒子都在一行显示。
属性名:flex-wrap
属性值
- wrap:换行
- nowrap:不换行(默认)
行对齐方式
属性名:align-content
属性值
| 属性值 | 效果 |
|---|---|
| flex-start | 默认值,弹性盒子从起点开始依次排列 |
| fex-end | 弹性盒子从终点开始依次排列 |
| center | 弹性盒子沿主轴居中排列 |
| space-between | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子之间 |
| space-around | 弹性盒子沿主轴均匀排列,空白间距均分在弹性盒子两侧 |
| space-evenly | 弹性盒子沿主轴均匀排列,弹性盒子与容器之间间距相等 |
/* 弹性容器 */
.box {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
/* 调整方式 行对齐方式:对单行弹性盒子不生效 */
/*align-content: flex-start;*/
/*align-content: flex-end;*/
/*align-content: space-around;*/
/*align-content: space-between;*/
align-content: space-evenly;
height: 800px;
width: 1000px;
border: 1px solid red;
}
.box div {
width: 300px;
height: 200px;
background-color: #c92121;
}
<div class="box">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
</div>

浙公网安备 33010602011771号