flex 布局筆記
flex 布局詳解
flex 使用
div{
display: flex;
}
- 此div便是flex容器.其內(nèi)的所有子元素都是flex成員.整個為flex項目;
flex 容器屬性
- flex-flow
- flex-direction
- flex-wrap
- justify-content
- align-items
- align-content
- flex-flow屬性: 排列方向+ 換行
.box {
flex-flow: <flex-direction> || <flex-wrap>;
}
- flex-direction屬性:排列方向;
.box {
flex-direction: row | row-reverse | column | column-reverse;
}
左至右(默認)/右至左/上至下/下至上
- flex-wrap屬性:換行;
.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}
不換行(默認)/新行從下左/新行上左
- justify-content屬性:水平對齊;
.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
}
左對齊(默認)/右對齊/居中/左右貼/左右平均間隔
- align-items屬性:豎直對齊;
.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}
垂直居上/垂直居下/垂直居中/第一行文字上對齊/拉伸(默認)
- align-content屬性: 多行布局;
.box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
左上角/左下角/居中/上下貼/上下平均間隔/拉伸(默認)
flex 自身屬性
- order
- flex
- flex-grow
- flex-shrink
- flex-basis
- align-self
- flex屬性: 集成 放大/縮小/自定義大小屬性
.item {
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}
屬性快捷值: auto(1,1,auto) 和 none(0,0,auto) 和 默認(0,1,auto)
- order屬性: 排序
.item {
order: <integer>;
}
數(shù)小靠前,默認0
- flex-grow屬性: 放大比例;
.item {`
flex-grow: <number>; /* default 0 */
}
按比例平分剩余空間; 0的不擴展;
- flex-shrink屬性: 縮小比例;
.item {
flex-shrink: <number>; /* default 1 */
}
按比例平分已有空間; 0 的不參與平分;
- flex-basis屬性: 分配前大小;
.item {
flex-basis: <length> | auto; /* default auto */
}
自定大小;
- align-self屬性: 單獨修改豎直對齊的狀態(tài);
.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}
自動(默認)/居上/居下/居中/第一行文字對齊/拉伸
總結(jié)
使用步驟
- 定義: 使DOM元素成為彈性盒子;
div {
display:flex;
}
- 定位: 確定排列的方向和是否換行;
div{
display:flex;
flex-flow: row wrap:
}
- 布局: 確定主軸行列的布置方式;
div{
display:flex;
flex-flow:column nowrap;
justify-content: space-between;
align-items: baseline;
}
- 布局: 當多行時,應使用多行的布置方式;
div{
display:flex;
flex-flow:row-reverse wrap;
justify-content: space-around;
align-content: space-around;
}
- 布局: 當修改某一個單元值時,使用其特有的布置;
div{
display:flex;
flex-flow:column-reverse wrap-reverse;
justify-content: center;
align-content: stretch;
}
.item{
align-self: baseline;
}
- 布局: 當確定單行時,單位設(shè)定伸縮方式;
div{
display:flex;
flex-flow:row nowrap;
justify-content: center;
align-content: center;
}
.item {
align-self: flex-end;
flex: 1 1 auto;
}
- 布局: 有時候可以指定順序;
div{
display:flex;
flex-flow:row nowrap;
justify-content: center;
align-content: center;
}
.item {
align-self: flex-end;
flex: 1 1 auto;
order: 3;
}
浙公網(wǎng)安備 33010602011771號