對于此博文的整理
html
<div class="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
</div>
父元素屬性。
1 容器的flex-direction屬性
此屬性決定子元素布局的方向。
.parent{
flex-direction: row | row-reverse | column | column-reverse;
}
分別代表:從左至右,從右至左,從上到下,從下到上。
2 容器的flex-wrap屬性。(和換行有關)
.parent{
flex-wrap: nowrap(默認) | wrap | wrap-reverse
//不換行 , 換行 ,從下至上換行。
}
no-wrap(默認不換行)
.parent{
width:450px;
height:450px;
border:1px solid #333;
display:flex;
flex-wrap:nowrap;
margin:50px auto;
}
.child{
width:200px;
height:200px;
background:red;
margin:10px;
font-size:30px;
line-height:200px;
text-align:center;
}

第一張
可以看到四個div橫向排列,且不換行,但是四個子div因為父div寬度有限的原因,他們的寬度被擠壓成了92.5px而不是設定的200px。
wrap(換行)
.parent{
flex-wrap:wrap;
}

;
wrap-reverse
.parent{
flex-wrap:wrap-reverse;
}

justify-content屬性。(主軸的對齊方式。)
.parent{
justify-content:flex-start | flex-end | center | space-between | space-around;
}
flex-start(默認)
//修改相應css樣式。
.parent{
width:550px;
justify-content:flex-start;
}

;
flex-end
.parent{
justify-content:flex-end;
}

center;

space-between;兩端對齊,項目之間間隔。相同。

space-around; 間距相同。

align-items(交叉/Y軸上的對齊方式。)
.parent{
align-items:flex-start | flex-end | center | baseline | stretch(默認);
}
//和上面的屬性幾乎一致。
stretch:如果沒有設置高度。高度和容器高度一致。
align-content(定義了多跟軸線的對齊方式。)
.parent{
align-content:flex-start | flex-end | center | space-between | space-around | stretch;
}
- flex-start:與交叉軸的起點對齊。
- flex-end:與交叉軸的終點對齊。
- center:與交叉軸的中點對齊。
- space-between:與交叉軸兩端對齊,軸線之間的間隔平均分布。
- space-around:每根軸線兩側的間隔都相等。所以,軸線之間的間隔比軸線與邊框的間隔大一倍。
- stretch(默認值):軸線占滿整個交叉軸
flex-end。
.parent{
width:550px;
height:550px;
border:1px solid #333;
display:flex;
flex-wrap:wrap;
margin:50px auto;
justify-content:space-around;
align-content:flex-end;
}
.child{
width:200px;
height:200px;
background:red;
margin:10px;
font-size:30px;
line-height:200px;
text-align:center;
}

flex-between

子元素的屬性。
order(排列順序。)
.child{
order:<integer>
}
flex-grow
放大比例,默認為0,即如果存在剩余空間也不放大。
flex-shrink
縮小比例,默認值為1,即如果空間不足,該項目將縮小。
flex-basis
屬性定義了在分配多余空間之前,項目占據的主軸空間(main size)。瀏覽器根據這個屬性,計算主軸是否有多余空間。它的默認值為auto,即項目的本來大
它可以設為跟width或height屬性一樣的值(比如350px),則項目將占據固定空間
align-self
align-self屬性允許單個項目有與其他項目不一樣的對齊方式,可覆蓋align-items屬性。默認值為auto,表示繼承父元素的align-items屬性,如果沒有父元素,則等同于stretch。
.parent{
width:550px;
height:550px;
border:1px solid #333;
display:flex;
margin:50px auto;
}
.child:nth-child(2){
align-self:flex-end;
}
