flexbox学习笔记

参考阮一峰博客~http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

flexbox弹性盒子


display:flex,规定这是个容器,可以设置容器内部一些属性。
也就是说,我们要设置的时候设置的是容器而不是我们要作用的元素
简单列一下相关属性:
下面的box都是指的容器,而不是项目!!!

容器的属性

flex-direction属性

flex-direction属性决定主轴的方向(即项目的排列方向)。

.box {
  flex-direction: column-reverse | column | row | row-reverse ;
}
flex-direction.png
flex-wrap属性

默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行

.box{
flex-wrap: nowrap(不换行) | wrap(另起一行 ,第一行在上面)| wrap-reverse(另起一行,第一行在下面);
}
justify-content属性

justify-content属性定义了项目在主轴上(默认主轴是行(水平方向),可以通过flex-direction:column来设置成列)的对齐方式。

.box {
 justify-content: flex-start | flex-end | center | space-between | space-around;
}

跟主轴的方向有关,假设主轴方向是水平从左向右

  • flex-start(默认值):左对齐
  • flex-end:右对齐
  • center :居中
  • space-between:两端对齐,项目间间隔相等
  • space-space:每个项目两侧间隔相等


    justify-content.png
align-items属性

align-items属性定义项目在交叉轴(水平垂直方向除了主轴的另外一条)上如何对齐。

.box {
  align-items: flex-start | flex-end | center | baseline | stretch;
}

假设交叉轴是垂直从上到下

  • flex-start:交叉轴的起点对齐(靠上)
  • flex-end :交叉轴的终点对齐(靠下)
  • center:交叉轴的中点对齐,也就是垂直居中
  • baseline:项目第一行文字基线对齐
  • strech(默认值):如果当前项目
    未设置高度或者高度为auto,项目将占满整个容器的高度


    align-items.png
align-content

多根轴线的对齐方式
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

.box {
   align-content: flex-start | flex-end | center | space-between | space-  around | stretch;
}

在下面这样的情景需要用到

 <div class="box">
    <div class="row">
    <span class="item"></span>
    <span class="item"></span>
</div>
    <div class="row">
    <span class="item"></span>
    <span class="item"></span>
</div>
</div>//html代码,有两个弹性盒子,也就是说有两根轴线
 
  .box {
    display: flex;
    flex-wrap: wrap;
    align-content: space-between;
  }

.row{
  flex-basis: 100%;
  display: flex;
  justify-content: space-between;
}//CSS

项目的属性

align-self属性

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示[继承父元素的align-items]属性,如果没有父元素,则等同于stretch。

.item {
  align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

一般喜欢用

.item:nth-child(2) {
align-self: center;
}
order属性

order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。

.item {
   order: <integer>;
}

实例如下:

    .item1{
        height: 10px;
        width: 10px;
        border-radius: 50%;
        background-color: black;
        order:2;
    }
    .item{
        height: 10px;
        width: 10px;
        border-radius: 50%;
        background-color: blue;
        order:1;
    }//CSS
<div class="box">
    <span class="item1"></span>
    <span class="item"></span>//html
</div> //结果:蓝点在红点前面
flex-grow属性

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

.item {
  flex-grow: <number>; /* default 0 */
}
flex-shrink属性

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

.item {
  flex-shrink: <number>; /* default 1 */
}

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。

负值对该属性无效。

flex-basis属性

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。

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

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

flex属性

flex属性是flex-grow(放大比例,默认为0,就算存在剩余空间,也不缩小), flex-shrink(缩小比例,默认为1,如果空间不够,将等比例减小) 和 flex-basis(定义主轴空间,浏览器判断计算主轴是否有多余空间)的简写,默认值为0 1 auto。后两个属性可选。

  .item {
  flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
  }

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。

建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值
问题:实现类似于下面的布局。

布局.png
    .box{
        height:100px;
        width:100px;
        background-color:red;
        flex-wrap:wrap;
        display:flex;           
        flex-direction:column;
    }
    .row{
        display: flex;
        justify-content:space-between;
        }
    .item{
        margin:1px;
        height: 30px;
        width: 30px;
        border-radius: 50%;
        background-color: blue;
    }//CSS
    <div class="box">
      <div class="row">
        <span class="item"></span>
        <span class="item"></span>          
      </div>
      <div class="row">
        <span class="item"></span>
        <span class="item"></span>
       </div>
      <div class="row">
        <span class="item"></span>
        <span class="item"></span>
      </div>
  </div>//html

布局

网格布局


最简单的网格布局,就是平均分布。在容器里面平均分配空间。注意flex-flow要设置成1,这样才会扩出来。

.Grid{
  display:flex;
}
.Grid-cell{
  flex:1;
  margin-left:20px;
  background-color: black;//为了明显的看出来
}// CSS
  <div class="Grid">
    <div class="Grid-cell">111</div>
    <div class="Grid-cell">111</div>
    <div class="Grid-cell">111</div>
  </div>//HTML

百分比布局


一个网格为固定的,其他网格的大小自动分配

.Grid-cell {
  flex: 1;
  background-color: black;
  margin-left:20px;
}//自动分配剩余空间
.Grid-cell.u-1of3 {
  flex: 0 0 33.3333%;
}

.Grid-cell.u-1of4 {
  flex: 0 0 25%;
}
<div class="Grid">
  <div class="Grid-cell u-1of4">...</div>
  <div class="Grid-cell">...</div>
  <div class="Grid-cell u-1of3">...</div> 
</div>

圣杯布局

指的是一种最常见的网站布局。页面从上到下,分成三个部分:头部(header),躯干(body),尾部(footer)。其中躯干又水平分成三栏,从左到右为:导航、主栏、副栏。


    .HolyGrail {
      display: flex;
      min-height: 100vh;
      flex-direction: column;
    }

  header,
  footer {
  flex: 1;
  background-color: grey;
  }

.HolyGrail-body {
 display: flex;
 flex: 1;
 height: 350px;
}

 .HolyGrail-content {
    flex: 1;
    background-color: red;
}

.HolyGrail-nav, .HolyGrail-ads {
  /* 两个边栏的宽度设为12em */
  flex: 0 0 12em;
  background-color: black;
}

  .HolyGrail-nav {
  /* 导航放到最左边 */
   order: -1;
  background-color: black;
}

在手机端content部分会变成垂直叠加

 @media (max-width: 768px) {
  .HolyGrail-body {
  flex-direction: column;
  flex: 1;
  }
.HolyGrail-nav,
.HolyGrail-ads,
.HolyGrail-content {
 flex: auto;
}
}

输入框布局


我们常常需要在输入框的前方添加提示,后方添加按钮

    .input{
        display:flex;
        justify-content:center;
    }//这里顺便让他居中一波
    .InputAddOn {
        display: flex;
        width:200px;    
        }

    .InputAddOn-field {
    flex: 1;
    }//CSS
    <div class="input">
      <div class="InputAddOn">
        <span class="InputAddOn-item">...</span>
        <input class="InputAddOn-field">
        <button class="InputAddOn-item">...</button>
    </div>
  </div>

悬挂式布局


有时,主栏的左侧或右侧,需要添加一个图片栏。

    .Media {
      display: flex;
      align-items: flex-start;
    }

   .Media-figure {
    margin-right: 1em;
  }

  .Media-body {
  flex: 1;
  }

固定的底栏


有时,页面内容太少,无法占满一屏的高度,底栏就会抬高到页面的中间。这时可以采用Flex布局,让底栏总是出现在页面的底部。

.Site {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
  }

.Site-content {
  flex: 1;
}//CSS
  <body class="Site">
  <header>...</header>
     <main class="Site-content">...</main>
     <footer>...</footer>
  </body>

流式布局

每行的项目数一定,会自动分行

.parent {
      width: 200px;
      height: 150px;
      background-color: black;
      display: flex;
      flex-flow: row wrap;
      align-content: flex-start;
      }

 .child {
    box-sizing: border-box;
    background-color: white;
    flex: 0 0 25%;
    height: 50px;
    border: 1px solid red;
    }
可以说,用flexbox方便了很多,我们要让图片居中,用align-items和justify-content就可以(只不过Ie9以下的浏览器不兼容)

父容器是box,属性在box设置就行了

.box{
            height: 500px;
            background-color: black;
            display: flex;
            align-items:center;
            justify-content:center;
        }
<div class="box">
  <img src = "good8.jpg"/>
</div>
居中.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解dis...
    Mx勇阅读 4,627评论 0 26
  • 移动开发基本知识点 一.使用rem作为单位 html { font-size: 100px; } @media(m...
    横冲直撞666阅读 3,518评论 0 6
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,806评论 1 92
  • 传统的网页布局基于盒装模型,使用display,position,float属性来达成各种布局。对于一些特殊的布局...
    exialym阅读 2,649评论 0 11
  • 前言 FlexBox是css3的一种新的布局方式,天生为解决布局问题而存在的它,比起传统的布局方式,我们使用Fle...
    zevei阅读 1,438评论 23 3