18 - 弹性盒模型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xyyojl/article/details/82740719

弹性盒模型

  • flex的兼容性
  • 什么是弹性盒模型
  • flex容器与flex项目
  • 父元素控制子元素整体
  • 子元素的私有特性

flex的兼容性

旧版本的flex写法只支持旧的语法书写

  • 最早 display:box
  • 过渡display:flex box
  • 当今标准 display:flex

兼容性与浏览器内核

兼容性写法,针对老版本的浏览器不能识别最新版的,针对某一些样式,在浏览器中不支持的问题

浏览器 浏览器内核 兼容性前缀
IE/360/猎豹/百度/世界之窗 Trident -ms-
火狐 Gecko -moz-
Opera Blink -o-
苹果safari/安卓默认 Webkit (Safari内核原型,开源) -webkit-
Chrome(谷歌自主研发) Blink -webkit-

flex容器

  • 由父元素控制子元素布局的方案,需要给父元素添加flex属性,任意元素都可添加flex样式

  • 被添加的元素成为容器,所有子元素自动成为容器的成员(flex项目)

  • 子元素float/clear/vertical-align失效
display: flex/inline-flex;

容器的控制方式

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

flex-direction

项目排列方向(水平没换行会有弹性压缩,宽度变小)

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

  • row: 从左到右的水平方形排列(默认),主轴为水平方向,起点在左端
  • row-reverse 从右到左,与row相反,主轴为水平方向,起点在右端
  • column 从上往下排列,主轴为垂直方向,起点在上沿
  • column-reverse: 从下往上排列,与column相反,主轴为垂直方向,起点在下沿

flex-direction:row的效果图:

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo - flex </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            display: flex;
            flex-direction: row;
        }
        .box .box-item{
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
    </div>
</body>
</html>

下面效果图实现的代码只需改变flex-direction

flex-direction:row-reverse的效果图:

这里写图片描述

flex-direction:column的效果图:

这里写图片描述
flex-direction:column-reverse的效果图:

这里写图片描述

内部盒子换行方式

flex-wrap

  • nowrap: 默认单行(弹性压缩)
  • wrap 多行显示超出换行,类似于float,高度不塌陷,从上往下换行
  • wrap-reverse 翻转wrap排列,从下往上换行,第一行在下方

flex-wrap:nowrap的效果图

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo - flex-wrap:nowrap </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            display: flex;
            flex-wrap: nowrap;
        }
        .box .box-item{
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
    </div>
</body>
</html>

flex-wrap:wrap的效果图,实现下面的效果,基于上面的代码,修改flex-wrap的值

这里写图片描述

flex-wrap:wrap-reverse的效果图,实现下面的效果,基于上面的代码,修改flex-wrap的值

这里写图片描述

flex-flow

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

flex-flow: <flex-direction> || <flex-wrap>;

通过上面的两个属性的组合,形成各种各样的效果

下面举一个例子:

效果图:

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo - flew-flow </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            height: 600px;
            display: flex;
            flex-flow: column wrap;
        }
        .box .box-item{
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
    </div>
</body>
</html>

justify-content

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

justify-content: flex-start | flex-end | center | space-between | space-around;
  • flex-start(默认值):当前轴起始位置开始排列
  • flex-end:当前轴起始位置对面开始排列
  • center: 居中排列
  • space-between:两端对齐,项目之间的间隔都相等。将空隙均分配到元素间排列
  • space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍与两边的边框是有间距的

下面的例子是假设主轴为从左到右。

flex-start: flex-start;的效果图:

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo -  </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            height: 110px;
            border: 1px solid red;
            display: flex;
            justify-content: flex-start;
        }
        .box .box-item{
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
    </div>
</body>
</html>

flex-start: flex-end;的效果图:

这里写图片描述

justify-content: center;的效果图
这里写图片描述

justify-content:space-between;的效果图

这里写图片描述

justify-content:space-around;的效果图

这里写图片描述

align-items

align-items属性定义项目在交叉轴上如何对齐

.box {
  align-items: flex-start | flex-end | center | baseline | stretch;
}
  • stretch: 默认,如果项目未设置高度或设置高度为auto,将占满整个容器的高度
  • flex-start:交叉轴的起点对齐。`
  • flex-end:交叉轴的终点对齐。
  • center:交叉轴的中点对齐。
  • baseline: 项目的第一行文字的基线对齐。

下面的例子是假设交叉轴为从上到下。

align-items: flex-start的效果图

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo -  </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            display: flex;
            align-items: flex-start;
        }
        .box .box-item{
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }
        .box .item-tall{
            height: 300px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
    </div>
</body>
</html>

align-items: flex-end的效果图

这里写图片描述

align-items: center的效果图

这里写图片描述

align-items: baseline 的效果图

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo -  </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            display: flex;
            align-items: baseline;
        }
        .box .box-item{
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }
        .box .item-tall{
            height: 300px;
            font-size: 88px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
    </div>
</body>
</html>

align-items: stretch的效果图

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo -  </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            display: flex;
            align-items: stretch;
        }
        .box .box-item{
            width: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }
        .box .item-tall{
            height: 300px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
    </div>
</body>
</html>

align-content

多行(列)元素对齐方式

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

更多详细例子,这里就不写啦:

Flex 布局示例

item项目属性

设置在每一个项目的样式,用于单独控制

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

order

.box{
    order: 数值;
}

order属性定义项目的排列顺序。数值(序号)越小,排列越靠前,默认为0。和元素的书写顺序无关

效果图

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo -  </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            display: flex;
        }
        .box .box-item{
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }
        .order{
            order: -1;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item order">4</div>
    </div>
</body>
</html>

flex-grow

单个元素的放大比例

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

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

效果图:

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo -  </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 600px;
            display: flex;
            /* align-items: stretch; */
        }
        .box .box-item{
            position: relative;
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }
        .box .box-item span{
            position: absolute;
            width:100px;
            left: -50%;
            margin-left: 50px;
            top: 10px;
            color: #fff;
            font-size: 16px;
            line-height: 1;
        }
        .box-item2{
            flex-grow:2
        }
        .box .box-item2 span{
            margin-left: 320px;
        }

    </style>
</head>
<body>
    <div class="box">
        <div class="box-item">1
            <span>flex-grow:1</span>
        </div>
        <div class="box-item box-item2">2
            <span>flex-grow:2</span>
        </div>
        <div class="box-item">3
            <span>flex-grow:1</span>
        </div>
    </div>
</body>
</html>

flex-shrink

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

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

负值对该属性无效。

效果图

这里写图片描述

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>小demo -  </title>
    <style>
        body{
            margin: 0;
        }
        .box{
            width: 280px;
            display: flex;
        }
        .box .box-item{
            position: relative;
            width: 100px;
            height: 100px;
            margin: 5px;
            background-color: red;
            text-align: center;
            line-height: 100px;
            color: #fff;
            font-size: 50px;
        }
        .box .box-item span{
            position: absolute;
            width:100px;
            left: -50%;
            margin-left: 50px;
            top: 10px;
            color: #fff;
            font-size: 16px;
            line-height: 1;
        }
        .box-item1{
            flex-shrink: 0;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box-item box-item1">1
            <span>flex-shrink:0</span>
        </div>
        <div class="box-item">2
        </div>
        <div class="box-item">3
        </div>
    </div>
</body>
</html>

flex-basis

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

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

flex

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

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

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

align-self

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

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

参考资料:

Flex 布局教程:语法篇

Flex 布局示例

猜你喜欢

转载自blog.csdn.net/xyyojl/article/details/82740719