flex布局
1. flex是什么
2. flex解决了什么问题
- 块元素的垂直居中
- 元素在窗口中的自动伸缩,适应容器大小的变化,特别适合移动端布局
3. flex布局的角色有哪些
- 只有二级:父级和子级
- 父元素:flex容器(container)
- 子元素:flex项目或flex元素*(item)
4. flex项目的布局方向
- flex是一维布局:项目要么水平排列,要么垂直排列,任何时候只能沿一个方向排列
- flex项目的排列方向称为主轴,排列方式有两种,所以主轴也有两种:水平/行轴、垂直/纵轴
- 与主轴对应的轴称为交叉轴或副轴、侧轴
5. 可以用在flex容器上的属性有哪些
- flex-direction:设置容器中的主轴方向,默认为行轴,有两个选项:
row
和column
- flex-wrap:设置元素是否允许换行,默认不换行,有两个选项:
wrap
和nowrap
- flex-flow:是上面两个属性的简写,第一个参数是主轴方向,第二个参数是否允许换行
- justify-content:设置容器中的项目在主轴上的对齐方式,有三个选项:
flex-start
、flex-end
和center
,默认为flex-start
;另外还可以可以分配主轴上的剩余空间,有三个选项:space-between
、space-around
和space-evenly
- align-items:项目在单行容器在交叉轴上的排列方式,有三个选项:
flex-start
、flex-end
和center
,默认为flex-start
- align-content:项目在多行容器在交叉轴上的排列方式,参数及意义与
justify-content
相同
1220作业总结
- 没有看老师给的素材就开始做,增加了工作量,效果还不好,很多效果没有做上,比如:logo的鼠标效果;顶部右侧快速入口的鼠标效果
- 过多的使用
float
,就像老师说的:float是魔鬼,能不用就不用
,容易造成布局错乱,兼容性差 - 学到了两个知识点:
+
选择相邻元素:input[type='search'] + label
表示input相邻的label
元素img
图片下部分有空白,可以用display:block
或者font-size:0
去除
- 有个问题老师可能没看到:中部菜单右侧的文字部分,宽度都设置25%,使用
float:left
时,一行能显示4个,两行正好显示8个,而使用display:inline-block
时,一行只能显示3个,8个分成了3行,display:inline-block
有什么特殊之处?
1223作业
代码练习
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flex练习</title>
<style>
.container{
display: flex;
width: 620px;
/*border:2px dashed lightblue;*/
background-color: gray;
box-sizing: border-box;
}
.item{
width: 100px;
height: 50px;
border:1px dashed red;
background-color: lightgreen;
box-sizing: border-box;
text-align: center;
line-height: 50px;
}
.container.demo2{ /**允许换行**/
height: 200px;
flex-wrap: wrap;
}
.container.demo3{ /**纵轴换行**/
height: 200px;
flex-flow:column wrap;
}
.container.demo4{ /**居中对齐方式**/
height: 200px;
flex-flow:row wrap;
justify-content: center;
}
.container.demo5{ /**右对齐方式**/
height: 200px;
flex-flow:row wrap;
justify-content: flex-end;
}
.container.demo6{ /**间距**/
height: 200px;
flex-flow:row wrap;
justify-content: space-around;
}
.container.demo7{ /**间距**/
height: 200px;
flex-flow:row wrap;
justify-content: space-between;
}
.container.demo8{ /**纵轴间距**/
height: 200px;
flex-flow:column wrap;
justify-content: space-evenly;
}
.container.demo9{ /**副轴多行**/
height: 200px;
flex-flow:row wrap;
justify-content: space-between;
align-content: space-evenly;
}
.container.demo10{ /**副轴单行**/
height: 200px;
flex-flow:row wrap;
justify-content: space-between;
align-items: center;
}
</style>
</head>
<body>
<h1>demo1:行轴不换行</h1>
<div class="container demo1">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo2:行轴换行</h1>
<div class="container demo2">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo3:flex-flow方式</h1>
<div class="container demo3">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo4:对齐方式</h1>
<div class="container demo4">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo5:右对齐方式</h1>
<div class="container demo5">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo6:间距</h1>
<div class="container demo6">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo7:间距</h1>
<div class="container demo7">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo8:纵轴间距</h1>
<div class="container demo8">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo9:行轴主轴,副轴多行排列方式</h1>
<div class="container demo9">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
<span class="item">6</span>
<span class="item">7</span>
<span class="item">8</span>
</div>
<hr>
<h1>demo10:行轴主轴,副轴单行排列方式</h1>
<div class="container demo9">
<span class="item">1</span>
<span class="item">2</span>
<span class="item">3</span>
<span class="item">4</span>
<span class="item">5</span>
</div>
</body>
</html>
属性复习