前端基础类学习

移动端系列(flex布局)

2017-11-26  本文已影响270人  饥人谷_米弥轮
弹性盒子 display: flex,采用Flex布局的元素,称为Flex容器(flex container),简称”容器”。它的所有子元素自动成为容器成员,称为Flex项目(flex item),简称”项目”。

PS:flex布局首先要理清楚主轴和侧轴,因为flex的属性很多是根据主侧轴进行布局

flex-direction 设置主轴方向
//flex.scss
 .fix_box {
  list-style: none;
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: column; //设置主轴方向
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
}
image.png
justify-content 主轴对齐
//flex.scss
.fix_box {
  margin: 0;
  padding: 0;
  list-style: none;
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: row;
  justify-content: space-between;  //富裕空间在元素之间平均分配
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
}
image.png
align-items 侧轴对齐
.fix_box {
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: row;
  align-items: flex-end;  //flex-end 以侧轴结束一侧
  // justify-content: space-between;
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
}
image.png
flex-wrap 换行
.fix_box {
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
}
image.png
align-content 堆栈伸缩行
.fix_box {
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-content: space-around; //富裕空间在元素两侧平均分配
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
}
image.png
order 容器排序
.fix_box {
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: row;
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
  .item:nth-of-type(1){
    order: 1
  }
  .item:nth-of-type(2){
    order: 12
  }
  .item:nth-of-type(3){
    order: 3
  }
  .item:nth-of-type(4){
    order: 4
  }
  .item:nth-of-type(5){
    order: 5
  }
  .item:nth-of-type(6){
    order: 6
  }
  .item:nth-of-type(7){
    order: 7
  }
  .item:nth-of-type(8){
    order: 8
  }
  .item:nth-of-type(9){
    order: 9
  }
  .item:nth-of-type(10){
    order: -1
  }
}
image.png
flex 伸缩性

我这里将每个子容器一共分成55份,flex后面的数字表示55分之几

.fix_box {
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: row;
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
  .item:nth-of-type(1){
    flex:1
  }
  .item:nth-of-type(2){
    flex:2
  }
  .item:nth-of-type(3){
    flex:3
  }
  .item:nth-of-type(4){
    flex:4
  }
  .item:nth-of-type(5){
    flex:5
  }
  .item:nth-of-type(6){
    flex:6
  }
  .item:nth-of-type(7){
    flex:7
  }
  .item:nth-of-type(8){
    flex:8
  }
  .item:nth-of-type(9){
    flex:9
  }
  .item:nth-of-type(10){
    flex:10
  }
}
image.png
align-self 子元素侧轴对齐
.fix_box {
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  flex-direction: row;
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
  }
  .item:nth-of-type(1){
    flex:1;
  }
  .item:nth-of-type(2){
    flex:2
  }
  .item:nth-of-type(3){
    flex:3
  }
  .item:nth-of-type(4){
    flex:4
  }
  .item:nth-of-type(5){
    flex:5;
    align-self:center;  //元素居中 富裕空间 平分左右两侧
  }
  .item:nth-of-type(6){
    flex:6
  }
  .item:nth-of-type(7){
    flex:7;
    align-self:center;  //元素居中 富裕空间 平分左右两侧
  }
  .item:nth-of-type(8){
    flex:8
  }
  .item:nth-of-type(9){
    flex:9
  }
  .item:nth-of-type(10){
    flex:10
  }
}
image.png
margin:auto 居中显示
.fix_box {
  width: 800px;
  height: 500px;
  border: 5px solid pink;
  display: flex;
  .item {
    width: 80px;
    height: 80px;
    background-color: yellow;
    border: 1px solid black;
    text-align: center;
    line-height: 80px;
    margin: auto; //居中显示
  }
}
image.png
上一篇 下一篇

猜你喜欢

热点阅读