flex 弹性布局

一. flex 解释
1、flex 布局 为 flexible BOX 的缩写 ,意思为 弹性布局。
2、块级元素和行内块级元素都可以使用flex布局
3、Webkit内核的浏览器,需要加上-webkit前缀。

二. flex 容器 属性

1、flex-direction
此属性决定主轴的方向

.flex{flex-direction: row; // (默认值) 主轴水平方向,从左往右   如图:flex-direction: row-reverse; // 主轴水平方向的逆方向,从右往左flex-direction: column; // 主轴为垂直方向,从上往下flex-direction: column-reverse; // 主轴为垂直方向的逆方向,从下往上
}


2、flex-wrap
此属性定义,如果一条轴线上排列不下,换行的方式

.flex{flex-wrap: nowrap; // (默认)不换行    如图:flex-wrap: wrap; // 正常换行 从上到下    如图:flex-wrap: wrap-reverse; // 逆方向换行 从下到上    如图:
}


3、flex-flow
此属性定义,是flex-direction和flex-wrap的方式;

.flex {flex-flow: <flex-direction>  空格 <flex-wrap>;
}

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

.flex{justify-content: flex-start; // 左对齐(默认)justify-content: flex-end;    // 右对齐justify-content: center;  // 居中justify-content: space-between; // 两端对齐。且项目间间隔相等justify-content: space-around; // 每个项目两侧间隔相等,所以项目间 间隔 比项目与边框间间隔大一倍justify-content:  space-evenly; // 项目间间隔与项目与边框间 间隔均匀分配
}


4、align-items
此属性定义,项目在交叉轴上的对齐方式

.flex{align-items: flex-start; // 交叉轴的起点对齐align-items: flex-end; // 交叉轴的终点对齐align-items:center; // 交叉轴的中点对齐align-items: baseline; // 项目的第一行文字的基线对齐align-items: stretch; // (默认值) 如果项目未设置高度或设为auto ,将充满整父级容器高度
}


4、align-content
此属性定义,多个项目多根轴线的对齐方式,只有一个轴线时没有作用

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



以上的属性都是容器的属性

三. flex 项目 属性

1、order
此属性决定项目的排列顺序,数值越小。排列越靠前。

.box{order: number; // 默认为 0 ,要讲哪个项目向前移动 将其order设置为负数。
}


2、flex-grow
此属性决定项目的放大比列。

.box{flex-grow: 5; // 默认为 0
}


3、flex-shrink
此属性决定项目的缩小比列。

.item {flex-shrink: number; // 默认为1.要将哪个项目缩小 值设为0 ;
}


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

4、flex
此属性定义为 flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

.item{flex: flex-grow flex-shrink flex-basis; // 简写方式
}

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

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

个人主页 index.dodomun.cn

附代码:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>flex布局</title><style>h2 {text-align: center;}.flex {width: 500px;margin: 30px auto;background: #cccccc;}.box {width: 30px;height: 30px;margin: 10px;background: #333;text-align: center;line-height: 30px;color: #fff;}.flex1 {display: flex;flex-direction: row;}.flex2 {display: flex;flex-direction: row-reverse;}.flex3 {display: flex;flex-direction: column;}.flex4 {display: flex;flex-direction: column-reverse;}.flex5 {display: flex;flex-wrap: nowrap;}.flex6 {display: flex;flex-wrap: wrap;}.flex7 {display: flex;flex-wrap: wrap-reverse;}.flex8 {display: flex;justify-content: flex-start;}.flex9 {display: flex;justify-content: flex-end;}.flex10 {display: flex;justify-content: center;}.flex11 {display: flex;justify-content: space-between;}.flex12 {display: flex;justify-content: space-around;}.flex13 {display: flex;justify-content: space-evenly;}.flex14 {display: flex;justify-content: space-evenly;align-items: flex-start;}.flex15 {display: flex;justify-content: space-evenly;align-items: flex-end;}.flex16 {display: flex;justify-content: space-evenly;align-items: center;}.flex17 {display: flex;justify-content: space-evenly;align-items: baseline;}.flex18 {display: flex;justify-content: space-evenly;align-items: stretch;}.flex19 {display: flex;height: 240px;flex-wrap: wrap;align-content: flex-start;}.flex20 {display: flex;height: 240px;flex-wrap: wrap;align-content: flex-end;}.flex21 {height: 240px;display: flex;flex-wrap: wrap;align-content: center;}.flex22 {height: 240px;display: flex;flex-wrap: wrap;align-content: space-between;}.flex23 {height: 240px;display: flex;flex-wrap: wrap;align-content: space-around;}.flex24 {height: 240px;display: flex;flex-wrap: wrap;align-content: stretch;}.flex25 {display: flex;flex-direction: row;}.flex25>.box5 {order: -5;}.flex26 {display: flex;flex-direction: row;}.flex26>.box3 {flex-grow: 3;}.flex27 {display: flex;flex-direction: row;}.flex27>.box {flex: 1;}.flex27>.box3 {flex: 0;}.flex28 {height: 150px;display: flex;justify-content: space-evenly;}.flex28>.box3{align-items: auto;}.flex29 {height: 150px;display: flex;justify-content: space-evenly;}.flex29>.box3{align-self: flex-start;}.flex30 {height: 150px;display: flex;justify-content: space-evenly;}.flex30>.box3{align-self: flex-end;}.flex31 {height: 150px;display: flex;justify-content: space-evenly;}.flex31>.box3{align-self: center;}.flex32 {height: 150px;display: flex;justify-content: space-evenly;}.flex32>.box3{line-height: 50px;align-self: baseline;}</style>
</head><body><h2>1.flex-direction: row</h2><div class="flex flex1"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>2.flex-direction: row-reverse</h2><div class="flex flex2"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>3.flex-direction: column</h2><div class="flex flex3"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>4.flex-direction: column-reverse</h2><div class="flex flex4"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>5.flex-wrap: nowrap</h2><div class="flex flex5"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div></div><h2>6.flex-wrap: wrap</h2><div class="flex flex6"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div></div><h2>7.flex-wrap: wrap-reverse</h2><div class="flex flex7"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div><div class="box">5</div></div><h2>8.justify-content: flex-start</h2><div class="flex flex8"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>9.justify-content: flex-end</h2><div class="flex flex9"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>10.justify-content: center</h2><div class="flex flex10"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>11.justify-content: space-between</h2><div class="flex flex11"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>12.justify-content: space-around</h2><div class="flex flex12"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>13.justify-content: space-evenly</h2><div class="flex flex13"><div class="box">1</div><div class="box">2</div><div class="box">3</div><div class="box">4</div><div class="box">5</div></div><h2>14.align-items: flex-start</h2><div class="flex flex14"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div></div><h2>15.align-items: flex-end</h2><div class="flex flex15"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div></div><h2>16.align-items: center</h2><div class="flex flex16"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div></div><h2>17.align-items: baseline</h2><div class="flex flex17"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div></div><h2>18.align-items: stretch</h2><div class="flex flex18"><div class="box" style="height: auto">1</div><div class="box" style="height: auto">2</div><div class="box" style="height: auto">3</div><div class="box" style="height: auto">4</div><div class="box" style="height: auto">5</div></div><h2>19.align-content: flex-start</h2><div class="flex flex19"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div><div class="box" style="height: 25px">6</div><div class="box" style="height: 40px">7</div><div class="box" style="height: 50px">8</div><div class="box" style="height: 60px">9</div><div class="box" style="height: 55px">10</div><div class="box" style="height: 25px">11</div><div class="box" style="height: 40px">12</div><div class="box" style="height: 50px">13</div><div class="box" style="height: 60px">14</div><div class="box" style="height: 55px">15</div></div><h2>20.align-content: flex-end</h2><div class="flex flex20"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div><div class="box" style="height: 25px">6</div><div class="box" style="height: 40px">7</div><div class="box" style="height: 50px">8</div><div class="box" style="height: 60px">9</div><div class="box" style="height: 55px">10</div><div class="box" style="height: 25px">11</div><div class="box" style="height: 40px">12</div><div class="box" style="height: 50px">13</div><div class="box" style="height: 60px">14</div><div class="box" style="height: 55px">15</div></div><h2>21.align-content: center</h2><div class="flex flex21"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div><div class="box" style="height: 25px">6</div><div class="box" style="height: 40px">7</div><div class="box" style="height: 50px">8</div><div class="box" style="height: 60px">9</div><div class="box" style="height: 55px">10</div><div class="box" style="height: 25px">11</div><div class="box" style="height: 40px">12</div><div class="box" style="height: 50px">13</div><div class="box" style="height: 60px">14</div><div class="box" style="height: 55px">15</div></div><h2>22.align-content: space-between</h2><div class="flex flex22"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div><div class="box" style="height: 25px">6</div><div class="box" style="height: 40px">7</div><div class="box" style="height: 50px">8</div><div class="box" style="height: 60px">9</div><div class="box" style="height: 55px">10</div><div class="box" style="height: 25px">11</div><div class="box" style="height: 40px">12</div><div class="box" style="height: 50px">13</div><div class="box" style="height: 60px">14</div><div class="box" style="height: 55px">15</div></div><h2>23.align-content: space-around</h2><div class="flex flex23"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div><div class="box" style="height: 25px">6</div><div class="box" style="height: 40px">7</div><div class="box" style="height: 50px">8</div><div class="box" style="height: 60px">9</div><div class="box" style="height: 55px">10</div><div class="box" style="height: 25px">11</div><div class="box" style="height: 40px">12</div><div class="box" style="height: 50px">13</div><div class="box" style="height: 60px">14</div><div class="box" style="height: 55px">15</div></div><h2>24.align-content: stretch</h2><div class="flex flex24"><div class="box" style="height: 25px">1</div><div class="box" style="height: 40px">2</div><div class="box" style="height: 50px">3</div><div class="box" style="height: 60px">4</div><div class="box" style="height: 55px">5</div><div class="box" style="height: 25px">6</div><div class="box" style="height: 40px">7</div><div class="box" style="height: 50px">8</div><div class="box" style="height: 60px">9</div><div class="box" style="height: 55px">10</div><div class="box" style="height: 25px">11</div><div class="box" style="height: 40px">12</div><div class="box" style="height: 50px">13</div><div class="box" style="height: 60px">14</div><div class="box" style="height: 55px">15</div></div><h2>1. order (项目5的order值为 -1)</h2><div class="flex flex25"><div class="box box1">1</div><div class="box box2">2</div><div class="box box3">3</div><div class="box box4">4</div><div class="box box5">5</div></div><h2>2. flex-grow (项目3的值为3)</h2><div class="flex flex26"><div class="box box1">1</div><div class="box box2">2</div><div class="box box3">3</div><div class="box box4">4</div><div class="box box5">5</div></div><h2>3. flex-shrink (项目3的值为0)</h2><div class="flex flex27"><div class="box box1">1</div><div class="box box2">2</div><div class="box box3">3</div><div class="box box4">4</div><div class="box box5">5</div></div><h2>4. align-self: auto</h2><div class="flex flex28"><div class="box box1" style="height: 25px">1</div><div class="box box2" style="height: 40px">2</div><div class="box box3" style="height: 50px">3</div><div class="box box4" style="height: 60px">4</div><div class="box box5" style="height: 55px">5</div></div><h2>5. align-self: flex-start</h2><div class="flex flex29"><div class="box box1" style="height: 25px">1</div><div class="box box2" style="height: 40px">2</div><div class="box box3" style="height: 50px">3</div><div class="box box4" style="height: 60px">4</div><div class="box box5" style="height: 55px">5</div></div><h2>6. align-self: flex-end</h2><div class="flex flex30"><div class="box box1" style="height: 25px">1</div><div class="box box2" style="height: 40px">2</div><div class="box box3" style="height: 50px">3</div><div class="box box4" style="height: 60px">4</div><div class="box box5" style="height: 55px">5</div></div><h2>7. align-self: center</h2><div class="flex flex31"><div class="box box1" style="height: 25px">1</div><div class="box box2" style="height: 40px">2</div><div class="box box3" style="height: 50px">3</div><div class="box box4" style="height: 60px">4</div><div class="box box5" style="height: 55px">5</div></div><h2>8. align-self: baseline</h2><div class="flex flex32"><div class="box box1" style="height: 25px">1</div><div class="box box2" style="height: 40px">2</div><div class="box box3" style="height: 50px">3</div><div class="box box4" style="height: 60px">4</div><div class="box box5" style="height: 55px">5</div></div>
</body></html>

css中的flex(弹性)布局相关推荐

  1. CSS中使用flex弹性布局实现上下左右垂直居中排列并设置子元素之间的间距

    场景 Flex是Flexible Box的缩写,意为"弹性布局". 怎样使用弹性布局实现页面上下两个元素上下左右垂直居中排列. 实现如下类似布局 最外层是是一个div,div里面是 ...

  2. 移动web现状、viewport视口、二倍图、移动web开发主流方案、布局技术选型(流式布局、flex弹性布局、less+rem+媒体查询布局、混合布局、媒体查询、bootstrap)

    移动端web现状: 移动端常见浏览器:UC浏览器,QQ浏览器,Opera浏览器,百度手机浏览器,360安全浏览器,谷歌浏览器,搜狗手机浏览器,猎豹浏览器及杂牌浏览器.移动端常见的浏览器都是基于webk ...

  3. 微信小程序开发(三)——IE盒子,Flex弹性布局,色子六面

    目录 ie盒子模型 Flex弹性布局 三大特性: 块元素和内联元素的转换 background-image背景图片 尺寸单位rpx 定位 练习:色子的六面 ie盒子模型 盒子模型是css中一个重要的概 ...

  4. 移动端基础(2)—— flex弹性布局

    一.flex弹性布局 flexible-box-layout 弹性盒式模型,它能更加高效的控制元素的对齐.排列,更重要的是能够自动计算布局内元素的尺寸,无论这个元素的尺寸是固定的还是动态的. 父元素( ...

  5. css什么是自适应布局,CSS中常见的自适应布局是什么

    CSS中常见的自适应布局是什么 发布时间:2020-12-05 13:24:07 来源:亿速云 阅读:102 作者:小新 这篇文章主要介绍CSS中常见的自适应布局是什么,文中介绍的非常详细,具有一定的 ...

  6. flex弹性布局教程-09-容器属性flex-flow

    本节目标 掌握flex-flow的使用,flex-flow是flex-direction和flex-wrap的合集. 掌握上左右下的布局编写技巧. 内容摘要 本篇介绍了容器属性 flex-flow,是 ...

  7. 页面布局 - flex弹性布局

    flex弹性布局 html: <!DOCTYPE html> <html><head><meta charset="utf-8" /> ...

  8. [css] 你有用过弹性布局吗?说说你对它的理解

    [css] 你有用过弹性布局吗?说说你对它的理解 按我的经验在样式规律上大致分下类吧,等宽弹性布局单元素弹性布局多元素定比弹性布局等隙布局用 flex 或 grid 能非常轻松的完成以上效果,而 cs ...

  9. HTML中用弹性布局设置位置,HTML的flex弹性布局

    flex 布局概述 1. flex 是什么flex 是 Flexible Box 的缩写,意为弹性布局 flex 2009 年就已出现,浏览器兼容性很好,请放心使用 2. flex 解决了什么问题块元 ...

  10. flex弹性布局教程-04项目属性flex-grow

    本节目标 掌握flex-grow的使用. 掌握flex-grow放大的计算公式(难点). 掌握编写导航条的技巧. 内容摘要 本篇介绍了项目属性 flex-grow,用于定义项目的扩大系数,用于分配容器 ...

最新文章

  1. 结构体的两种声明方式:堆上和栈上以及在双链表的应用
  2. 读书笔记——《黑客大曝光》(1/8)
  3. python3菜鸟-菜鸟笔记Python3——数据可视化(一)
  4. php 服务器方案,分享几种常见WEB服务器配置方案
  5. 【PAT乙级】1058 选择题 (20 分)
  6. 服务器上flash不显示动画,win10电脑在线预览不能加载flash
  7. 关于php 高并发解决的一点思路
  8. 北大暑期课作业 - 对cnblog 和其他技术博客的分析,比较和展望
  9. JavaScript-操作DOM对象-更新dom节点
  10. Java导入导出Excel工具类ExcelUtil
  11. 用javascript实现有效时间的控制,并显示要过期的时间
  12. 组策略设置IE 11的Compatible View
  13. 关于Python,虚拟环境和Visual Studio Code集成的说明
  14. paste linux 相同字符,Linux命令之字符串处理命令paste命令使用实例
  15. Solaris 图形化界面登陆的控制
  16. python求解在给定递减数组中寻找两个数和等于定值,乘积最小
  17. shell脚本使用getopts自定义传入参数选项
  18. 人像美颜美妆算法入门必备
  19. Processing 椭圆运动模拟
  20. 怎么学计算机基本步骤,学习计算机知识的基本步骤是什么?

热门文章

  1. Python openpyxl 删除excel有删除线的文字
  2. 计算几何之多边形重心
  3. 北京药监局考试计算机操作,考科一电脑操作
  4. 使用vmware+centos7+openfiler搭建达梦DSC两节点集群
  5. 【支付专区】之检查微信预下单返回结果
  6. 分享一下我的从业经历和工作感悟
  7. 慎用!3个容易被打的Python恶搞脚本!
  8. 空气中弥漫着『病毒』的味道
  9. Element的使用
  10. 适用于 Windows 10 的触摸板手势