CSS弹性布局(一)

弹性布局可以用于做响应式页面的制作
使用弹性布局需要使用要display里的flex属性
例:让一个div是弹性布局,代码如下

.div{
	display:flex;
	}

flex里有很多属性,其中设置在容器上的属性有6个

1.flex direction:决定主轴的方向(即项目的排列方向)。

  • row:主轴为水平方向,起点在左边
<head>
    <meta charset="utf-8">
    <title>弹性布局</title>
    <style>
        .div{
            width: 1300px;
            background-color: green;
            height: 200px;
            display: flex;
            flex-direction:row;
        }
        .div1,.div2,.div3{
            width: 300px;
            height: 200px;
        }
        .div1{
            background-color: red;
        }
        .div2{
            background-color: darkcyan;
        }
        .div3{
            background-color: orange;
        }

    </style>
</head>

<body>
<div class="div">
    <div class="div1">one</div>
    <div class="div2">two</div>
    <div class="div3">three</div>
</div>
</body>

效果如下
在这里插入图片描述

  • row-reverse:主轴为水平方向,起点在右边。
.div{
            width: 1300px;
            background-color: green;
            height: 200px;
            display: flex;
            flex-direction:row;
        }

结果如下
在这里插入图片描述

  • column:主轴为垂直方向,起点在上沿。
    例:
.div{
            width: 1300px;
            background-color: green;
            height: 200px;
            display: flex;
            flex-direction:column;
        }

结果如下
在这里插入图片描述

div的高度被自动压缩

  • column-reverse:主轴为垂直方向,起点在下沿。
.div{
            width: 1300px;
            background-color: green;
            height: 200px;
            display: flex;
            flex-direction:column-reverse;
        }

结果如下图
在这里插入图片描述

2.flex-wrap:一行排不下,如何换行

  • wrap:换行,第一行在上方
 .div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            flex-wrap: wrap;
        }

结果如下
在这里插入图片描述

  • nowrap:不换行
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            flex-wrap: wrap;
        }

结果如下
在这里插入图片描述

div不换行,宽度自动收缩

  • wrap-reverse:换行,第一行在下方。
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            flex-wrap: wrap-reverse;
            margin-top: 200px;
        }

结果如下
在这里插入图片描述

3.flex-flow:flex-direction属性和flex-wrap属性的简写形式

.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            flex-flow: row wrap;
            margin-top: 200px;
        }

4.justify-content:项目在主轴上的对齐方式

  • flex-start:左端对齐
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:flex-start;
            margin-top: 200px;
        }

结果如下
在这里插入图片描述

  • flex-end:右端对齐
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:flex-end;
            margin-top: 200px;
        }

结果如下

在这里插入图片描述

  • center:居中对齐
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:center;
            margin-top: 200px;
        }

结果如下
在这里插入图片描述

-space-around:每个项目两侧的间隔相等

.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:space-around;
            margin-top: 200px;
        }

结果如下
在这里插入图片描述

-space-between:两端对齐,项目之间的间隔都相等

.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:space-between;
            margin-top: 200px;
        }

结果如下
在这里插入图片描述

5.align-items:项目在交叉轴上如何对齐

  • flex-start
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:space-between;
            align-item:flex-start;
            
        }

结果如下
在这里插入图片描述

  • flex-end
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:space-between;
            align-item:flex-end;
            
        }

结果如下

在这里插入图片描述

  • center
.div{
            width: 100%;
            background-color: green;
            height: 200px;
            display: flex;
            justify-content:space-between;
            align-item:center;
            
        }

结果如下

在这里插入图片描述

  • stretch::如果项目未设置高度或设为auto,将占满整个容器的高度。
.div{
            width: 100%;
            background-color: green;
            height: 500px;
            display: flex;
            justify-content: space-between;
            align-items: stretch;

        }

结果如下
在这里插入图片描述

  • baseline:项目的第一行文字的基线对齐。
.div{
            width: 100%;
            background-color: green;
            height: 500px;
            display: flex;
            justify-content: space-between;
            align-items: stretch;

        }

在这里插入图片描述

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

  • 12
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值