Skip to main content

Flex 布局属性详解

简介

通常我们在制作html页面的时候,都是基于盒模型,在2009年,W3C提出了一种新的方案——Flex布局,可以简便、完整、响应式地实现各种页面布局。目前,浏览器的支持情况IE10+及现代浏览器都支持,那么是否该尝试这种新式布局方法呢。

Flex布局是什么?

Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。 任何一个容器都可以指定为Flex布局。

.box{
display: flex;
display: -webkit-flex; //
display: inline-flex; // 行内元素也可以使用Flex布局
}

注意,设为Flex布局以后,子元素的floatclearvertical-align属性将失效。

基本概念

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

flex-container

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end。 项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size

容器的6个属性

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

    • row (默认值) 主轴为水平方向,起点在左端。
    • row-reverse 主轴为水平方向,起点在右端。
    • column 主轴为垂直方向,起点在上沿
    • column-reverse 主轴为垂直方向,起点在下沿
  • 2、flex-wrap 属性定义,如果一条轴线排不下,如何换行

    • nowrap (默认) 不换行
    • wrap 换行,第一行在上方
    • wrap-reverse 换行,第一行在下方
  • 3、flex-flow 属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap

  • 4、justify-content 属性定义了项目在主轴上的对齐方式

    • flex-start (默认) 左对齐
    • flex-end 右对齐
    • center 居中
    • space-between 两端对齐,项目之间的间隔都相等
    • space-around 每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍
  • 5、align-items 属性定义项目在交叉轴上如何对齐

    • stretch (默认) 如果项目未设置高度或设为auto,将占满整个容器的高度
    • flex-start 交叉轴的起点对齐
    • flex-end 交叉轴的终点对齐
    • center 交叉轴的中点对齐
    • baseline 项目的第一行文字的基线对齐
  • 6、align-content 属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用

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

项目的6个属性

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

Flex 布局实例

flex 栅格化布局实例

<!doctype html>
<html lang="en" class="no-js">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./style.css">
<title>WEE-Document-Flex-Layout</title>
<style>
body {
padding: 0 40px;a
background: url(http://og8z552x2.bkt.clouddn.com/grid.png);
}
div {opacity: 0.9;}
.flex-column {
padding:10px;
background-color:#ccc;
}

.flex-column span{
background-color: #666;
margin-bottom: 10px;
line-height:30px;
display: block;
border:1px solid #000;
}
</style>
</head>

<body>

<div class="container">

<div class="col-exp">
<h2>Flex 布局 </h2>
<p>24列栅格化,与上面使用方式类似,栅格更细致,变化更多样</p>
<h3>Flex 布局用法说明 <a target="_blank" href="http://caniuse.com/#search=flex">兼容性</a></h3>
<p>flex 容器 有两种</p>
<p>
flex-column: 容器里的[栅格]以横向排列,与传统栅格化的row一样
</p>
<p>
flex-row: 容器里的[栅格]以竖向排列,表示一层叠一层的排列方式
</p>
<p>
栅格化为24列,如果单行超过24列,即自动换行
</p>
<h3>用法示列</h3>
<div class="flex-column">
<div class="flex-col-12 flex-test"><span>flex-col-12</span></div>
<div class="flex-col-12 flex-test"><span>flex-col-12</span></div>
<div class="flex-col-8 flex-test"><span>flex-col-8</span></div>
<div class="flex-col-16 flex-test"><span>flex-col-16</span></div>
<div class="flex-col-6 flex-test"><span>flex-col-6</span></div>
<div class="flex-col flex-test"><span>flex-col 自动填充宽度</span></div>
<div class="flex-col-6 flex-test"><span>flex-col-6</span></div>
<div class="flex-col-14 flex-test"><span>flex-col-14</span></div>
<div class="flex-col"><span>flex-col 自动填充宽度</span></div>
<div class="flex-col-16 flex-test"><span>flex-col-16</span></div>
<div class="flex-col-12 flex-test"><span>flex-col-12</span></div>

<div class="flex-col-16 flex-row">
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
</div>
<div class="flex-col-8 flex-row">
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
</div>
</div>
<h3>flex-middle 垂直居中 flex-center 水平居中</h3>
<div class="flex-row flex-middle flex-center" style="min-height: 200px;background:#ccc;border:1px solid #ccc">
<div class="flex-col-6 flex-test"><span>flex-col-6垂直居中</span></div>
<div class="flex-col-6 flex-test"><span>flex-col-6多行垂直居中</span></div>
</div>

<h3>flex-push-* 向左偏移</h3>
<div class="flex-column">
<div class="flex-col-12"><span>flex-col-12</span></div>
</div>
<div class="flex-column">
<div class="flex-col-12 flex-push-6"><span>flex-col-12 flex-push-6</span></div>
</div>
<div class="flex-column">
<div class="flex-col-10 flex-push-14"><span>flex-col-10 flex-push-14</span></div>
</div>

<h3>flex-order-* 排序 order越小排在越前面</h3>
<div class="flex-column">
<div class="flex-col-5 flex-order-4"><span>flex-col-4 flex-order-4</span></div>
<div class="flex-col-5 flex-order-3"><span>flex-col-4 flex-order-3</span></div>
<div class="flex-col-5 flex-order-2"><span>flex-col-4 flex-order-2</span></div>
<div class="flex-col-5 flex-order-1"><span>flex-col-4 flex-order-1</span></div>
</div>

<h3>flex-left左对齐 flex-right 右对齐</h3>
<div class="flex-column">
<div class="flex-col-4"><span>flex-col-4</span></div>
<div class="flex-col-4"><span>flex-col-4</span></div>
<div class="flex-col-4"><span>flex-col-4</span></div>
</div>

<div class="flex-column flex-right">
<div class="flex-col-6"><span>flex-col-6</span></div>
<div class="flex-col-6"><span>flex-col-6</span></div>
<div class="flex-col-6"><span>flex-col-6</span></div>
</div>

<div class="flex-row flex-left">
<div class="flex-col-4"><span>flex-col-4 左对齐</span></div>
<div class="flex-col-4 flex-right"><span>flex-col-4 右对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 左对齐</span></div>
</div>

<div class="flex-row">
<div class="flex-col-4 flex-right"><span>flex-col-4 右对齐</span></div>
<div class="flex-col-4 flex-left"><span>flex-col-4 左对齐</span></div>
<div class="flex-col-4 flex-right"><span>flex-col-4 右对齐</span></div>
</div>

<h3>flex-top 顶部对齐 flex-bottom 底部对齐</h3>
<div class="flex-column flex-bottom" style="background:#ccc;min-height:120px">
<div class="flex-col-6"><span>flex-col-6 底部对齐</span></div>
<div class="flex-col-6 flex-top"><span>flex-col-6 顶部对齐</span></div>
<div class="flex-col-6"><span>flex-col-6 底部对齐</span></div>
</div>

<h3>flex-between 等宽对齐 —— 自动调整栅格间距,保持两边间距相同</h3>
<div class="flex-column flex-between">
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
</div>
<div class="flex-row flex-between flex-center" style="background:#ccc;min-height: 300px;">
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
</div>

<h3>flex-around 分散排列 —— 与等宽对齐相同,但靠两侧的间距比[栅格]之间的间距大一倍</h3>
<div class="flex-column flex-around">
<div class="flex-col-3"><span>分散排列</span></div>
<div class="flex-col-3"><span>分散排列</span></div>
<div class="flex-col-3"><span>分散排列</span></div>
<div class="flex-col-3"><span>分散排列</span></div>
</div>
<div class="flex-row flex-around flex-center" style="background:#ccc;min-height: 400px;">
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
<div class="flex-col-4"><span>flex-col-4 等宽对齐</span></div>
</div>

<h3>响应式</h3>
<p><strong>flex-md</strong> 当分辨率低于 992px 的时候,使[容器]变为竖向排列</p>
<div class="flex-column flex-middle flex-md" style="background:#ccc;min-height: 100px;">
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
</div>

<p><strong>flex-sm</strong> 当分辨率低于 768px 的时候,使[容器]变为竖向排列</p>
<div class="flex-column flex-middle flex-sm" style="background:#ccc;min-height: 100px;">
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
</div>

<p><strong>flex-md-hide</strong> 当分辨率低于 992px 的时候,隐藏[栅格]或[容器]</p>
<p><strong>flex-md-show</strong> 当分辨率低于 992px 的时候,显示[栅格]或[容器]</p>
<div class="flex-column flex-middle flex-md" style="background:#ccc;min-height: 100px;">
<div class="flex-col-24"><span>flex-col-24</span></div>
<div class="flex-col flex-md-hide"><span>低于992px则隐藏该栅格</span></div>
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col flex-md-show"><span>低于992px 则显示该栅格</span></div>
<div class="flex-col flex-md-show"><span>低于992px 则显示该栅格</span></div>
<div class="flex-col flex-md-show"><span>低于992px 则显示该栅格</span></div>
</div>

<p><strong>flex-sm-hide</strong> 当分辨率低于 768px 的时候,隐藏[栅格]或[容器]</p>
<p><strong>flex-sm-show</strong> 当分辨率低于 768px 的时候,显示[栅格]或[容器]</p>
<div class="flex-column flex-middle flex-md" style="background:#ccc;min-height: 100px;">
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col flex-sm-hide"><span>低于 768px 则隐藏该栅格</span></div>
<div class="flex-col"><span>flex-col</span></div>
<div class="flex-col flex-sm-show"><span>低于 768px 则显示该栅格</span></div>
</div>

</div>

</div>

</body>

</html>

Flex 布局容器属性演示

<!doctype html>
<html lang="zh-CN">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Flex布局详解</title>
<style>
body {
padding: 0 40px;
background: url(http://og8z552x2.bkt.clouddn.com/grid.png);
}

div {opacity: 0.8;}

.flex-wrap {
display:flex;
padding:10px;
background-color:#ccc;
}
.item {
height:50px;
line-height:50px;
flex: 1;
margin:5px;
background-color:#000;
font-size:30px;
text-align: center;
color:#fff;
}

.flex-row {
flex-direction:row;
}
.flex-row-reverse {
flex-direction:row-reverse;
}
.flex-column {
flex-direction:column;
}
.flex-column-reverse {
flex-direction:column-reverse
}

/*--------flex网格布局------------*/

.flex-grid {
display:flex;
padding:10px;
background-color:#ccc;
justify-content: center;
margin-bottom:20px;
}
.fol-2 {
flex:2;
}
.fol-3 {
flex:3;
}
.fol-4 {
flex:4;
}


</style>
</head>

<body>
<h1>Flex 布局 容器属性演示</h1>
<h2>flex-direction: row </h2>
<div class="flex-wrap flex-row">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>

<h2>flex-direction:row-reverse </h2>
<div class="flex-wrap flex-row-reverse">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>

<h2>flex-direction:column </h2>
<div class="flex-wrap flex-column">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>

<h2>flex-direction:column-reverse </h2>
<div class="flex-wrap flex-column-reverse">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>

<h1>Flex 布局实际案例</h1>
<h2>网格布局演示</h2>
<div class="flex-grid">
<div class="item fol-2">1/2</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
<div class="flex-grid">
<div class="item fol-3">2/3</div>
<div class="item">2</div>
</div>
<div class="flex-grid">
<div class="item">1</div>
<div class="item fol-3">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
</body>

</html>


本文参考引用文档

Flex 布局教程:语法篇

Flex 布局教程:实例篇