Web04--Flex布局

1、flex布局

1.1 flex认识

1.2 flex组成

1.3 flex布局

1.3.1 主轴对齐方式

html 复制代码
<!DOCTYPE html>
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>flex--主轴对齐方式</title>
    <style>
        .box{
            width: 900px;
            height: 400px;
            border: 1px solid;
            display: flex;
            /* justify-content  项目在主轴(水平)方向的排布
                    flex-start:默认 从起点开始排 顺着主轴方向
                    flex-end:   从终点开始排  逆着主轴方向
                    center      居中
                    space-between:左 右 紧贴两边,中间平均分
                    space-around:均匀分布在盒子两侧,二倍宽
                    space-evenly:全部平均分

                       */
            justify-content: space-evenly;
        }
        .box div{
            width: 200px;
            height: 100px;
            background-color: aqua;
            border: 1px dashed rebeccapurple;
        }
    </style>
</head>
<body>
   <div class="box">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
   </div> 
</body>
</html>

1.3.2 侧轴对齐方式

html 复制代码
<!DOCTYPE html>
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>侧轴对齐方式</title>
    <style>
        .box{
            width: 900px;
            height: 400px;
            border: 1px solid;
            display: flex;
            /* align-items  项目在侧轴方向的排布
                    flex-start:默认 上 侧轴的起点
                    flex-end:  侧轴终点
                    center      侧轴居中
                stretch:如果子元素没有高,默认拉伸高,沿着侧轴方向拉伸
                       */
            /* align-items: stretch; */
            /* justify-content: center; */
        }
        .box div{
            width: 200px;
            /* height: 100px; */
            background-color: aqua;
            border: 1px dashed rebeccapurple;
        }
    </style>
</head>
<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div> 
</body>
</html>

1.3.3 修改主轴方向

html 复制代码
<!DOCTYPE html>
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>改变主轴方向</title>
    <style>
        .box{
            width: 900px;
            height: 600px;
            border: 1px solid;
            display: flex;
            /* flex-direction:调整主轴的方向
                    row 默认 水平方向 行 从左到右
                    column 垂直方向 列  从上到下
                    row-reverse 水平方向 从右到左
                    column-reverse 垂直方向 从下到上
                       */
            flex-direction: column-reverse;
        }
        .box div{
            width: 200px;
            height: 100px;
            background-color: aqua;
            border: 1px dashed rebeccapurple;
        }
    </style>
</head>
<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div> 
</body>
</html>

1.3.4 align-self

html 复制代码
<!DOCTYPE html>
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>align-self</title>
    <style>
        * {
            box-sizing: border-box;
        }
        .box {
            display: flex;
            width: 780px;
            height: 500px;
            border: 1px solid red;
            align-items: center;
        }
        .box .son {
            width: 234px;
            height: 100px;
            background-color: pink;
            border: 1px dashed deeppink;
        }
        .box .son:nth-child(1){
			/* align-slef:单独控制某个弹性盒子的侧轴对齐方式 */
            align-self: flex-start;
        }
        .box .son:nth-child(3){
            align-self: flex-end;
        }
     </style>
</head>
<body>
     <div class="box">
        <div class="son">1</div>
        <div class="son">2</div>
        <div class="son">3</div>
     </div>
</body>
</html>

1.3.5 弹性伸缩比

html 复制代码
<!DOCTYPE html>
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>弹性伸缩比</title>
    <style>
        .box{
            width: 1000px;
            height: 600px;
            border: 1px solid;
            display: flex;
        }
        .box div{
            flex: 1;
            width: 200px;
            height: 100px;
            background-color: aqua;
            border: 1px dashed rebeccapurple;
        }
        .box div:nth-child(1){
            flex: 2;
        }
        .box div:last-child{
            flex: 2;
        }
    </style>
</head>
<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div> 
</body>
</html>

1.3.6 弹性盒子换行

弹性盒子可以自动挤压或拉伸,默认情况下,所有盒子都在一行显示。

属性名:flex-wrap

属性值:

wrap:换行

nowrap:不换行(默认)

html 复制代码
<!DOCTYPE html>
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>弹性盒子换行</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .box{
            width: 700px;
            height: 500px;
            background-color: pink;
            display: flex;
            /* flex-wrap wrap 换行,nowrap 不换行 */
            flex-wrap: wrap;
            justify-content: space-between;
            align-items: center;
        }
        .son{
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="son">1</div>
        <div class="son">2</div>
        <div class="son">3</div>
        <div class="son">4</div>
        <div class="son">5</div>
        <div class="son">6</div>
     </div>
</body>
</html>

1.3.7 行对齐方式

html 复制代码
<!DOCTYPE html>
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>行对齐方式</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .box{
            width: 700px;
            height: 500px;
            border: 1px solid;
            display: flex;
            /* 添加了换行,浏览器就会认为你是多行 */
            flex-wrap: wrap;
            justify-content: space-evenly;
            /* 1.多行 2.控制行在侧轴上的对齐方式  */
            align-content: space-evenly;
            
        }
        .son{
            width: 200px;
            height: 200px;
            background-color: aqua;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="son">1</div>
        <div class="son">2</div>
        <div class="son">3</div>
        <div class="son">4</div>
        <div class="son">5</div>
        <div class="son">6</div>
     </div>
</body>
</html>
相关推荐
2501_915918415 分钟前
Web 前端可视化开发工具对比 低代码平台、可视化搭建工具、前端可视化编辑器与在线可视化开发环境的实战分析
前端·低代码·ios·小程序·uni-app·编辑器·iphone
程序员的世界你不懂1 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
索迪迈科技1 小时前
网络请求库——Axios库深度解析
前端·网络·vue.js·北京百思可瑞教育·百思可瑞教育
gnip1 小时前
JavaScript二叉树相关概念
前端
rannn_1111 小时前
【Javaweb学习|实训总结|Week1】html基础,CSS(选择器、常用样式、盒子模型、弹性盒布局、CSS定位、动画),js(基本类型、运算符典例)
css·笔记·学习·html
attitude.x2 小时前
PyTorch 动态图的灵活性与实用技巧
前端·人工智能·深度学习
β添砖java2 小时前
CSS3核心技术
前端·css·css3
空山新雨(大队长)2 小时前
HTML第八课:HTML4和HTML5的区别
前端·html·html5
猫头虎-前端技术3 小时前
浏览器兼容性问题全解:CSS 前缀、Grid/Flex 布局兼容方案与跨浏览器调试技巧
前端·css·node.js·bootstrap·ecmascript·css3·媒体