> 该教程整理自 [阮一峰Flexible教程](http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html)
网页布局(layout)是CSS的一个重点应用。
![](http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071001.gif)
<p>布局的传统解决方案,基于<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/box_model" target="_blank">盒状模型</a>,依赖 <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/display" target="_blank">display</a>属性 + <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/position" target="_blank">position</a>属性 + <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/float" target="_blank">float</a>属性。它对于那些特殊布局非常不方便,比如,<a href="https://css-tricks.com/centering-css-complete-guide/" target="_blank">垂直居中</a>就不容易实现。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071002.png" alt="" title=""></p>
<p>2009年,W3C提出了一种新的方案----Flex布局,可以简便、完整、响应式地实现各种页面布局。目前,它已经得到了所有浏览器的支持,这意味着,现在就能很安全地使用这项功能。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071003.jpg" alt="" title=""></p>
<p>Flex布局将成为未来布局的首选方案。本文介绍它的语法。</p>
<p>以下内容主要参考了下面两篇文章:<a href="https://css-tricks.com/snippets/css/a-guide-to-flexbox/" target="_blank">A Complete Guide to Flexbox</a> 和 <a href="https://scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties" target="_blank">A Visual Guide to CSS3 Flexbox Properties</a>。</p>
<h2>一、Flex布局是什么?</h2>
<p>Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。</p>
<p>任何一个容器都可以指定为Flex布局。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box</span><span class="token punctuation">{</span>
<span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>行内元素也可以使用Flex布局。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box</span><span class="token punctuation">{</span>
<span class="token property">display</span><span class="token punctuation">:</span> inline-flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>Webkit内核的浏览器,必须加上<code>-webkit</code>前缀。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box</span><span class="token punctuation">{</span>
<span class="token property">display</span><span class="token punctuation">:</span> -webkit-flex<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* Safari */</span>
<span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>注意,设为Flex布局以后,子元素的<code>float</code>、<code>clear</code>和<code>vertical-align</code>属性将失效。</p>
<h2>二、基本概念</h2>
<p>采用Flex布局的元素,称为Flex容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为Flex项目(flex item),简称"项目"。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071004.png" alt="" title=""></p>
<p>容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做<code>main start</code>,结束位置叫做<code>main end</code>;交叉轴的开始位置叫做<code>cross start</code>,结束位置叫做<code>cross end</code>。</p>
<p>项目默认沿主轴排列。单个项目占据的主轴空间叫做<code>main size</code>,占据的交叉轴空间叫做<code>cross size</code>。</p>
<h2>三、容器的属性</h2>
<p>以下6个属性设置在容器上。</p>
<blockquote>
<ul>
<li>flex-direction</li>
<li>flex-wrap</li>
<li>flex-flow</li>
<li>justify-content</li>
<li>align-items</li>
<li>align-content</li>
</ul>
</blockquote>
<h3>3.1 flex-direction属性</h3>
<p><code>flex-direction</code>属性决定主轴的方向(即项目的排列方向)。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box </span><span class="token punctuation">{</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> row | row-reverse | column | column-reverse<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071005.png" alt="" title=""></p>
<p>它可能有4个值。</p>
<blockquote>
<ul>
<li><code>row</code>(默认值):主轴为水平方向,起点在左端。</li>
<li><code>row-reverse</code>:主轴为水平方向,起点在右端。</li>
<li><code>column</code>:主轴为垂直方向,起点在上沿。</li>
<li><code>column-reverse</code>:主轴为垂直方向,起点在下沿。</li>
</ul>
</blockquote>
<h3>3.2 flex-wrap属性</h3>
<p>默认情况下,项目都排在一条线(又称"轴线")上。<code>flex-wrap</code>属性定义,如果一条轴线排不下,如何换行。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071006.png" alt="" title=""></p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box</span><span class="token punctuation">{</span>
<span class="token property">flex-wrap</span><span class="token punctuation">:</span> nowrap | wrap | wrap-reverse<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>它可能取三个值。</p>
<p>(1)<code>nowrap</code>(默认):不换行。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071007.png" alt="" title=""></p>
<p>(2)<code>wrap</code>:换行,第一行在上方。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071008.jpg" alt="" title=""></p>
<p>(3)<code>wrap-reverse</code>:换行,第一行在下方。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071009.jpg" alt="" title=""></p>
<h3>3.3 flex-flow</h3>
<p><code>flex-flow</code>属性是<code>flex-direction</code>属性和<code>flex-wrap</code>属性的简写形式,默认值为<code>row nowrap</code>。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box </span><span class="token punctuation">{</span>
<span class="token property">flex-flow</span><span class="token punctuation">:</span> <span class="token ignore"><</span>flex-direction> || <span class="token ignore"><</span>flex-wrap><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3>3.4 justify-content属性</h3>
<p><code>justify-content</code>属性定义了项目在主轴上的对齐方式。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box </span><span class="token punctuation">{</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> flex-start | flex-end | center | space-between | space-around<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071010.png" alt="" title=""></p>
<p>它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。</p>
<blockquote>
<ul>
<li><code>flex-start</code>(默认值):左对齐</li>
<li><code>flex-end</code>:右对齐</li>
<li><code>center</code>: 居中</li>
<li><code>space-between</code>:两端对齐,项目之间的间隔都相等。</li>
<li><code>space-around</code>:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。</li>
</ul>
</blockquote>
<h3>3.5 align-items属性</h3>
<p><code>align-items</code>属性定义项目在交叉轴上如何对齐。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box </span><span class="token punctuation">{</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> flex-start | flex-end | center | baseline | stretch<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071011.png" alt="" title=""></p>
<p>它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。</p>
<blockquote>
<ul>
<li><code>flex-start</code>:交叉轴的起点对齐。</li>
<li><code>flex-end</code>:交叉轴的终点对齐。</li>
<li><code>center</code>:交叉轴的中点对齐。</li>
<li><code>baseline</code>: 项目的第一行文字的基线对齐。</li>
<li><code>stretch</code>(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。</li>
</ul>
</blockquote>
<h3>3.6 align-content属性</h3>
<p><code>align-content</code>属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.box </span><span class="token punctuation">{</span>
<span class="token property">align-content</span><span class="token punctuation">:</span> flex-start | flex-end | center | space-between | space-around | stretch<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071012.png" alt="" title=""></p>
<p>该属性可能取6个值。</p>
<blockquote>
<ul>
<li><code>flex-start</code>:与交叉轴的起点对齐。</li>
<li><code>flex-end</code>:与交叉轴的终点对齐。</li>
<li><code>center</code>:与交叉轴的中点对齐。</li>
<li><code>space-between</code>:与交叉轴两端对齐,轴线之间的间隔平均分布。</li>
<li><code>space-around</code>:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。</li>
<li><code>stretch</code>(默认值):轴线占满整个交叉轴。</li>
</ul>
</blockquote>
<h3>四、项目的属性</h3>
<p>以下6个属性设置在项目上。</p>
<blockquote>
<ul>
<li><code>order</code></li>
<li><code>flex-grow</code></li>
<li><code>flex-shrink</code></li>
<li><code>flex-basis</code></li>
<li><code>flex</code></li>
<li><code>align-self</code></li>
</ul>
</blockquote>
<h3>4.1 order属性</h3>
<p><code>order</code>属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.item </span><span class="token punctuation">{</span>
<span class="token property">order</span><span class="token punctuation">:</span> <span class="token ignore"><</span>integer><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071013.png" alt="" title=""></p>
<h3>4.2 flex-grow属性</h3>
<p><code>flex-grow</code>属性定义项目的放大比例,默认为<code>0</code>,即如果存在剩余空间,也不放大。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.item </span><span class="token punctuation">{</span>
<span class="token property">flex-grow</span><span class="token punctuation">:</span> <span class="token ignore"><</span>number><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* default 0 */</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071014.png" alt="" title=""></p>
<p>如果所有项目的<code>flex-grow</code>属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的<code>flex-grow</code>属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。</p>
<h3>4.3 flex-shrink属性</h3>
<p><code>flex-shrink</code>属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.item </span><span class="token punctuation">{</span>
<span class="token property">flex-shrink</span><span class="token punctuation">:</span> <span class="token ignore"><</span>number><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* default 1 */</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071015.jpg" alt="" title=""></p>
<p>如果所有项目的<code>flex-shrink</code>属性都为1,当空间不足时,都将等比例缩小。如果一个项目的<code>flex-shrink</code>属性为0,其他项目都为1,则空间不足时,前者不缩小。</p>
<p>负值对该属性无效。</p>
<h3>4.4 flex-basis属性</h3>
<p><code>flex-basis</code>属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为<code>auto</code>,即项目的本来大小。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.item </span><span class="token punctuation">{</span>
<span class="token property">flex-basis</span><span class="token punctuation">:</span> <span class="token ignore"><</span>length> | auto<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* default auto */</span>
<span class="token punctuation">}</span>
</code></pre>
<p>它可以设为跟<code>width</code>或<code>height</code>属性一样的值(比如350px),则项目将占据固定空间。</p>
<h3>4.5 flex属性</h3>
<p><code>flex</code>属性是<code>flex-grow</code>, <code>flex-shrink</code> 和 <code>flex-basis</code>的简写,默认值为<code>0 1 auto</code>。后两个属性可选。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.item </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> none | [ <span class="token ignore"><</span><span class="token string">'flex-grow'</span>> <span class="token ignore"><</span><span class="token string">'flex-shrink'</span>>? || <span class="token ignore"><</span><span class="token string">'flex-basis'</span>> ]
<span class="token punctuation">}</span>
</code></pre>
<p>该属性有两个快捷值:<code>auto</code> (<code>1 1 auto</code>) 和 none (<code>0 0 auto</code>)。</p>
<p>建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。</p>
<h3>4.6 align-self属性</h3>
<p><code>align-self</code>属性允许单个项目有与其他项目不一样的对齐方式,可覆盖<code>align-items</code>属性。默认值为<code>auto</code>,表示继承父元素的<code>align-items</code>属性,如果没有父元素,则等同于<code>stretch</code>。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.item </span><span class="token punctuation">{</span>
<span class="token property">align-self</span><span class="token punctuation">:</span> auto | flex-start | flex-end | center | baseline | stretch<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071016.png" alt="" title=""></p>
<p>该属性可能取6个值,除了auto,其他都与align-items属性完全一致。</p>
<p>(完)</p>