转 : http://www.cnblogs.com/waisonlong/p/6055020.html

flex的使用以及布局

 

1.添加flex属性后的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<style>
        body{
            font-size:35px;
        }
        .flex-box{
            display:flex;
            display: -webkit-flex;/*chorme*/
       display: -ms-flex;/*ie*/
             padding: 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: gray !important; background: none !important;">#2d2d2d;
            margin-bottom: 10px;
        }
        .flex-content{
            width:300px;
            height:300px;
             padding: 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: gray !important; background: none !important;">#c1c1c1;
            margin:10px;
            border:3px solid #c34343;
        }
        .flex-content2{
            width:300px;
            height:300px;
             padding: 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: gray !important; background: none !important;">#c1c1c1;
            border:3px solid #c34343;
        }
        .common-box{
             padding: 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.8em !important; outline: 0px !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; min-height: auto !important; color: gray !important; background: none !important;">#3d3d3d;
        }
    </style>

  

<div class="flex-box">
<div class="flex-content">我是flex-content</div>
<div class="flex-content">我是flex-content</div>
<div class="flex-content">我是flex-content</div>
</div>
<div class="flex-box">
<div class="flex-content2">我是flex-content2</div>
<div class="flex-content2">我是flex-content2</div>
<div class="flex-content2">我是flex-content2</div>
</div>
<div class="common-box">
<div class="flex-content">我是common-content</div>
<div class="flex-content">我是common-content</div>
<div class="flex-content">我是common-content</div>
</div>

效果如下:

ff47,360,chrome40基本一致,ie10却不支持,查了下是版本问题,flex是新版本ie11之后才使用的,ie10及以前使用的属性是flexbox;,增加display:-ms-flexbox;就支持了。

Flexbox布局的语法经过几年发生了很大的变化,也给Flexbox的使用带来一定的局限性,因为语法版本众多,浏览器支持不一致,致使Flexbox布局使用不多。Flexbox布局主要有三种语法版本:

  1. 2009版本,我们称之为老版本,使用的是“display:box”或者“display:inline-box”;
  2. 2011版本,我们称之为混合版本,使用的是“display:flexbox”或者“display:inline-flexbox”;
  3. 2013版本,也就是最新语法版本,使用的是“display:flex”或者“display:inline-flex”。

2.基本概念

引用阮一峰老师对其的描述如下:

采用Flex布局的元素,称为Flex容器(flex container),简称"容器"。

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

容器默认存在两根轴:水平的主轴(main axis)和垂直的交叉轴(cross axis)。主轴的开始位置(与边框的交叉点)叫做main start,结束位置叫做main end;交叉轴的开始位置叫做cross start,结束位置叫做cross end

项目默认沿主轴排列。单个项目占据的主轴空间叫做main size,占据的交叉轴空间叫做cross size

以下6个属性设置在容器上。

  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

2.1 flex-direction属性

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

.flex-box{ flex-direction: row | row-reverse | column | column-reverse; }
row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上沿。
column-reverse:主轴为垂直方向,起点在下沿。
<div class="flex-box">
<div class="flex-content">1我是flex-content</div>
<div class="flex-content">2我是flex-content</div>
<div class="flex-content">3我是flex-content</div>
</div>
以row-reverse的属性为例的效果:

2.2 flex-wrap属性

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

1
2
3
.flex-box{
  flex-wrap: nowrap | wrap | wrap-reverse;
}
nowrap :默认,不换行
wrap :换行,第一行在上方
wrap-reverse :换行,第一行在下方

默认属性的显示效果:

ie,ff,360如图效果

chorme如下图效果:

不同浏览器的显示效果有所不同,但其实都是当容器比项目的小的时候,就会被撑大,但是撑到最大也只能是当前浏览器的宽度,但是ie,ff,360里面项目的大小因为单词把其撑大了,不能跟着浏览器的宽度变小而缩小,而chorme却不一样,不会因为项目的文字而把项目撑大。

当使用wrap属性时

2.3 flex-flow属性

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

.flex-box {
flex-flow: <flex-direction> || <flex-wrap>;
}

2.4 justify-content属性

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

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

它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。

flex-start(默认值):左对齐
flex-end:右对齐
center: 居中
space-between:两端对齐,项目之间的间隔都相等。
space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

2.5 align-items属性

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

它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。

flex-start:交叉轴的起点对齐。
flex-end:交叉轴的终点对齐。
center:交叉轴的中点对齐。
baseline: 项目的第一行文字的基线对齐。
stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

3.6 align-content属性

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

.flex-box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}
flex-start:与交叉轴的起点对齐。
flex-end:与交叉轴的终点对齐。
center:与交叉轴的中点对齐。
space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
stretch(默认值):轴线占满整个交叉轴(如果项目未设置高度或设为auto时,并且align-item连用,否则失效)
)

3.项目的属性

以下6个属性设置在项目上。

order
flex-grow
flex-shrink
flex-basis
flex
align-self

3.1 order属性

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

 .item {
order: <integer>;
}

如下设置:

css

<style>
body{
font-size:20px;
}
.flex-box{
display:flex;
display: -webkit-flex;/*chorme*/
display: -ms-flex;/*ie*/
display:-ms-flexbox;/*ie10*/
flex-wrap:wrap; background-color:#2d2d2d;
margin-bottom: 0px;
height:500px;
}
.flex-content{
width:200px;
height:100px;
background-color:#c1c1c1;
margin:10px;
border:3px solid #c34343;
}
.h100{ font-size:10px;
}
.h200{ font-size:20px;
}
.h300{ font-size:30px;
}
.item_1 {
order: 1;
}
.item_2 {
order: 3;
}
.item_3 {
order: 4;
}
.item_4 {
order: 2;
}
</style>

html

<div class="flex-box">
<div class="flex-content h100 item_1">1我是flex-content</div>
<div class="flex-content h200 item_2">2我是flex-content</div>
<div class="flex-content h300 item_3">3我是flex-content</div>
<div class="flex-content h100 item_4">4我是flex-content</div>
</div>

效果如下图显示:

同样垂直方向也适用

3.2 flex-grow属性

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

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

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

如图所示:

容器的宽度是1000px,项目的都设了100px;那么剩余空间就是1000-3*100=700px;按照剩余空间1:2:1的分配,就是175:350:175,最后得到如图显示效果。

3.3 flex-shrink属性

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

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

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

负值对该属性无效。

效果如图:

3.4 flex-basis属性

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

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

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

3.5 flex属性

flex属性是flex-growflex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

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

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

建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。集体使用例子可参考:flex属性flex-grow、flex-shrink、flex-basis

3.6 align-self属性

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

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

父元素的align-items为flex-end;如图

该属性可能取6个值,除了auto,其他都与align-items属性完全一致。

flex的使用以及布局 转载的更多相关文章

  1. flex做的圣杯布局

    now,给大家分享一个用flex写的圣杯布局,大家可以参考一下子 首先圣杯布局是两列固定宽度,中间自适应. 我直接说一下步骤,上图,上图 1.步骤1 2.步骤2 上面就是基本的步骤,下面我把代码给大家 ...

  2. display:flex和display:box布局浏览器兼容性分析

    display:flex和display:box都可用于弹性布局,不同的是display:box是2009年的命名,已经过时,用的时候需要加上前缀:display:flex是2012年之后的命名.在实 ...

  3. 使用display:flex;实现两栏布局和三栏布局

    一.使用display:flex;实现两栏布局 body,div{margin:0px;padding:0px;} .flex-container{display:flex;height:300px; ...

  4. 87.CSS Flex 弹性盒模型布局教程(共用的css在48篇文章gird)

    CSS Flex 弹性盒模型布局教程 Flex 是 Flexible Box 的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性. flex布局就是给任何一个容器添加 dis ...

  5. CSS3总结五:弹性盒子(flex)、弹性盒子布局

    弹性盒子容器的属性与应用 display:flex/inline-flex flex-direction flex-wrap justify-content align-items align-con ...

  6. 彻底搞懂flex弹性盒模型布局

    为什么要用flex 基于css3简单方便,更优雅的实现,浏览器兼容性好,传统的css实现一个div居中布局要写一堆代码,而现在几行代码就搞定了,没有理由不用flex. 兼容性: Base Browse ...

  7. CSS的flex布局(转载)

    我们之前已经学过一些布局模型,比如说浮动,绝对定位等等,但是这些布局方式一是不够简洁,而是使用的范围确实是太窄了. flex模型拥有比较多的属性,来设置多样的布局方式,接下来我们就详细介绍各种属性对布 ...

  8. CSS3 中FLEX快速实现BorderLayout布局

    学习完flex的布局模式之后,我们趁热打铁,来实现一个BoxLayout布局.什么是BoxLayout布局?那我们先上一个图看看 BoxLayout布局写过后端UI代码的编程者应该不陌生了,写前端的代 ...

  9. [Flex] ButtonBar系列——简单布局

    <?xml version="1.0" encoding="utf-8"?> <!--通过layout属性,设置ButtonBar布局--&g ...

随机推荐

  1. WCF实现进程间管道通信Demo

    一.代码结构: 二.数据实体类: using System; using System.Collections.Generic; using System.Linq; using System.Run ...

  2. django系列1--介绍与简单原理, wsgiref模块

    一.web应用框架 Web应用框架(Web application framework)是一种计算机软件框架,用来支持动态网站.网络应用程序及网络服务的开发.这种框架有助于减轻网页开发时共通性活动的工 ...

  3. jQuery--事件, 事件绑定, 阻止事件冒泡, 事件委托,页面载入后函数

    1.常用事件, 按住shift键实现同步选择效果,搜索框联想效果 2.阻止事件冒泡 3.事件委托 4.使用 $(document).ready(function (){...}) 实现文件加载完绑定事 ...

  4. 201621123023《Java程序设计》第9周学习总结

    一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 二.书面作业 1. List中指定元素的删除(题集题目) 1.1 实验总结.并回答:列举至少2种在List中删除 ...

  5. “全栈2019”Java第一百一十一章:内部类可以被覆盖吗?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. iOS中文本属性Attributes

    NSFontAttributeName //设置字体大小 NSParagraphStyleAttributeName //设置段落格式 NSForegroundColorAttributeName / ...

  7. Django2.0 URL配置详解

    转自:https://www.cnblogs.com/feixuelove1009/p/8399338.html Django2.0发布后,很多人都拥抱变化,加入了2的行列. 但是和1.11相比,2. ...

  8. nginx之重写

    rewrite可以写在server段.location段和if段.语法: rewrite regexp replacement [flag] flag是标记.有4种标记,它们的作用如下表. flag ...

  9. SpringBoot 异步线程简单三种样式

    引用:在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x ...

  10. C++基础知识-派生类、调用顺序、访问等级、函数遮蔽

    一.派生类的概念 类之间有一种层次关系,有父亲类,有孩子类. 车这个类,当成父类(也叫基类.超类),派生出卡车.轿车,他们属于孩子类(子类.派生类) 继承:有父亲类,有孩子类,构成了层次关系.继承这种 ...