目录
弹性盒布局常用元素
在除了用浮动和postion元素等传统方式来进行页面布局以外,在CSS3中又引入了一个功能强大的弹性盒布局flex。
设置方式
给一个外层盒子div设置display:flex;外层元素就是flex容器,里面元素采用flex布局的块级标签的子元素。
代码如下:
<style>
.flex-box{
background-color: lightskyblue;
display: flex;
}
div>div{
background-color: lightcoral;
margin: 20px;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="flex-box">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
</body>
效果:
当给外层设置flex属性值后,里面的子元素div元素默认是块级元素,变成弹性元素后就失去了原有的块级元素的特征,不再独自占一行。除了设置display:flex;将此元素将显示为弹性盒容器以外,还有另一种display:inline-flex;可以设置此元素将显示为行内弹性盒容器。
添加一个设置为display:inline-flex;的盒子和设置display:flex;的盒子进行对比,代码如下:
<style>
.flex-box{
background-color: lightskyblue;
display: flex;
}
.flex-box>div{
background-color: lightcoral;
margin: 20px;
width: 100px;
height: 100px;
}
.inline-flex-box{
background-color: lightblue;
display: inline-flex;
}
.inline-flex-box>div{
background-color: pink;
margin: 20px;
width: 100px;
height: 100px;
}
</style>
<div class="flex-box">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
<div class="inline-flex-box">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
</div>
</body>
效果:
flex-direction
flex-direction用来设置弹性元素的布局方向。
有以下属性值:
- row:默认值,主轴为水平方向(水平布局),起点在左边,垂轴默认从上往下。
- row-reverse:主轴为水平方向(水平布局),起点在右端,垂轴从上往下。
- column:主轴为垂直方向(垂直布局)起点在上方,垂轴为从左向右。
- column-reverse:主轴为垂轴方向(垂直布局)起点在下面,垂轴从左往右
flex-direction: row-reverse;
flex-direction: column;
flex-direction: column-reverse;
flex-wrap
flex-wrap 属性规定 flex 容器是单行或者多行,同时横轴的方向决定了新行堆叠的方向。
- nowrap:默认值,表示不换行
- wrap:换行
- wrap-reverse:换行,第一行在下方
row:
- wrap:
.flex-box{
background-color: lightskyblue;
display: flex;
flex-wrap:wrap;
}
当弹性容器一行无法容纳其中元素时就会自动换行。
- wrap-reverse:
.flex-box{
background-color: lightskyblue;
display: flex;
flex-direction: row;
flex-wrap:wrap-reverse;
}
flex-wrap会对不同的flex-direction属性值产生不同的影响,例如:
当flex-direction为
row-reverse:
- wrap
.flex-box{
background-color: lightskyblue;
display: flex;
flex-direction: row-reverse;
flex-wrap:wrap;
}
- wrap-reverse
.flex-box{
background-color: lightskyblue;
display: flex;
flex-direction: row-reverse;
flex-wrap:wrap-reverse;
}
column:
- wrap
.flex-box{
background-color: lightskyblue;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 500px;
}
- wrap-reverse
.flex-box{
background-color: lightskyblue;
display: flex;
flex-direction: column;
flex-wrap:wrap-reverse;
height: 500px;
}
column-reverse:
- wrap
.flex-box{
background-color: lightskyblue;
display: flex;
flex-direction: column-reverse;
flex-wrap:wrap;
height: 500px;
}
- wrap-reverse
.flex-box{
background-color: lightskyblue;
display: flex;
flex-direction: column-reverse;
flex-wrap:wrap-reverse;
height: 500px;
}
弹性元素主轴上的对齐方式(justify-content)
弹性元素在主轴上的对齐方式是通过justify-content属性来实现的,这个属性一共有6个值
- flex-start:默认值,左对齐
- flex-end:右对齐
- center:居中
- space-between:两端对齐,项目之间的间隔相等
- space-around:项目两侧的间距相同,项目之间的间距比两侧的间距大一倍
- space-evenly:所有弹性元素均匀排放,元素之间的间距折叠
flex-start:
flex-end:
center:
space-between:
space-around:
space-evenly:
弹性元素垂轴上的对齐属性(align-items)
弹性元素在主轴上的对齐方式是通过align-items属性来实现的
- stretch:默认值。元素被拉伸以适应容器。
- center:元素位于容器的中心。
- flex-start:元素位于容器的开头
- flex-end:元素位于容器的结尾。
- baseline:元素位于容器的基线上。
stretch(默认值)
<style>
.flex-box {
background-color: lightskyblue;
display: flex;
height: 500px;
}
.flex-box>div {
background-color: lightcoral;
margin: 20px;
padding:20px;
}
</style>
<body>
<div class="flex-box">
<div>One</div>
<div>Two<br>Two</div>
<div>Three<br>Three<br>Three</div>
<div>Four<br>Four<br>Four<br>Four<br>Four</div>
<div>Five<br>Five<br>Five<br>Five<br>Five<br>Five</div>
</div>
</body>
center
修改上面的代码
align-items: center;
flex-start
修改上代码为:
align-items: flex-start;
flex-end
修改上面代码:
align-items: flex-end;
baseline
不过这里要修改一下第三个div的字体效果才能明显出来
<style>
.flex-box {
background-color: lightskyblue;
display: flex;
height: 500px;
align-items: baseline;
}
.flex-box>div {
background-color: lightcoral;
margin: 20px;
padding: 20px;
}
</style>
<body>
<div class="flex-box">
<div>One</div>
<div>Two<br>Two</div>
<div style="font-size: 2em;">Three<br>Three<br>Three</div>
<div>Four<br>Four<br>Four<br>Four<br>Four</div>
<div>Five<br>Five<br>Five<br>Five<br>Five<br>Five</div>
</div>
</body>
align-self属性
align-self可以单独设置弹性容器中的弹性元素的align-items属性。例如:
<style>
.flex-box {
background-color: lightskyblue;
display: flex;
height: 500px;
align-items: baseline;
}
.flex-box>div {
background-color: lightcoral;
margin: 20px;
padding: 20px;
}
.two{
align-self: flex-end;
}
</style>
<body>
<div class="flex-box">
<div>One</div>
<div class="two">Two<br>Two</div>
<div>Three<br>Three<br>Three</div>
<div>Four<br>Four<br>Four<br>Four<br>Four</div>
<div>Five<br>Five<br>Five<br>Five<br>Five<br>Five</div>
</div>
</body>