├── README.md ├── LICENSE ├── style.css └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # flex-box-demo Flex 布局示例 2 | 3 | > 感谢阮一峰老师的教程 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html 4 | 5 | 本示例将教程上所有的布局都简单的实现了一遍,预览地址:http://static.vgee.cn/static/index.html 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 JailBreak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 20px; 3 | margin: 0 auto; 4 | background-color: #E2E2E2; 5 | max-width: 1024px; 6 | color: #595B66; 7 | font-family: 'Microsoft Yahei', sans-serif; 8 | } 9 | h1,h2,h3,h4 { 10 | color: #797979; 11 | // border-bottom: solid 1px #797979; 12 | padding: 5px 0; 13 | } 14 | h1, h2 { 15 | border-bottom: solid 2px #797979; 16 | } 17 | p { 18 | font-size: 15px; 19 | background: #797979; 20 | padding: 10px; 21 | border-radius: 5px; 22 | color: white; 23 | line-height: 2; 24 | } 25 | a { 26 | color: #FFD200; 27 | } 28 | .box { 29 | background-color: white; 30 | margin: 0 0 55px; 31 | display: flex; 32 | 33 | } 34 | .box-tall { 35 | height: 800px; 36 | } 37 | .box-item { 38 | width: 200px; 39 | height: 200px; 40 | line-height: 200px; 41 | vertical-align: middle; 42 | margin: 5px; 43 | background-color: #ffd200; 44 | font-size: 100px; 45 | color: white; 46 | text-align: center; 47 | } 48 | .item-tall { 49 | height: 400px; 50 | line-height: 400px; 51 | } 52 | 53 | 54 | .box-1 { 55 | flex-direction: row; 56 | } 57 | 58 | .box-2 { 59 | flex-direction: row; 60 | flex-wrap: nowrap; 61 | } 62 | 63 | .box-3 { 64 | flex-direction: row; 65 | flex-wrap: wrap; 66 | } 67 | 68 | .box-4 { 69 | flex-direction: row; 70 | flex-wrap: wrap-reverse; 71 | } 72 | 73 | .box-5 { 74 | flex-flow: row nowrap; 75 | } 76 | 77 | .box-6 { 78 | justify-content: flex-start; 79 | } 80 | .box-7 { 81 | justify-content: flex-end; 82 | } 83 | .box-8 { 84 | justify-content: center; 85 | } 86 | .box-9 { 87 | justify-content: space-between; 88 | } 89 | .box-10 { 90 | justify-content: space-around; 91 | } 92 | 93 | .box-11 { 94 | align-items: flex-start; 95 | } 96 | .box-12 { 97 | align-items: flex-end; 98 | } 99 | .box-13 { 100 | align-items: center; 101 | } 102 | .box-14 { 103 | align-items: baseline; 104 | } 105 | .box-14 .box-item{ 106 | font-size: 88px; 107 | line-height: initial; 108 | text-decoration: underline; 109 | } 110 | .box-14 .item-tall{ 111 | font-size: 122px; 112 | line-height: initial; 113 | } 114 | .box-15 { 115 | align-items: stretch; 116 | } 117 | .box-15 .box-item { 118 | height: auto; 119 | } 120 | 121 | .box-16 { 122 | flex-wrap: wrap; 123 | align-content: flex-start; 124 | } 125 | .box-17 { 126 | flex-wrap: wrap; 127 | align-content: flex-end; 128 | } 129 | .box-18 { 130 | flex-wrap: wrap; 131 | align-content: center; 132 | } 133 | .box-19 { 134 | flex-wrap: wrap; 135 | align-content: space-between 136 | } 137 | .box-20 { 138 | flex-wrap: wrap; 139 | align-content: space-around; 140 | } 141 | .box-21 { 142 | flex-wrap: wrap; 143 | align-content: stretch; 144 | } 145 | .box-21 .box-item { 146 | height: auto; 147 | } 148 | 149 | .box-22 .order { 150 | order: -1; 151 | } 152 | .box-item div{ 153 | font-size: 14px; 154 | position: relative; 155 | top: -150px; 156 | } 157 | 158 | .box-23 .box-item{ 159 | flex-grow: 1; 160 | width: auto; 161 | } 162 | .box-23 .grow-2 { 163 | flex-grow: 2; 164 | } 165 | 166 | .box-24 .box-item { 167 | width: 400px; 168 | } 169 | .box-24 .shrink{ 170 | flex-shrink: 0; 171 | } 172 | 173 | .box-25 .box-item { 174 | flex-basis: 200px; 175 | width: 400px; // width 将失去作用 176 | } 177 | 178 | .box-26 { 179 | height: 400px; 180 | } 181 | .box-26 .box-item { 182 | align-self: flex-start; 183 | } 184 | .box-26 .end { 185 | align-self: flex-end; 186 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |感谢阮一峰老师的教程http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
11 |
本示例将教程上所有的布局都简单的实现了一遍,并保存GitHub上面
12 |
https://github.com/JailBreakC/flex-box-demo
flex-direction属性决定主轴的方向(即项目的排列方向)。
16 |17 | .box { 18 | flex-direction: row | row-reverse | column | column-reverse; 19 | } 20 |
21 |默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
30 |31 | .box{ 32 | flex-wrap: nowrap | wrap | wrap-reverse; 33 | } 34 |
35 |nowrap(默认):不换行。
36 |wrap:换行,第一行在上方。
46 |wrap-reverse:换行,第一行在下方。
56 |flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
68 |69 | .box { 70 | flex-flow: <flex-direction> || <flex-wrap>; 71 | } 72 |
73 |justify-content属性定义了项目在主轴上的对齐方式。
85 |86 | .box { 87 | justify-content: flex-start | flex-end | center | space-between | space-around; 88 | } 89 |
90 |flex-start(默认值):左对齐
91 |flex-end:右对齐
98 |center: 居中
105 |space-between:两端对齐,项目之间的间隔都相等。
112 |space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
119 |align-items属性定义项目在交叉轴上如何对齐。
128 |129 | .box { 130 | align-items: flex-start | flex-end | center | baseline | stretch; 131 | } 132 | 133 |
134 |flex-start:交叉轴的起点对齐。
135 |flex-end:交叉轴的终点对齐。
142 |center:交叉轴的中点对齐。
149 |baseline: 项目的第一行文字的基线对齐。
156 |stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
163 |align-content属性定义了多根轴线(多行)的对齐方式。如果项目只有一根轴线,该属性不起作用。
172 |173 | .box { 174 | align-content: flex-start | flex-end | center | space-between | space-around | stretch; 175 | } 176 | 177 |
178 |flex-start:交叉轴的起点对齐。
179 |flex-end:与交叉轴的终点对齐。
191 |center:与交叉轴的中点对齐。
203 |space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
215 |space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
227 |stretch(默认值):轴线占满整个交叉轴。
239 |order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
254 |255 | .item { 256 | order: <integer>; 257 | } 258 |
259 |flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
268 |如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
269 |270 | .item { 271 | flex-grow: <number>; /* default 0 */ 272 | } 273 |
274 |flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
282 |如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
负值对该属性无效。
284 | .item { 285 | flex-shrink: <number>; /* default 1 */ 286 | } 287 |
288 |flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
296 |297 | .item { 298 | flex-basis: <length>; | auto; /* default auto */ 299 | } 300 |
301 |flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
309 |该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
310 |311 | .item { 312 | flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ] 313 | } 314 |
315 | 316 |align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。
318 |319 | .item { 320 | align-self: auto | flex-start | flex-end | center | baseline | stretch; 321 | } 322 |
323 |