> 该教程整理自 [阮一峰Flexible教程](http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html)
今天介绍常见布局的Flex写法。
你会看到,不管是什么布局,Flex往往都可以几行命令搞定。
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071327.png" alt="" title=""></p>
<p>我的主要参考资料是<a href="http://davidwalsh.name/flexbox-dice" target="_blank">Landon Schropp</a>的文章和<a href="http://philipwalton.github.io/solved-by-flexbox/" target="_blank">Solved by Flexbox</a>。</p>
<h2>一、骰子的布局</h2>
<p>骰子的一面,最多可以放置9个点。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071328.png" alt="" title=""></p>
<p>下面,就来看看Flex如何实现,从1个点到9个点的布局。你可以到<a href="http://codepen.io/LandonSchropp/pen/KpzzGo" target="_blank">codepen</a>查看Demo。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071329.png" alt="" title=""></p>
<p>如果不加说明,本节的HTML模板一律如下。</p>
~~~
<div class="box">
<span class="item"></span>
</div>
~~~
<p>上面代码中,div元素(代表骰子的一个面)是Flex容器,span元素(代表一个点)是Flex项目。如果有多个项目,就要添加多个span元素,以此类推。</p>
<h3>1.1 单项目</h3>
<p>首先,只有左上角1个点的情况。Flex布局默认就是首行左对齐,所以一行代码就够了。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071301.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>设置项目的对齐方式,就能实现居中对齐和右对齐。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071302.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071303.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7507)
<p>设置交叉轴对齐方式,可以垂直移动主轴。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071304.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071305.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071306.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071307.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3>1.2 双项目</h3>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071308.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071309.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071310.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071311.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token property">align-items</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071312.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.item:nth-child(2) </span><span class="token punctuation">{</span>
<span class="token property">align-self</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071313.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.item:nth-child(2) </span><span class="token punctuation">{</span>
<span class="token property">align-self</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3>1.3 三项目</h3>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071314.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.item:nth-child(2) </span><span class="token punctuation">{</span>
<span class="token property">align-self</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.item:nth-child(3) </span><span class="token punctuation">{</span>
<span class="token property">align-self</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3>1.4 四项目</h3>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071315.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">flex-wrap</span><span class="token punctuation">:</span> wrap<span class="token punctuation">;</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> flex-end<span class="token punctuation">;</span>
<span class="token property">align-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071316.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>box<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>column<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>column<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
</code></pre>
<p>CSS代码如下。</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 property">flex-wrap</span><span class="token punctuation">:</span> wrap<span class="token punctuation">;</span>
<span class="token property">align-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.column </span><span class="token punctuation">{</span>
<span class="token property">flex-basis</span><span class="token punctuation">:</span> 100%<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 property">justify-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7496)
<h3>1.5 六项目</h3>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071317.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">flex-wrap</span><span class="token punctuation">:</span> wrap<span class="token punctuation">;</span>
<span class="token property">align-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7498)
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071318.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span>
<span class="token property">flex-wrap</span><span class="token punctuation">:</span> wrap<span class="token punctuation">;</span>
<span class="token property">align-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071319.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>box<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>row<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>row<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>row<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
</code></pre>
<p>CSS代码如下。</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 property">flex-wrap</span><span class="token punctuation">:</span> wrap<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.row</span><span class="token punctuation">{</span>
<span class="token property">flex-basis</span><span class="token punctuation">:</span> 100%<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>
<span class="token selector">.row:nth-child(2)</span><span class="token punctuation">{</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.row:nth-child(3)</span><span class="token punctuation">{</span>
<span class="token property">justify-content</span><span class="token punctuation">:</span> space-between<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7497)
<h3>1.6 九项目</h3>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071320.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">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span>
<span class="token property">flex-wrap</span><span class="token punctuation">:</span> wrap<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2>二、网格布局</h2>
<h3>2.1 基本网格布局</h3>
<p>最简单的网格布局,就是平均分布。在容器里面平均分配空间,跟上面的骰子布局很像,但是需要设置项目的自动缩放。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071321.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid-cell<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid-cell<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid-cell<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
</code></pre>
<p>CSS代码如下。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.Grid </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>
<span class="token selector">.Grid-cell </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7499)
<h3>2.2 百分比布局</h3>
<p>某个网格的宽度为固定的百分比,其余网格平均分配剩余的空间。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071322.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid-cell u-1of4<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid-cell<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Grid-cell u-1of3<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
</code></pre>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.Grid </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>
<span class="token selector">.Grid-cell </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.Grid-cell.u-full </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 0 100%<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.Grid-cell.u-1of2 </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 0 50%<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.Grid-cell.u-1of3 </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 0 33.3333%<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.Grid-cell.u-1of4 </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 0 25%<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7500)
<h2>三、圣杯布局</h2>
<p><a href="https://en.wikipedia.org/wiki/Holy_Grail_(web_design)" target="_blank">圣杯布局</a>(Holy Grail Layout)指的是一种最常见的网站布局。页面从上到下,分成三个部分:头部(header),躯干(body),尾部(footer)。其中躯干又水平分成三栏,从左到右为:导航、主栏、副栏。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071323.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>HolyGrail<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>header</span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>header</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>HolyGrail-body<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>main</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>HolyGrail-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>main</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>nav</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>HolyGrail-nav<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>nav</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>aside</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>HolyGrail-ads<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>aside</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>footer</span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>footer</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span>
</code></pre>
<p>CSS代码如下。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.HolyGrail </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 property">min-height</span><span class="token punctuation">:</span> 100vh<span class="token punctuation">;</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">header,
footer </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.HolyGrail-body </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 property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.HolyGrail-content </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.HolyGrail-nav, .HolyGrail-ads </span><span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/* 两个边栏的宽度设为12em */</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 0 12em<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.HolyGrail-nav </span><span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">/* 导航放到最左边 */</span>
<span class="token property">order</span><span class="token punctuation">:</span> -1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7501)
<p>如果是小屏幕,躯干的三栏自动变为垂直叠加。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token atrule">@media (max-width<span class="token punctuation">:</span> 768px)</span> <span class="token punctuation">{</span>
<span class="token selector">.HolyGrail-body </span><span class="token punctuation">{</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.HolyGrail-nav,
.HolyGrail-ads,
.HolyGrail-content </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> auto<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2>四、输入框的布局</h2>
<p>我们常常需要在输入框的前方添加提示,后方添加按钮。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071324.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>InputAddOn<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>InputAddOn-item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>span</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>input</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>InputAddOn-field<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>button</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>InputAddOn-item<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>button</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
</code></pre>
<p>CSS代码如下。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.InputAddOn </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>
<span class="token selector">.InputAddOn-field </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7502)
<h2>五、悬挂式布局</h2>
<p>有时,主栏的左侧或右侧,需要添加一个图片栏。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071325.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Media<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>img</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Media-figure<span class="token punctuation">"</span></span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span> <span class="token attr-name">alt</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span><span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Media-body<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span>
</code></pre>
<p>CSS代码如下。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.Media </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 property">align-items</span><span class="token punctuation">:</span> flex-start<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.Media-figure </span><span class="token punctuation">{</span>
<span class="token property">margin-right</span><span class="token punctuation">:</span> 1em<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.Media-body </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7504)
<h3>六、固定的底栏</h3>
<p>有时,页面内容太少,无法占满一屏的高度,底栏就会抬高到页面的中间。这时可以采用Flex布局,让底栏总是出现在页面的底部。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071326.png" alt="" title=""></p>
<p>HTML代码如下。</p>
<pre class=" language-markup"><code class=" language-markup">
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Site<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>header</span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>header</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>main</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Site-content<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>main</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>footer</span><span class="token punctuation">></span></span>...<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>footer</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span>
</code></pre>
<p>CSS代码如下。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.Site </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 property">min-height</span><span class="token punctuation">:</span> 100vh<span class="token punctuation">;</span>
<span class="token property">flex-direction</span><span class="token punctuation">:</span> column<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.Site-content </span><span class="token punctuation">{</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 1<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7505)
<h3>七,流式布局</h3>
<p>每行的项目数固定,会自动分行。</p>
<p><img src="http://www.ruanyifeng.com/blogimg/asset/2015/bg2015071330.png" alt="" title=""></p>
<p>CSS的写法。</p>
<pre class=" language-css"><code class=" language-css">
<span class="token selector">.parent </span><span class="token punctuation">{</span>
<span class="token property">width</span><span class="token punctuation">:</span> 200px<span class="token punctuation">;</span>
<span class="token property">height</span><span class="token punctuation">:</span> 150px<span class="token punctuation">;</span>
<span class="token property">background-color</span><span class="token punctuation">:</span> black<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 property">flex-flow</span><span class="token punctuation">:</span> row wrap<span class="token punctuation">;</span>
<span class="token property">align-content</span><span class="token punctuation">:</span> flex-start<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token selector">.child </span><span class="token punctuation">{</span>
<span class="token property">box-sizing</span><span class="token punctuation">:</span> border-box<span class="token punctuation">;</span>
<span class="token property">background-color</span><span class="token punctuation">:</span> white<span class="token punctuation">;</span>
<span class="token property">flex</span><span class="token punctuation">:</span> 0 0 25%<span class="token punctuation">;</span>
<span class="token property">height</span><span class="token punctuation">:</span> 50px<span class="token punctuation">;</span>
<span class="token property">border</span><span class="token punctuation">:</span> 1px solid red<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
[运行一下](http://www.shouce.ren/study/api/s/7506)
<p>(完)</p>