CSS Flex 弹性布局

Flex 布局 = 弹性布局 = 伸缩布局

采用Flex布局的元素,称为Flex容器,它的所有子元素称为容器成员,称为Flex项目

flex布局原理:通过给父盒子添加flex属性,来控制子盒子的位置和排列方式

常见父盒子属性:

1.flex-direction:设置主轴方向

2.justify-content:设置主轴上字元素的排列方式

3.flex-wrap:设置子元素是否换行

4.align-items:设置侧轴上的字元素排列方式(单行)

5.align-content:设置侧轴上的子元素的排列方式(多行)

6.flex-flow:复合属性,等于同时设置了flex-direction和flex-wrap

常见子盒子属性:

1.flex :1 表示子项目所占份数

2.align-self:控制子项自己在侧轴的排列方式

具体如下:

1.flex-direction:设置主轴方向

flex布局中 ,分为主轴和侧轴,主轴默认为x轴,水平向右,侧轴为y轴,水平向下,

flex-direction:row  从左到右(默认值)  

flex-direction: row-reverse 从右到左

flex-direction: column  从上到下

flex-direction:  column-reverse 从下到上

flex-direction:row  从左到右(默认值)  

 .father {
            display: flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
            /* 默认的主轴为x轴 */
            flex-direction: row;
        }
        
.son {
            width: 150px;
            height: 100px;
            background-color: rgb(153, 230, 122);
            /* margin-left: 10px; */
 }

 <div class="father">
        <span class="son">1</span>
        <span class="son">2</span>
        <span class="son">3</span>
        <span class="son">4</span>
        <span class="son">5</span>
        <span class="son">6</span>
 </div>

   flex-direction: row-reverse 从右到左

 flex-direction: row-reverse;

    flex-direction: column  从上到下

flex-direction: column;

flex-direction:  column-reverse 从下到上

 flex-direction: column-reverse;

2.justify-content:设置主轴上子元素的排列方式

注意:使用前确认好主轴是哪个

justify-content: flex-start   如果主轴是x轴,则从左到右 (默认值)

justify-content: flex-end  从尾部开始排列

justify-content: center  在主轴居中对齐

justify-content: space-around  平方剩余空间

justify-content: space-between 先两边贴边,再平分剩余空间 

justify-content: flex-start   如果主轴是x轴,则从左到右 (默认值)

.father {
            display: flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
            /* 默认的主轴为x轴 */
            flex-direction: row;
            justify-content: flex-start;
        }
.son   {
            width: 150px;
            height: 100px;
            background-color: rgb(153, 230, 122);
            /* margin-left: 10px; */
        }
<div class="father">
        <span class="son">1</span>
        <span class="son">2</span>
        <span class="son">3</span>
</div>

justify-content: flex-end  从尾部开始排列

 flex-direction: row;
 justify-content: flex-end;

justify-content: center  在主轴居中对齐

 flex-direction: row;
 justify-content: center;

justify-content: space-around  平方剩余空间

 flex-direction: row;
 justify-content: space-between;

justify-content: space-between 先两边贴边,再平分剩余空间 

flex-direction: row;
justify-content: space-around;

3.flex-wrap:设置子元素是否换行

flex-wrap: nowrap  默认不换行

flex-wrap: wrap  换行

flex-wrap: nowrap  默认不换行,如果装不开,会缩小子元素的宽度,放在父元素里

.father {
            display: flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
            /* 默认的主轴为x轴 */
            flex-direction: row;
            justify-content: space-around;
            flex-wrap: nowrap;
        }
.son {
            width: 150px;
            height: 100px;
            background-color: rgb(153, 230, 122);
            margin: 10px;
        }
  <div class="father">
        <span class="son">1</span>
        <span class="son">2</span>
        <span class="son">3</span>
        <span class="son">4</span>
        <span class="son">5</span>
        <span class="son">6</span>
        <span class="son">7</span>
        <span class="son">8</span>
    </div>

flex-wrap: wrap  换行

 .father {
            display: flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
            /* 默认的主轴为x轴 */
            flex-direction: row;
            flex-wrap: wrap;
        }
        

4.align-items:设置侧轴上的字元素排列方式(单行)

align-items:flex-start   从上到下

align-items:flex-end 从下到上

align-items:center 垂直居中

stretch:拉伸   (默认值)

align-items:center 侧轴垂直居中

 .father {
            display: flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
            /* 默认的主轴为x轴 */
            flex-direction: row;
            /* 侧轴居中 */
            align-items: center;
        }
 .son {
            width: 150px;
            height: 100px;
            background-color: rgb(153, 230, 122);
            margin: 10px;
        }
  <div class="father">
        <span class="son">1</span>
        <span class="son">2</span>
        <span class="son">3</span>
    </div>

5.align-content:设置侧轴上的子元素的排列方式(多行)

只能用于出现换行情况(多行) 在单行情况下是没有效果的

align-content: flex-start   从侧轴的头部开始排列(默认值)

align-content: flex-end  从侧轴的尾部开始排列

align-content: center  在侧轴轴居中对齐

align-content: space-around  子项在侧轴平方剩余空间

align-content:space-between 子项在侧轴先两边贴边,再平分剩余空间 

align-content:stretch 设置子项元素高度平分父元素高度

6.flex-flow:复合属性,等于同时设置了flex-direction和flex-wrap

  /* flex-direction: row;
    flex-wrap: wrap; */
   flex-flow: column wrap;

flex-flow: column wrap;   === flex-direction: row;  +  flex-wrap: wrap;

给子项目设置:

1.flex :1 表示子项目所占份数

.father {
            display: flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
            /* flex-direction: row;
            flex-wrap: wrap; */
            /* flex-flow: column wrap; */
            /* 侧轴居中 */
            align-items: center;
        }
        
        .son1,
        .son2,
        .son3 {
            width: 150px;
            height: 100px;
            background-color: rgb(153, 230, 122);
            margin: 10px;
        }
        
        .father .son1,
        .father .son3 {
            flex: 1;
        }
        
        .father .son2 {
            flex: 3;
        }

  <div class="father">
        <span class="son1">1</span>
        <span class="son2">2</span>
        <span class="son3">3</span>
    </div>

2.align-self:控制子项自己在侧轴的排列方式

.father {
            display: flex;
            width: 800px;
            height: 300px;
            background-color: pink;
            margin: 0 auto;
        }
        
        .son1,
        .son2,
        .son3 {
            width: 150px;
            height: 100px;
            background-color: rgb(153, 230, 122);
            margin: 10px;
        }
        
        .father .son1,
        .father .son3 {
            flex: 1;
        }
        
        .father .son2 {
            flex: 3;
            align-self: flex-end;
        }
  <div class="father">
        <span class="son1">1</span>
        <span class="son2">2</span>
        <span class="son3">3</span>
    </div>

3.order:定义项目的排列顺序,数值越小,排列越靠前,默认值为0

4,flex-order:定义项目的放大比例,默认为0

5.flex-shrink:定义了项目的缩小比例,默认为1 ,当空间不足是,项目将缩小,如有一个项目的flex-shrink设置为0,其他项目为1,当空间不足时,前者不缩小。

6.flex-basis:定义了项目占主轴的空间,浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。它可以设为跟widthheight属性一样的值(比如100px),则项目将占据固定空间

7.flex属性是flex-growflex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

.son{
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
}

3-7 参考 https://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值