【重学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>
相关推荐
java1234_小锋4 小时前
【技术专题】HTML5 网页技术 - HTML5 Canvas 基础以及动画入门
前端·html·html5
知了学历孙老师7 小时前
小自考的校考和统考,出题方和难度差在哪
java·大数据·前端
ShineWinsu9 小时前
对于JavaScript:操作元素的解析
开发语言·前端·javascript·面试·web·dom·元素
福兮说9 小时前
JSON 格式正确,数据就可靠吗?我用一份自编配置检查类型与空值
java·前端·json
程序员老赵11 小时前
Docker 部署青龙面板:轻松搭建定时任务管理平台
前端·docker·开源
Swot11 小时前
Asciidoctor 公式数字下标错位问题:原因分析与解决方案
前端
Swot11 小时前
leo-editor 中将 Markdown 内容转成 asciidoc 格式
前端
狒狒AI11 小时前
Remotion / GSAP+Three / HyperFrames / Skia 四栈实测:代码渲染知识视频的选栈方法(附开源技能)
前端·人工智能·aigc
修炼的dance11 小时前
浏览器里导出H.265,8.5秒和55.5秒差在硬件
前端
补码缩写11 小时前
WebCodecs选编码器,探测之后还要验解码和播放
前端