webBasic

CSS中flex布局

2019-08-13  本文已影响0人  九瀺

学习阮一峰老师的博客后自我总结。阮一峰老师博客链接http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool

网页布局(layout)是 CSS 的一个重点应用。


flex是flexible box 的缩写,意为“弹性布局”,用来为盒子模型提供更多的灵活性

webkit内核:-webkit-flex

注意:设为flex布局时,子元素的float、clear、vertical-align属性将失效。


基本概念

采用 Flex 布局的元素,称为 Flex 容器(flex container),简称"容器"。它的所有子元素自动成为容器成员,称为 Flex 项目(flex item),简称"项目"。

flex-container.png
cross axis :垂直方向的交叉轴(默认从上往下)
main axis :水平方向的主轴(默认从左往右)
<style>
  .container {
    border: 1px solid #000;
    width: 300px;
    height: 200px;
    display: flex;   
  }
  .item {
    padding: 2px;
  }
  .item1 {
    width: 50px;
    height: 50px;
    background-color: blue;
  }
  .item2 {
    width: 50px;
    height: 50px;
    background-color: red;
  }
  .item3 {
    width: 50px;
    height: 50px;
    background-color: yellow;
  }
</style>
<body>
  <div class="container">
    <div class="item item1">item1</div>
    <div class="item item2">item2</div>
    <div class="item item3">item3</div>
  </div>
</body>

基本属性

flex-direction :row | row-reverse | column | column-reverse

flex-direction: row;
Image.png
    flex-direction: row-reverse;

Image [1].png
    flex-direction: column;

Image [2].png
    flex-direction: column-reverse;

Image [3].png

flex-wrap :nowrap | wrap |wrap-reverse

    flex-wrap: nowrap;

Image [4].png
    flex-wrap: wrap;

Image [5].png
    flex-wrap: wrap-reverse;

Image [6].png

flex-flow : 属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap

    -webkit-flex-flow: column wrap;
    -ms-flex-flow: column wrap;
    flex-flow: column wrap;

Image [7].png

justify-content:项目在主轴上的对齐方式

justify-content : flex-start | flex-end | center | space-between | space-around ;

      justify-content: flex-start;

Image [8].png
      justify-content: flex-end;

Image [9].png
      justify-content: center;

Image [10].png
      justify-content: space-between;

Image [11].png
      justify-content: space-around;

Image [12].png

align-items:定义项目在交叉轴上上的对齐方式
align-items:flex-start | flex-end | center | baseline | strech(默认值);

      -ms-align-items: flex-start;
    align-items: flex-start;

Image [13].png
      -ms-align-items: flex-end;
    align-items: flex-end;

Image [14].png
      -ms-align-items: center;
    align-items: center;

Image [15].png
      ju-ms-align-items: baseline;
    align-items: baseline;

baseline:根据项目的第一行文字的基线对齐


Image [16].png
       -ms-align-items: stretch;
    align-items: stretch;

<u>盒子拥有自身高度</u>


Image [17].png

<u>盒子高度没有设定或者为auto</u>


Image [18].png

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

align-content 定义了多根轴线的对齐方式
flex-start 与交叉轴的起点对齐
flex-end 与交叉轴的终点对齐
center 与交叉轴的中点对齐
space-between 与交叉轴两端对齐,轴线之间的间隔平均分布
space-around 每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍
stretch(默认值) 轴线占满整个交叉轴

项目属性

项目属性 属性作用
order 定义项目的排列顺序。数值越小,排列越靠前,默认为0。
flex-grow 定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。
flex-shrink 定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。
flex-basis 定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。
flex 属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。
align-self 允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

order

.item3 {
    background-color: yellow;
    order: 1;
  }

Image [19].png
flex-grow
  .item1 {
    background-color: blue;
    flex-grow: 1;

  }

<u>如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。</u>

Image [20].png
flex-shrink
.item3 {
    background-color: yellow;
    flex-shrink: 0;
  }
Image [21].png
flex-basis
.item {
    width: 50px;
    height: 50px;  
  }
.item3 {
    background-color: yellow;
    flex-basis: 150px;
  }
Image [22].png

<u>如果项目自身定义的宽度与其他盒子的总宽度大过父盒子宽度将会被压缩至合适宽度来填满空间,并不会强制使用自身宽度</u>

.container width:500px;
.item width:100px;
.item3 width:150px;


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

<u>该属性有两个快捷值:auto (1 1 auto)none (0 0 auto)。建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。</u>

align-self

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

    .item3 {
    background-color: yellow;
    align-self: flex-end;
  }
Image [24].png
上一篇下一篇

猜你喜欢

热点阅读