1、将课堂中的全部案例照写一遍,并达到默写级别
demo1.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>设置弹性元素的增长因子</title>
<link rel="stylesheet" href="css/style1.css">
</head>
<body>
<h1>设置弹性元素的增长因子</h1>
<h3>1、所有弹性元素不增长,以原始宽度显示,增长因子为0(默认)</h3>
<div class="container flex demo1">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>2、将所有剩余空间分配给指定弹性元素,例如:第三个</h3>
<div class="container flex demo2">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>3、全部剩余空间按增长因子在不同弹性元素间分配</h3>
<div class="container flex demo3">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>4、增长因子支持小数,因为是按增长比例分配</h3>
<div class="container flex demo4">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>5、每个弹性元素宽度不同时,同样适用以上分配规律</h3>
<div class="container flex demo5">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
</body>
</html>
style1.css
@import"public.css";
.container{
width: 550px;
}
.item{
width:100px;
}
.demo1>.item{
flex-grow: 0;
}
/*demo2将剩余空间分配给第三个元素*/
.demo2>.item:first-of-type{
flex-grow: 0;
}
.demo2>.item:nth-of-type(2){
flex-grow: 0;
}
.demo2>.item:last-of-type{
flex-grow:1
}
/*demo3将剩余空间按照增长因子比例分配,(550-100*3)/(总的增长因子)*每个元素所占的增长因子*/
.demo3>.item:first-of-type{
flex-grow: 1;
}
.demo3>.item:nth-of-type(2){
flex-grow: 2;
}
.demo3>.item:last-of-type{
flex-grow: 5;
}
.demo4>.item:first-of-type{
flex-grow: 0.5;
}
.demo4>.item:nth-of-type(2){
flex-grow: 0.8;
}
.demo4>.item:last-of-type{
flex-grow: 2.3;
}
.demo5>.item:first-of-type{
width: 50px;
flex-grow: 2.5;
}
.demo5>.item:nth-of-type(2){
width: 150px;
flex-grow: 1.2;
}
.demo5>.item:last-of-type{
width:100px;
flex-grow: 1.8;
}
demo2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>设置弹性元素的缩减因子</title>
<link rel="stylesheet" href="css/style2.css">
</head>
<body>
<h1>flex-shrink:设置弹性元素的缩减因子</h1>
<h3>1、所有弹性元素不缩减,以原始宽度显示</h3>
<div class="container flex demo1">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>2、所有弹性元素自适应容器宽度且不换行,缩减因子:(默认)</h3>
<div class="container flex demo2">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>3、当三个弹性元素的缩减因子不相等时</h3>
<div class="container flex demo3">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>4、缩减因子也可以是小数,只要大于就可以</h3>
<div class="container flex demo4">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>5、当每个弹性元素宽度不一样时,完全是另一道风景线</h3>
<div class="container flex demo5">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
</body>
</html>
style2.css
@import"public.css";
.container{
width: 550px;
}
.item{
width:300px;
}
.demo1>.item{
flex-shrink: 0;
}
.demo2>.item:first-of-type{
flex-shrink: 1;
}
.demo2>.item:nth-of-type(2){
flex-shrink: 1;
}
.demo2>.item:last-of-type{
flex-shrink: 1;
}
.demo3>.item:first-of-type{
flex-shrink: 10;
}
.demo3>.item:nth-of-type(2){
flex-shrink: 2;
}
.demo3>.item:last-of-type{
flex-shrink: 7;
}
.demo4>.item:first-of-type{
flex-shrink: 1.3;
}
.demo4>.item:nth-of-type(2){
flex-shrink: 2.5;
}
.demo4>.item:last-of-type{
flex-shrink: 3.7;
}
.demo5>.item:first-of-type{
width: 200px;
flex-shrink: 2.2;
} /*200-200*2.2*0.12067=146.9045*/
.demo5>.item:nth-of-type(2){
width: 280px;
flex-shrink: 1.7;
} /*280-280*1.7*0.12067=222.561*/
.demo5>.item:last-of-type{
width: 300px;
flex-shrink: 3.3;
} /*300-300*3.3*0.12067=180.5367*/
/*等待缩减的空间/每个弹性元素的宽度与缩减因子乘积的总和*/
/*230/((200*2.2)+(280*1.7)+(300*3.3))=0.12067*/
demo3.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>设置弹性元素的基准尺寸</title>
<link rel="stylesheet" href="css/style3.css">
</head>
<body>
<h1>flex-basis:设置弹性元素的基准尺寸</h1>
<h3>1、在未设置弹性元素宽度时,以内容宽度显示</h3>
<div class="container flex demo1">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>2、存在自定义元素宽度时,则以该宽度显示</h3>
<div class="container flex demo2">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>3、自动状态下,由浏览器根据预设值自行判定</h3>
<div class="container flex demo3">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>4、当元素存在自定义宽度与基准宽度时,以基准宽度为准</h3>
<div class="container flex demo4">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>5、元素基准宽度支持百分比设置</h3>
<div class="container flex demo5">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
</body>
</html>
style3.css
@import "public.css";
.container{
width:550px;
}
.demo1>.item{
flex-basis: content;
}
.demo2>.item{
flex-basis:100px;
}
.demo3>.item{
flex-basis: auto;
}
.demo4>.item{
width:100px;
flex-basis: 150px; /*基准宽度*/
}
.demo5>:first-child{
flex-basis: 20%; /*550*0.2=110*/
}
.demo5>:nth-child(2){
flex-basis: 30%;
}
.demo5>:last-child{
flex-basis: 50%;
}
demo4.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>简化弹性元素的基本设置</title>
<link rel="stylesheet" href="css/style4.css">
</head>
<body>
<h1>flex:简化弹性元素的基本设置</h1>
<h3>1:根据宽度计算,允许缩减适应容器</h3>
<div class="container flex demo1">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>2:根据宽度计算,元素完全弹性以适应容器</h3>
<div class="container flex demo2">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>3:元素完全失去弹性,以原始大小呈现</h3>
<div class="container flex demo3">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>4:一个数值表示增长因子</h3>
<div class="container flex demo4">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>5:第三个有具体数值时,以它为计算标准</h3>
<div class="container flex demo5">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
<h3>6:第三个有具体数值时,以它为计算标准</h3>
<div class="container flex demo6">
<span class="item">item1</span>
<span class="item">item2</span>
<span class="item">item3</span>
</div>
</body>
</html>
style4.css
@import"public.css";
.container{
width: 550px;
}
.demo1>.item{
width: 100px;
height:60px;
/*flex: 0 1 auto;*/
flex:initial;
}
.demo2>.item{
width:100px;
height:60px;
/*flex:1 1 auto;*/
flex:auto;
}
.demo3>.item{
width:100px;
height:60px;
/*flex:0 0 auto;*/
flex:none;
}
.demo4>.item{
width:100px;
height:60px;
flex:1; /*只有一个增长因子,均分剩余空间*/
}
.demo5>.item{
width;100px;
height:60px;
flex:1 1 200px;
}
.demo6>.item{
width:100px;
height:60px;
}
.dmeo6>.item:first-of-type{
flex: 0 1 10%;
} /*增长因子,缩减因子,基准尺寸*/
2、将flex属性的用法,手抄,建议二遍以上
flex属性的用法:
flex-grow :定义项目的放大比例,默认为0表示不放大, 即就算存在剩余空间也不放大
flex-shrink :定义了项目的缩小比例,默认为1,即如何空间不足,则自动缩小项目来填充
flex-basis :定义了项目占据的主轴空间,默认值为auto, 即项目原始大小
flex :是flex-grow,flex-shrink,flex-basis简写,默认值: 0 1 auto, 后二个属性可选
3、自学align-self,order的用法
align-self :个性化定定制某单个项目的对齐方式,可覆盖容器align-items
属性,默认auto,表示继承父元素的align-items
,如果没有父元素,则等价于stretch
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
order :定义项目排列顺序,索引越小越靠前,默认为0
.item {
order: integer;
}
4、试着将之前的一些案例用flex布局改定,例如圣杯布局
demo5.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>flex实现圣杯布局</title>
<link rel="stylesheet" href="css/style5.css">
</head>
<body>
<header>头部</header>
<main>
<article>主体</article>
<aside>左边框</aside>
<aside>右边框</aside>
</main>
<footer>底部</footer>
</body>
</html>
style5.css
*{
margin: 0;
padding: 0;
}
body{
height: 100vh;
display: flex;
flex-flow: column nowrap;
}
header,footer{
box-sizing: border-box;
background-color: lightgray;
height: 50px;
}
main{
box-sizing: border-box;
flex: 1;
display: flex;
background-color: mediumaquamarine;
}
main>aside{
box-sizing: border-box;
width: 200px;
}
main>aside:first-of-type{
background-color:lightpink;
order: -1; /*调整弹性元素在主轴上的顺序,默认为0,允许负值或其他整数,这里让左边栏到最左边*/
}
main>aside:last-of-type{
background-color: lightpink;
}
main>article{
box-sizing: content-box;
flex: 1;
}