Flex布局:實戰

來源:互聯網
上載者:User

轉載自http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

你會看到,不管是什麼布局,Flex往往都可以幾行命令搞定。

我只列出代碼,詳細的文法解釋請查閱《Flex布局教程:文法篇》。我的主要參考資料是Landon Schropp的文章和Solved by Flexbox。 一、骰子的布局

骰子的一面,最多可以放置9個點。

下面,就來看看Flex如何?,從1個點到9個點的布局。你可以到codepen查看Demo。

如果不加說明,本節的HTML模板一律如下。

<div class="box">  <span class="item"></span></div>

上面代碼中,div元素(代表骰子的一個面)是Flex容器,span元素(代表一個點)是Flex項目。如果有多重專案,就要添加多個span元素,以此類推。 1.1 單項目

首先,只有左上方1個點的情況。Flex布局預設就是首行靠左對齊,所以一行代碼就夠了。

.box {  display: flex;}

設定項目的對齊,就能實現置中對齊和靠右對齊。

.box {  display: flex;  justify-content: center;}

.box {  display: flex;  justify-content: flex-end;}

設定交叉軸對齊,可以垂直移動主軸。

.box {  display: flex;  align-items: center;}

.box {  display: flex;  justify-content: center;  align-items: center;}

.box {  display: flex;  justify-content: center;  align-items: flex-end;}

.box {  display: flex;  justify-content: flex-end;  align-items: flex-end;}
1.2 雙項目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.