CSS3弹性盒子
一、背景
CSS传统布局模式是盒状模型,主要是通过display、float、position属性来实现布局。在2009年,W3C提出了一种新的布局方案,flex布局即弹性盒子,它可以简便、完整、响应式地实现各种页面的布局。使用弹性盒子,也可以对一个容器中的子元素进行排列、对齐和分配空白空间。
二、弹性盒子
弹性容器(Flex container)包含了一个或多个的弹性子元素(Flex item)。任何一个容器都可以指定为弹性盒子,具体方法如:
display: flex | inline-flex;
/*inline-flex是用来实现行内元素的弹性布局*/
/*如果使用webkit内核的话,则需要加上前缀-webkit-flex*/
示例:
当不设置为弹性盒子时的效果:
设置为弹性盒子后:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
}
.flex-item{
width: 200px;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="flexbox">
<div class="flex-item">test1</div>
<div class="flex-item">test2</div>
<div class="flex-item">test3</div>
<div class="flex-item">test4</div>
</div>
</body>
</html>
结果:
当拖动边框使得边框宽度小于设置的容器宽度时,容器内的元素不会像浮动那样子发生换行,而是会进行大小的调整:
1.容器常用的属性
flex-direction
flex-direction指定的是弹性容器中子元素的排列方向(主轴的方向),常见的属性值有:
row:默认值,表示从左往右排列。
row-reverse:从右往左排列。
column:从上往下排列。
column-reverse:从下往上排列。
具体样例:
设置为row:
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
flex-direction: row;
}
.flex-item{
width: 200px;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
</style>
结果:
设置为row-reverse:
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
flex-direction: row-reverse;
}
.flex-item{
width: 200px;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
</style>
结果:
设置为column,结果:
设置为column-reverse,结果:
flex-wrap
flex-wrap表示的是容器的换行规则,规定flex容器是单行或者是多行:常用的值有:
nowrap:默认值,不换行。
wrap:设置为换行。
wrap-reverse:设置为逆序的换行。
具体示例如下:
当值为nowrap时:
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.flex-item{
width: 200px;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
</style>
当值为wrap时:
当值为wrap-reverse时:
flex-flow
flex-flow是容器的方向和换行的简写,默认值是flex-flow: row nowrap;使用和flex-direction、flex-wrap一样,这里来就不作演示了。
justify-content
justify-content表示的是主轴方向的对齐方式。常见的属性有以下几种:
flex-start:向主轴的起点对齐。
center:居中对齐。
flex-end:向主轴的终点对齐。
space-between:第一个和最后一个元素贴边显示,剩余的空白会自动分配,让每两个元素之间的间距是相等的。
space-around:将剩余空间的空白进行分配,也让每两个元素之间的间距是相等的。
具体示例:
当值设置为flex-start时:
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
justify-content: flex-start;
}
.flex-item{
width: 200px;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
</style>
结果:
当值设置为center时:
当值设置为flex-end时:
当值为space-between时:
当值为space-around时:
align-items
align-items表示的是交叉轴方向(与主轴垂直)的对齐方式,常见的值有:
stretch:默认值,如果子元素没有设置高度,会按照容器百分百的高度显示。
baseline:让所有文字在同一基线上显示。
具体示例:
当值stretch时:
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
justify-content: space-around;
align-items: stretch;
}
.flex-item{
width: 200px;
/* height: 200px; */
background-color: #FFC0CB;
border: 1px solid black;
}
</style>
结果:
当值为baseline时:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
justify-content: space-around;
align-items: baseline;
}
.flex-item{
width: 200px;
text-align: center;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
.f30{
font-size: 30px;
}
.f20{
font-size: 20px;
}
</style>
</head>
<body>
<div class="flexbox">
<div class="flex-item f30">test1</div>
<div class="flex-item">test2</div>
<div class="flex-item f20">test3</div>
<div class="flex-item">test4</div>
</div>
</body>
</html>
结果:
当值为flex-start时:
当值为flex-end、center时,道理同justify-content一样,就不展示啦。
align-content
align-content表示的是含多轴线的对齐方式,是指将整行元素进行对齐,如果窗口尺寸缩小的话也会相应地进行对齐,常见的值有:
flex-start:基于起点的对齐方式。
flex-end:基于终点的对齐方式。
center:居中的对齐方式。
space-between:头和尾贴边,中间的元素保持相同间距。
space-around:元素保持同样间距。
stretch:默认值。
具体示例:
当值为flex-start时:
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
.flex-item{
width: 200px;
text-align: center;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
</style>
结果:
当值为flex-end时:
当值为center时:
当值为space-between时:
当值为space-around时:
2.子元素常用属性
order
order设置的是项目的排列顺序,数值越小越靠前,默认是0。具体使用如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
}
.flex-item{
width: 200px;
text-align: center;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
.o1{/*给test1设置order为1*/
order: 1;
}
</style>
</head>
<body>
<div class="flexbox">
<div class="flex-item o1">test1</div>
<div class="flex-item">test2</div>
<div class="flex-item">test3</div>
<div class="flex-item">test4</div>
</div>
</body>
</html>
结果test1的显示就放在了最后:
flex-grow
flex-grow定义的是项目的放大比例,默认为0(如果存在剩余空间,也不放大)。即按子元素的比例显示,具体示例:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
}
.flex-item{
width: 200px;
text-align: center;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
.g1{
flex-grow: 1;
}
.g2{
flex-grow: 2;
}
.g3{
flex-grow: 3;
}
</style>
</head>
<body>
<div class="flexbox">
<div class="flex-item g1">test1</div>
<div class="flex-item g2">test2</div>
<div class="flex-item g3">test3</div>
<div class="flex-item">test4</div>
</div>
</body>
</html>
这里的子元素放大比例为:1:2:3:0,因此显示结果如下,但如果进行窗口的缩放,放大比例会失效:
flex-shrink
flex-shrink设置的是项目的缩小比例,默认为1(如果空间不足,改元素会缩小),当值设置为0的时候,对窗口大小进行缩小,值为0的元素会保持原来大小不进行缩放,具体使用如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
<style type="text/css">
.flexbox{
background-color: #00FFFF;
height: 1000px;
display: flex;
}
.flex-item{
width: 200px;
text-align: center;
height: 200px;
background-color: #FFC0CB;
border: 1px solid black;
}
.s1{
flex-shrink: 2;
}
.s2{
flex-shrink: 0;
}
.s3{
flex-shrink: 3;
}
</style>
</head>
<body>
<div class="flexbox">
<div class="flex-item s1">test1</div>
<div class="flex-item s2">test2</div>
<div class="flex-item s3">test3</div>
<div class="flex-item">test4</div>
</div>
</body>
</html>
结果(缩放后):
flex-basis
flex-basis表示项目在分配多余的空间之前,占据主轴的空间大小,其实就是项目在缩放之前的尺寸大小,常见的值有:
auto:默认值,项目本来大小。
length:项目将占据固定空间。
这里就不作演示了。
flex
flex用来设置弹性盒子对象的子元素分配空间。其实就是对上面三个属性的缩写,语法为:
flex:none | [flex-grow][flex-shrink][flex-basis];
该属性有两个快捷值:auto(值为:1 1 auto)和none(值为:0 0 auto)。
align-self
align-self用来设置单个项目的对齐方式,可覆盖align-items。常见的值有:
auto:默认值,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
还有其他的值如stretch、baseline、flex-start、flex-end、center,和align-items同理,这里就不作演示了。
3.注意
当我们设置了flex布局后,子元素的float、clear和vertical-align属性都将失效。
这一部分的内容到这里就结束啦,有不足的地方也希望大家帮我指出来,蟹蟹!