Flex布局详解及活用

目录

第一章、Flex布局详解

1.1 入门

1.1.1  flex是什么

1.1.2 为什么使用flex

1.1.3 设置方式

1.1.4 组成部分

1.2 常用属性

1.2.1 弹性容器(父)常用属性

1.2.2 弹性盒子(子)常用属性

 第二章、Flex活用--几种常用的布局

2.1 两列/两栏布局

2.2 水平垂直居中

2.3 三栏布局

2.4 圣杯布局

2.5 流式布局

第一章、Flex布局详解

1.1 入门

1.1.1  flex是什么

  • 弹性盒子布局
  • 是一种由浏览器提倡的布局模型
  • 布局网页更加简单灵活
  • 能避免浮动脱标(脱标:脱离标准文档流)的问题
  • 注意:如果子盒子没有设置高度,子盒子将会拉伸填满父盒子高度

1.1.2 为什么使用flex

  • 解决元素居中问题
  • 自动弹性伸缩,合适适配不同大小的屏幕、移动端和微信小程序、app

1.1.3 设置方式

  • 父元素添加display:flex子元素自动挤压或拉伸。
  • 添加display:flex后的视觉效果:子级一行排列/水平排列
  • 水平排列:默认主轴在水平,弹性盒子默认沿着主轴排列

1.1.4 组成部分

  • 弹性容器(父盒子)
  • 弹性盒子(子盒子)
  • 主轴
  • 侧轴

1.2 常用属性

1.2.1 弹性容器(父)常用属性

1.flex-direction   主轴方向 (常用)
2.flex-wrap        主轴一行满了换行 (常用)
3.justify-content  主轴元素对齐方式 (常用)
4.align-items      侧轴元素对齐方式//单行 (常用)
5.flex-flow        1和2的组合 (了解)
6.align-content    侧轴行对齐方式//多行 (了解)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .parents{
            display: flex;
            height: 600px;
            background-color: skyblue;
        }
///-----------------使用flex布局区域-----------------------
        .parents{
            flex-direction: row;
            /* flex-direction: row-reverse; 
            flex-direction: column;
            flex-direction: column-reverse; */
            flex-wrap: nowrap;
            /* flex-wrap: wrap;
            flex-wrap: wrap-reverse; */
            justify-content: center;
            /* justify-content: flex-start;
            justify-content: flex-end;
            justify-content: space-between;
            justify-content: space-around;
            justify-content: space-evenly; */
            align-items: center;
            /* align-items: flex-start;
            align-items: flex-end;
            align-items: baseline;
            align-items: stretch; */
        }
///---------------------------------------------------
        .son{
            padding: 80px;
        }
        .red{
            background-color: red;
        }
        .green{
            background-color: green;
        }
        .yellow{
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div class="parents">
        <div class="son red">1</div>
        <div class="son green">2</div>
        <div class="son yellow">3</div>
    </div>
</body>
</html>
  • flex-direction 主轴方向
row(默认值):主轴为水平方向,起点在左端。
row-reverse:主轴为水平方向,起点在右端。
column:主轴为垂直方向,起点在上沿。
column-reverse:主轴为垂直方向,起点在下沿。

  •  flex-wrap 主轴一行满了是否换行 
nowrap (默认值) 不换行压缩宽度
wrap    换行
wrap-reverses 反向换行

  • justify-content  主轴元素对齐方式

这里的方向与 flex-direction 主轴方向 定义的方向是对应的

flex-start (默认)靠着main-start对齐//参考常见术语(一般是左方向)
flex-end   靠着main-end对齐//参考常见术语(一般是右方向)
center     靠着主轴居中对齐//一般就是居中对齐
space-between 两端对齐,靠着容器壁,剩余空间平分
space-around  分散对齐,不靠着容器壁,剩余空间在每个项目二侧平均分配
space-evenly  平均对齐,不靠着容器壁,剩余空间平分

  •  align-items  侧轴元素对齐方式//单行
flex-start:侧轴的起点对齐。
flex-end:侧轴的终点对齐。
center:侧轴的中点对齐。
baseline: 项目的第一行文字的基线对齐。
stretch(默认值)伸展:如果项目未设置高度或设为auto,将占满整个容器的高度。

  •  flex-flow:flex-direction和flex-wrap的组合

 写法:flex-low: [flex-direction] [flex-wrap]

  •  align-content 侧轴行对齐方式 多行

用法与justify-content 的属性一模一样

1.2.2 弹性盒子(子)常用属性

flex-grow/flex:长大 (常用)
align-self: 覆盖弹性容器(父盒子) align-items 属性 (常用)
order 排序 (了解)
flex-basis: 有效宽度 (了解)
flex-shrinik: 缩小  (了解)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .parents{
            display: flex;
            height: 400px;
            overflow: hidden;
            background-color: skyblue;
        }
        .parents{
            align-items: flex-start;       
        }
        .son{
            padding: 100px;
        }
        .red{
            background-color: red;
            flex-grow: 1;
            /* flex: 1; */
            align-self: center;
            order: 1;
        }
        .green{
            background-color: green;
        }
        .yellow{
            background-color: yellow;
        }
    </style>
</head>
<body>
    
    <div class="parents">
        <div class="son red">1</div>
        <div class="son green">2</div>
        <div class="son yellow">3</div>
    </div>
</body>
</html>
  • flex-grow/flex:长大,默认为0

        -- 在容器主轴上存在剩余空间时, flex-grow才有意义

        -- 类似比例分配

.red{
     background-color: red;
     flex-grow: 1;
     /* flex: 1; */
}

  • align-self: 覆盖弹性容器(父盒子) align-items 属性

        -- 属性与align-items一样,使用后该盒子会有自己的"想法"

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

  •  order:将该盒子排序到第几个,默认为0

 第二章、Flex活用--几种常用的布局

2.1 两列/两栏布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>两列布局</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box {
            display: flex;
            height: 200px;
        }
        .left {
            background-color: rgb(234, 117, 21);
            /* flex-basis定义该项目在分配主轴空间之前提前获得200px的空间 */
            flex-basis: 200px;
            /* flex-grow定义该项目不分配剩余空间 */
            flex-grow: 0;
        }
        .main {
            background-color: rgb(18, 164, 254);
            /* flex-grow定义main占满剩余空间 */
            flex-grow: 1;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="left">left</div>
        <div class="main">main</div>
    </div>
</body>
</html>

2.2 水平垂直居中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>水平/垂直居中</title>
    <style>
        .box {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 500px;
            width: 500px;
            background-color: rgb(18, 164, 254);
        }
        .content {
            height: 200px;
            width: 200px;
            background-color: rgb(234, 117, 21);
            line-height: 200px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="content">我是子元素,我要垂直居中</div>
</div>
</body>
</html>

2.3 三栏布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>三栏布局</title>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    .box {
        display: flex;
        height: 200px;
        margin-bottom: 30px;
    }

    .left {
        background-color: yellow;
        flex-basis: 200px;
        /* flex-basis定义该项目在分配主轴空间之前提前获得200px的空间 */
        flex-grow: 0;
        /* flex-grow定义该项目不分配剩余空间 */
    }

    .main {
        background-color: green;
        flex-grow: 1;
        /* flex-grow定义main占满剩余空间 */
    }

    .right {
        background-color: blue;
        flex-basis: 100px;
        flex-grow: 0;
    }
</style>
<body>
<div class="box">
    <div class="left">left</div>
    <div class="main">main</div>
    <div class="right">right</div>
</div>
</body>
</html>

2.4 圣杯布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>圣杯布局</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    .box {
        display: flex;
        flex-direction: column;
        width: 100%;
        height: 500px;
        background-color: yellow;
        text-align: center;
        font-size: 30px;
    }
    header, footer {
        line-height: 80px;
        background: pink;
    }
    .content {
        display: flex;
        flex: 1;
    }
    nav {
        order: -1;
        background-color: blue;
        width: 80px;
    }
    aside {
        background-color: green;
        width: 80px;
    }
    main {
        flex: 1;
    }

</style>
<body>
    <div class="box">
        <header>header</header>
        <div class="content">
            <main>main</main>
            <nav>nav</nav>
            <aside>aside</aside>
        </div>
        <footer>footer</footer>
    </div>
</body>
</html>

2.5 流式布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>流式布局</title>
</head>
    <style>
        .box {
            display: flex;
            flex-flow: row wrap;
            height: 300px;
            width: 400px;
            background-color: yellow;
        }
        .content {
            width: 25%;
            background-color: blue;
            border: 2px solid red;
            box-sizing: border-box;
        }
    </style>
<body>
    <div class="box">
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
        <div class="content"></div>
    </div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值