当前位置:首页> 文章> CSS

Flex的布局属性

:2019-06-09   :舒彬琪   :46
Flex教学之Flex属性简介
容器属性
  • flex-flow


让弹性盒的元素以相反的顺序显示,且在必要的时候进行拆行:
display:flex;
flex-flow:row-reverse wrap;

  • flex-direction


设置 <div> 元素内弹性盒元素的方向为相反的顺序:
div
{
display:flex;
flex-direction:row-reverse;
}

  • flex-wrap


让弹性盒元素在必要的时候拆行:
display:flex;flex-wrap: wrap;

  • justify-content


在弹性盒对象的 <div> 元素中的各项周围留有空白:
div{    display: flex;    justify-content: space-around;}

  • align-items


居中对齐弹性盒的各项 <div> 元素:
div{    display: flex;    align-items:center;}

  • align-content


对齐弹性盒的 <div> 元素的各项:
div{    display: flex;    flex-flow: row wrap;    align-content:space-around;}

元素属性
  • order


设置弹性盒对象元素的顺序:
div#myRedDIV {order:2;}
div#myBlueDIV {order:4;}
div#myGreenDIV {order:3;}
div#myPinkDIV {order:1;}

  • flex-grow


让第二个元素的宽度为其他元素的三倍:
div:nth-of-type(1) {flex-grow: 1;}div:nth-of-type(2) {flex-grow: 3;}div:nth-of-type(3) {flex-grow: 1;}

  • flex-shrink


A, B, C 设置 flex-shrink:1, D , E 设置为 flex-shrink:2:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ftm教程(ftm.8hi.com.cn)</title>
<style>
#content {
  display: flex;
  width: 500px;
}

#content div {
  flex-basis: 120px;
  border: 3px solid rgba(0,0,0,.2);
}

.box { 
  flex-shrink: 1;
}

.box1 { 
  flex-shrink: 2; 
}
</style>
</head>
<body>

<p>div 总宽度为 500px, flex-basic 为 120px。</p>
<p>A, B, C 设置 flex-shrink:1。 D , E 设置为 flex-shrink:2</p>
<p>D , E 宽度与 A, B, C 不同</p>
<div id="content">
  <div class="box" style="background-color:red;">A</div>
  <div class="box" style="background-color:lightblue;">B</div>
  <div class="box" style="background-color:yellow;">C</div>
  <div class="box1" style="background-color:brown;">D</div>
  <div class="box1" style="background-color:lightgreen;">E</div>
</div>
</body>
</html>

  • flex-basis

设置第二个弹性盒元素的初始长度为 80 像素:
div:nth-of-type(2) {flex-basis: 80px;}


  • flex

Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性。

任何一个容器都可以指定为Flex布局。


  • align-self

居中对齐弹性对象元素内的某个项:
#myBlueDiv{    align-self:center;}