【重学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>
相关推荐
有诺千金35 分钟前
VUE3入门很简单(4)---组件通信(props)
前端·javascript·vue.js
2501_9447114336 分钟前
Vue-路由懒加载与组件懒加载
前端·javascript·vue.js
雨季6661 小时前
Flutter 三端应用实战:OpenHarmony “心流之泉”——在碎片洪流中,为你筑一眼专注的清泉
开发语言·前端·flutter·交互
换日线°1 小时前
前端3D炫酷展开效果
前端·3d
广州华水科技1 小时前
大坝变形监测的单北斗GNSS技术应用与发展分析
前端
Dontla1 小时前
浏览器localStorage共享机制介绍(持久化客户端存储方案)本地存储冲突、iframe、XSS漏洞、命名空间隔离
前端·网络·xss
霍理迪2 小时前
JS其他常用内置对象
开发语言·前端·javascript
tao3556672 小时前
HTML-03-HTML 语义化标签
前端·html
小马_xiaoen2 小时前
IndexedDB 从入门到实战:前端本地大容量存储解决方案。
前端
jiayong232 小时前
Vue2 与 Vue3 常见面试题精选 - 综合宝典
前端·vue.js·面试