【重学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>
相关推荐
用户307675281127几秒前
💡 从"傻等"到"流淌":我在AI项目中实现流式输出的血泪史(附真实代码+深度解析)
前端
bluceli2 分钟前
前端性能优化实战指南:让你的网页飞起来
前端·性能优化
SuperEugene3 分钟前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
没想好d4 分钟前
通用管理后台组件库-9-高级表格组件
前端
阿虎儿8 分钟前
React Hook 入门指南
前端·react.js
核以解忧31 分钟前
借助VTable Skill实现10W+数据渲染
前端
WangHappy33 分钟前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序
李剑一38 分钟前
要闹哪样?又出现了一款新的格式化插件,尤雨溪力荐,速度提升了惊人的45倍!
前端·vue.js
闲云一鹤1 小时前
Git LFS 扫盲教程 - 你不会还在用 Git 管理大文件吧?
前端·git·前端工程化
阿虎儿1 小时前
React Context 详解:从入门到性能优化
前端·vue.js·react.js