flex布局


什么是flex布局

flex布局即為彈性布局,使用display:flex進行布局,此布局使得盒模型布局更易使用。值得注意的是設為flex布局后,子元素的float、clear和vertical-align屬性將失效。

flex的使用

當元素使用flex后,該元素則成為flex容器(container),其容器屬性有:flex-direction、flex-wrap、flex-flow、justify-content、align-items、align-content。該容器中的元素則成為flex項目(item),其項目屬性有:order、flex-grow、flex-shrink、flex-basis、flex、align-self(此6屬性父元素必須為flex容器,否則失效),下面僅對容器屬性做說明。

flex-direction

共有4個值:row(默認值) | row-reverse | column | column-reverse;
flex-direction:row,從左至右依次排列。
image
flex-direction:row-reverse,從右到左依次排列。
image
flex-direction:column;從上到下依次排列。
image
flex-direction:column-reverse;從下到上依次排列
image

flex-wrap

共有3個值:nowrap(默認值) | wrap | wrap-reverse;
flex-wrap: nowrap;不換行。
image
flex-wrap:wrap ;換行。
image
flex-wrap: wrap-reverse;換行,但第一排在下面。
image

flex-flow

該屬性是flex-direction與flex-wrap屬性的簡寫,flex-flow有他們所有的值。

justify-content

該屬性有五個值:flex-start(默認值) | flex-end | center | space-between | space-around;
justify-content:flex-start;左對齊
image
justify-content:flex-end;右對齊。
image
justify-content:center;居中。
image
justify-content:space-between;均勻分布,但沒有額外margin。
image
justify-content:space-around; 均勻分布,有額外margin。
image

align-items

五個值:flex-start | flex-end | center | baseline | stretch(默認值);
align-items:flex-start;頂部對齊
image
align-items:flex-end;底部對齊
image
align-items:center;垂直居中
image
align-items:stretch;若子元素未設置高或為auto,將占滿整個容器的高度。
image
align-items:baseline;子元素第一行文字基線對齊。
image

align-content

共6個值:flex-start | flex-end | center | space-between | space-around | stretch(默認值);
align-content:flex-start;從頂部對齊。
image
align-content:flex-end;從底部對齊。
image
align-content:center;從中部對齊。
image
align-content:space-between;頂部底部都占用。
image
align-content:space-around;均勻分布。
image
align-content:stretch;子元素無高度時自動鋪滿。
image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM