赞
踩
定位布局:能够直接地定位元素的布局模式,定位元素奴基本与其他元素没有任何关系。
CSS3引入的flex布局,主要思想是让容器有能力控制子项目的高度,宽度(设置顺序)。以最佳方式填充可用空间(主要是为了适应所有类型的显示设备和屏幕大小)。flex容器会使子项目扩展来填满可用空间,或者缩小以防止溢出容器。
最重要的是flex布局方向不可预知,不像常规的布局(块就是从上到下,内联就是从左到右),常规布局适合页面布局,但是对于支持大型或者复杂的应用程序(特别是涉及方向布局方向的改变,缩放或者收缩)就缺乏灵性。
本文主要是学习最新版本的flex模型。学习之前还是了解基本知识。
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .parent-flex {
- width: 800px;
- height: 400px;
- background-color: rgb(250,234,202);
-
- display: flex;
- flex-direction: row;
- }
-
- .child-flex {
- width: 200px;
- height: 200px;
- background-color: rgb(203,234,253);
- }
- </style>
- </head>
- <body>
- <div class="parent-flex">
- <div class="child-flex">
- flex item1
- </div>
- <div class="child-flex">
- flex item2
- </div>
- <div class="child-flex">
- flex item3
- </div>
- </div>
- </body>
- </html>
当在父元素加上display:flex之后,内部子元素变成了如图的样子,自动排列起来。
测试上面的例子:修改为row-reverse
- .parent-flex {
- width: 800px;
- height: 400px;
- background-color: rgb(250,234,202);
-
- display: flex;
- flex-direction: row-reverse;
- }
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .parent-flex {
- width: 800px;
- height: 400px;
- background-color: rgb(250,234,202);
-
- display: flex;
- flex-direction: row;
- }
-
- .child-flex {
- width: 200px;
- height: 200px;
- background-color: rgb(203,234,253);
- }
- </style>
- </head>
- <body>
- <div class="parent-flex">
- <div class="child-flex">
- flex item1
- </div>
- <div class="child-flex">
- flex item2
- </div>
- <div class="child-flex">
- flex item3
- </div>
- <div class="child-flex">
- flex item4
- </div>
- <div class="child-flex">
- flex item5
- </div>
- </div>
- </body>
- </html>
上面的例子:我又添加了两个div子元素,这种情况下,父元素的宽度不够渲染所有的子元素。我们先来看一下,当前情况下子元素会如何渲染。
- .parent-flex {
- width: 800px;
- height: 400px;
- background-color: rgb(250,234,202);
-
- display: flex;
- flex-direction: row;
- flex-wrap: wrap;
- }
伸缩流方向与伸缩换行的结合物。也就是说flex-flow同时设定了flex-direction和flex-wrap两个属性,也就是这两个属性的缩写,决定了伸缩容器的主轴和侧轴。
flex-flow: <flex-direction> <flex-wrap>;
测试,使用flex-flow实现上面flex-flow的测试例子效果。- .parent-flex {
- width: 800px;
- height: 400px;
- background-color: rgb(250,234,202);
-
- display: flex;
- flex-flow:wrap row ;
- }
当伸缩项目溢出某一行时,这一属性也会在项目的对以上增加一些控制。
justify-content: flex-start | flex-end | center | space-between | space-around;- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- }
-
- .parent {
- width: 800px;
- background-color: rgb(250,234,202);
- display: flex;
- }
-
- .flex-start {
- justify-content: flex-start;
- }
-
- .flex-end {
- justify-content: flex-end;
- }
-
- .flex-center {
- justify-content: center;
- }
-
- .flex-space-between {
- justify-content: space-between;
- }
-
- .flex-space-around {
- justify-content: space-around;
- }
- </style>
- </head>
- <body>
- <div class="flex-start parent">
- <div class="child-flex">
- flex-start
- </div>
- <div class="child-flex">
- flex-start
- </div>
- <div class="child-flex">
- flex-start
- </div>
- </div>
- <br />
- <div class="flex-end parent">
- <div class="child-flex">
- flex-end
- </div>
- <div class="child-flex">
- flex-end
- </div>
- <div class="child-flex">
- flex-end
- </div>
- </div>
- <br />
- <div class="flex-center parent">
- <div class="child-flex">
- center
- </div>
- <div class="child-flex">
- center
- </div>
- <div class="child-flex">
- center
- </div>
- </div>
- <br />
- <div class="flex-space-between parent">
- <div class="child-flex">
- space-between
- </div>
- <div class="child-flex">
- space-between
- </div>
- <div class="child-flex">
- space-between
- </div>
- </div>
- <br />
- <div class="flex-space-around parent">
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- </div>
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- }
-
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
-
- .flex-space-around {
- justify-content: space-around;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- </div>
- </body>
- </html>
如图,可以看到当前三个div元素按照space-around主轴对齐,但是我们想一下侧轴,此时以侧轴方向来说可以看到div元素位置在上边框位置。我们可以通过align-content来进行侧轴方向的位置布局。
- .flex-space-around {
- justify-content: space-around;
- align-items:center;
- }
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
align-content: flex-start | flex-end | center | space-between | space-around | stretch;理解这个属性的还是需要flex-wrap。
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- }
-
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
-
- .flex-space-around {
- justify-content: space-around;
- flex-wrap: wrap;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- </div>
- </body>
- </html>
现在我们使用align-item和align-content的作用效果。
- .flex-space-around {
- justify-content: space-around;
- flex-wrap: wrap;
- align-items: :center;
- }
- .flex-space-around {
- justify-content: space-around;
- flex-wrap: wrap;
- align-content: center;
- }
一个伸缩容器项目是一个伸缩容器的子元素。伸缩容器的文本也是被认为是一个伸缩项目。伸缩项目中内容与普通流一样。伸缩项目都有一个主轴长度和一个侧轴长度。主轴长度是伸缩项目在主轴上的尺寸,侧轴长度是伸缩项目在侧轴上的尺寸。
默认情况下侧轴长度和父元素的侧轴的长度相同。之前说到的align-items: stretch已经说过。
伸缩容器中的伸缩项目默认显示顺序是遵守文档在源码中出现的先后顺序。我们可以通过伸缩项目的显示顺序修改伸缩项目在页面中的显示顺序。
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
order: <integer>;
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- background-color: rgb(203,234,253);
- display: flex;
- justify-content: center;
- align-items: center;
- }
-
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- 1
- </div>
- <div class="child-flex">
- 2
- </div>
- <div class="child-flex">
- 3
- </div>
- </div>
- </body>
- </html>
接下来我们使用order将顺序改为213.
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- background-color: rgb(203,234,253);
- display: flex;
- justify-content: center;
- align-items: center;
- }
-
- .child-flex:nth-child(1) {
- order: 1;
- }
-
- .child-flex:nth-child(3) {
- order: 2;
- }
-
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- 1
- </div>
- <div class="child-flex">
- 2
- </div>
- <div class="child-flex">
- 3
- </div>
- </div>
- </body>
- </html>
align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
align-self: auto | flex-start | flex-end | center | baseline | stretch;
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- display: flex;
- justify-content: center;
- align-items: center;
- }
-
- .child-flex:nth-child(3) {
- align-self: center;
- }
-
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- 1
- </div>
- <div class="child-flex">
- 2
- </div>
- <div class="child-flex">
- 3
- </div>
- </div>
- </body>
- </html>
flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
flex-grow: <number>; /* default 0 */
我们还用上面的例子,只是修改下面的css
- .child-flex:nth-child(3) {
- flex-grow: 1;
- }
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
伸缩项目的放大属性是需要根据空余空间来决定的。下面的例子将所有的子元素都扩大一倍。
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- display: flex;
- justify-content: center;
- align-items: center;
- flex-grow: 1;
- }
flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
flex-shrink: <number>; /* default 1 */
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- display: flex;
- justify-content: center;
- align-items: center;
- }
-
- .child-flex:nth-child(1) {
- flex-shrink: 0;
- }
-
- .child-flex:nth-child(2) {
- flex-shrink: 1;
- }
-
- .child-flex:nth-child(3) {
- flex-shrink: 1;
- }
-
- .parent {
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- 1
- </div>
- <div class="child-flex">
- 2
- </div>
- <div class="child-flex">
- 3
- </div>
- </div>
- </body>
- </html>
flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
flex-basis: <length> | auto; /* default auto */
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- display: flex;
- justify-content: center;
- align-items: center;
- }
-
- .child-flex:nth-child(1) {
- flex-basis: 300px;
- }
-
- .parent {
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- 1
- </div>
- <div class="child-flex">
- 2
- </div>
- <div class="child-flex">
- 3
- </div>
- </div>
- </body>
- </html>
当前例子的第一个子元素宽为200px,flex-basis:300px。可以看到flex-basis的优先级高于width。
flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
.item { flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]}
为什么要理解flex,因为个人认为很有必要。
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- background-color: rgb(203,234,253);
- }
-
- .parent {
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
-
- .flex-space-around {
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- </div>
- </body>
- </html>
如图,子元素没有设置高度和宽度。可以看到,子元素自动充满侧轴的方向,宽度是文字充满的宽度。这里我们需要定义的是默认flex的布局为一条主轴一条侧轴。可能你会说没有两条主轴是因为你横轴的宽度没有超过父元素的宽度。继续看一个例子:
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- background-color: rgb(203,234,253);
- }
-
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- </div>
- </body>
- </html>
这个例子中我们设置了父元素宽度800px,子元素为200px。我们再看当前的页面展示:
可以看到flex自动将子元素的宽度设置为160px。这也更加说明了默认情况一条横轴一条侧轴。除非是我们加上flex-wrap去定义一条主线排不下,可以换行。我们继续上面的例子:
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- flex-wrap: wrap;
- }
这是我们设置flex-wrap的情况下看到的结果,可以看到高度也已经felx处理了。
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- background-color: rgb(203,234,253);
- }
-
- .parent {
- width: 800px;
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- flex-wrap: wrap;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- <div class="child-flex">
- space-around
- </div>
- </div>
- </body>
- </html>
灵活的布局指的是什么呢?
我的理解是flex可以我们的设置自动修改子元素的属性去友好的展示在父元素的内部(个人理解)。个人理解:在原先的布局方式中,float的存在是为了可以自由的控制div的位置。但是现在flex可以实现float的效果。举个例子,父元素中需要将三个div,前面两个放在一起靠在左侧,另一个div需要仅靠在右侧。猛一看好像实现不了。但是felx是可以实现的。
- <!DOCTYPE html>
- <html lang="en-US">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- <style type="text/css">
- div {
- border: 2px solid black;
- border-radius: 5px;
- }
-
- .child-flex {
- width: 200px;
- height: 100px;
- background-color: rgb(203,234,253);
- display: flex;
- justify-content: center;
- align-items: center;
- }
-
- .parent {
- height: 600px;
- background-color: rgb(250,234,202);
- display: flex;
- justify-content: space-between;
- }
- </style>
- </head>
- <body>
- <div class="flex-space-around parent">
- <div class="child-flex">
- <div class="child-flex">
- 1
- </div>
- <div class="child-flex">
- 2
- </div>
- </div>
- <div class="child-flex">
- 3
- </div>
- </div>
- </body>
- </html>
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。