• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

css flex布局 —— 项目属性 flex-basis

武飞扬头像
*且听风吟
帮助1

flex-basis 属性定义了在分配多余空间之前,项目占据的主轴空间(main size)

浏览器根据这个属性,计算主轴是否有多余空间。

语法

.item {
  flex-basis: <length> | auto; /* default auto */
}

它可以设为跟 widthheight 属性一样的值(比如350px),则项目将占据固定空间。

实例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 300px;
      height: 100px;
      background-color: #e6e6e6;
    }
    .item {
      flex-basis: 50px;
      height: 50px;
    }
    .container div:nth-of-type(1) {background-color:coral;}
    .container div:nth-of-type(2) {background-color:lightblue;}
    .container div:nth-of-type(3) {background-color:khaki;}
    .container div:nth-of-type(4) {background-color:pink;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
  </div>
</body>
</html>
学新通

页面效果:
学新通

如果元素上同时设置了 widthflex-basis,那么 width 的值就会被 flex-basis 覆盖掉,实例:

    .item {
      width: 30px;
      flex-basis: 50px;
      height: 50px;
    }

页面效果:
学新通

可以看到,设置了 width: 30px;flex-basis: 50px;flex-basis 属性的优先级比 width 高,所以元素宽度还是 50px

把宽度调整大一些,实例:

    .item {
      flex-basis: 100px;
      height: 50px;
    }

页面效果:
学新通

可以看到,项目元素被压缩显示了,缩小后项目的宽度是75px,4个刚好为300px,等于容器的宽度。

所以,设置了flex-basis,如果项目的总宽度超过容器的宽度,那么会缩小到容器范围内。

flex-basis 也可以指定其占据父元素的百分比,如下:

  <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 300px;
      height: 100px;
      background-color: #e6e6e6;
    }
    .item {
      height: 50px;
    }
    .container div:nth-of-type(1) { flex-basis: 10%;background-color:coral;}
    .container div:nth-of-type(2) { flex-basis: 30%;background-color:lightblue;}
    .container div:nth-of-type(3) { flex-basis: 40%;background-color:khaki;}
    .container div:nth-of-type(4) { flex-basis: 10%;background-color:pink;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
  </div>
</body>
</html>
学新通

页面效果:
学新通

flex-basis ,默认值为 auto(根据内容自动调整大小),如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .container {
      display: flex;
      margin: 0px auto;
      width: 300px;
      height: 100px;
      background-color: #e6e6e6;
    }
    .item {
      height: 50px;
    }
    .container div:nth-of-type(1) { flex-basis: 10%;background-color:coral;}
    .container div:nth-of-type(2) { flex-basis: 30%;background-color:lightblue;}
    .container div:nth-of-type(3) { flex-basis: 40%;background-color:khaki;}
    .container div:nth-of-type(4) { flex-basis: auto;background-color:pink;}
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4 auto</div>
  </div>
</body>
</html>
学新通

页面效果:
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgehgij
系列文章
更多 icon
同类精品
更多 icon
继续加载