【重学css】flex弹性布局(子容器特性)

flex子项的特性

flex-grow扩展比例

对剩余空间等比分配;

1.默认值是0,表示不占用剩余空白间隙扩展自己的宽度;

2.flex-grow: 1;flew-grow大于1时和1的效果一样;

css 复制代码
    .main {
      width: 500px;
      height: 300px;
      background-color: skyblue;
      display: flex;
    }
    .main div {
      width: 100px;
      height: 100px;
      background-color: pink;
      flex-grow: 1;
    }

3.flex-grow: 0.5;

flex-shrink收缩比例

默认值是1,表示flex容器空间不足时,元素的收缩比例;

1.默认是1,自动收缩,和父容器大小相同;

2.flex-shrink: 0;

3.flex-shrink: 0.5;

4.假设有多个子元素,这个计算方式很特别;

css 复制代码
  <style>
    .main {
      width: 500px;
      height: 200px;
      background-color: skyblue;
      display: flex;
    }
    .main div:nth-child(1) {
      width: 300px;
      height: 100px;
      background-color: sandybrown;
      flex-shrink: 2;
    }

    .main div:nth-child(2) {
      width: 400px;
      height: 100px;
      background-color: pink;
      flex-shrink: 1;
    }
    /* 300 * 2 + 400 * 1 = 1000*/
    /* 300 - 600/1000 * 200 */
    /* 400 - 400/1000 * 200 */
  </style>
  <body>
    <div class="main">
      <div>1</div>
      <div>2</div>
    </div>
  </body>

flex-basis及flex缩写

默认值是auto,指定了flex元素在主轴方向上的初始大小;flex-basis可以是auto、固定像素、百分比;

1.子元素大小没变;

css 复制代码
    .main div {
      height: 100px;
      width: 100px;
      background-color: pink;
      flex-basis: 100px;
    }

2.子元素变大了;

css 复制代码
    .main div {
      height: 100px;
      width: 100px;
      background-color: pink;
      flex-basis: 200px;
    }

flex

flex是flex-grow、flex-shrink、flex-basis的缩写;

css 复制代码
flex: 1
相当于:
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;

flex: 0;
相当于:
flex-grow: 0;
flex-shrink: 1;
flex-basis: 0;

order(不常用)

默认值是0;改变某一个flex子项的排序位置;

1.order 1;第一个到最后面去了;

css 复制代码
   .main div:nth-child(1) {
      order: 1;
    }
   .main div:nth-child(4) {
      order: -1;
    }

align-self(不常用)

默认值是auto,控制单独某个flex子项的垂直对齐方式;当子元素的align-self: auto; 它会和父元素的align-items保持一致;

1.align-self: flex-start;

常见布局

等高布局

1.flex默认就是等高布局;

2.传统方式: 真是奇技淫巧,我是从来没用过,好神奇!

css 复制代码
  <style>
    .main {
      width: 300px;
      /* display: flex; */
      border: 1px black solid;
      overflow: hidden;
    }
    .main div {
      width: 100px;
      background-color: pink;
      float: left;
      margin-bottom: -2000px;
      padding-bottom: 2000px;
    }
    .main div:nth-child(2) {
      background-color: salmon;
      float: right;
    }
  </style>
  <body>
    <div class="main">
      <div>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
        <p>测试文字</p>
      </div>
      <div><p>测试文字</p></div>
    </div>
  </body>

两列与三列布局

1.flex

css 复制代码
// 两行
<style>
.main {
  height: 100vh;
  width: 100%;
  background-color: bisque;
  display: flex;
}
.left {
  background-color: pink;
  width: 300px;
}
.right {
  flex-grow: 1;
  background-color: sandybrown;
}
</style>
// 三列
<style>
.main {
  height: 100vh;
  width: 100%;
  background-color: bisque;
  display: flex;
}
.left {
  background-color: pink;
  width: 300px;
}
.middle {
  flex-grow: 1;
  background-color: bisque;
}
.right {
  width: 300px;
  background-color: sandybrown;
}
</style>

2.传统方式float或BFC

css 复制代码
.main {
  width: 100%;
  height: 100vh;
  background-color: salmon;
}
.main div:nth-child(1) {
  height: 100vh;
  width: 200px;
  background-color: antiquewhite;
  float: left;
}
.main div:nth-child(2) {
  /*还可以用BFC*/
  //overflow: hidden;
  margin-left: 200px;
  height: 100vh;
  background-color: aqua;
}

Sticky Footer布局

css 复制代码
.main {
  /*关键样式,content内容溢出也不会挡住footer和header*/
  min-height: 100vh;
  background-color: antiquewhite;
  display: flex;
  flex-direction: column;
}
.header {
  height: 50px;
  background-color: aquamarine;
}
.content {
  flex-grow: 1;
  background-color: salmon;
}
.footer {
  height: 50px;
  background-color: pink;
}

溢出项布局

1.flex; 只需子项设置flex-shrink: 0;

2.传统方式:

css 复制代码
  <style>
    .main {
      height: 100px;
      width: 500px;
      background-color: blanchedalmond;
    }
    /*宽度需要自己计算*/
    .main section {
      width: 1320px;
    }
    .main div {
      float: left;
      width: 100px;
      height: 50px;
      margin-right: 10px;
      background-color: pink;
    }
  </style>
相关推荐
云草桑8 分钟前
逆向工程 反编译 C# net core
前端·c#·反编译·逆向工程
布丁椰奶冻14 分钟前
解决使用nvm管理node版本时提示npm下载失败的问题
前端·npm·node.js
Leyla40 分钟前
【代码重构】好的重构与坏的重构
前端
影子落人间43 分钟前
已解决npm ERR! request to https://registry.npm.taobao.org/@vant%2farea-data failed
前端·npm·node.js
世俗ˊ1 小时前
CSS入门笔记
前端·css·笔记
子非鱼9211 小时前
【前端】ES6:Set与Map
前端·javascript·es6
6230_1 小时前
git使用“保姆级”教程1——简介及配置项设置
前端·git·学习·html·web3·学习方法·改行学it
想退休的搬砖人1 小时前
vue选项式写法项目案例(购物车)
前端·javascript·vue.js
加勒比海涛2 小时前
HTML 揭秘:HTML 编码快速入门
前端·html
啥子花道2 小时前
Vue3.4 中 v-model 双向数据绑定新玩法详解
前端·javascript·vue.js