前端基础(六):Flex、Grid布局



一、Flex弹性盒模型

图片

<style>
        #parent{width: 300px;height: 300px;border: 1px black solid;margin: 20px auto;display: flex;}
        #box{width: 100px;height: 100px;background: red;margin: auto;}
    </style>
</head>
<body>
    <!-- <div id="parent">    
        <div id="box"></div>
    </div> -->
    <!-- 快捷写法:div#parent>div#box -->
</body>
</html>

1、Flex父容器上的一些属性

1.1、flex-direction

flex-direction用来控制子项整体布局方向,是从左往右还是从右往左,是从上往下还是从下往上。
图片

<style>
        #box{width: 300px;height: 300px;border: 1px black solid;margin: 20px auto;display: flex;
        flex-direction: column;
        }
        #box div{width: 50px;height: 50px;color: white;line-height: 50px;text-align: center;
        background: red;
        }
    </style>
</head>
<body>
    <div id="box">
        <div>1</div><!-- 快捷写法:div{$}*3 -->
        <div>2</div>
        <div>3</div>
    </div>
</body>
</html>

1.2、flex-wrap

用来控制子项整体单行显示还是换行显示
图片

1.3、flex-flow

flex-flow属性是flex-direction和flex-wrap的缩写,表示flex布局的flow流动特性,第一个值表示方向,第二个值表示换行,中间用空格隔开。

 #box{width: 300px;height: 300px;border: 1px black solid;margin: 20px auto;display: flex;
        /* flex-direction: column;flex-wrap: wrap; */flex-flow: column wrap;
        }

1.4、justify-content

justify-content属性决定了主轴方向上子项的对齐和分布方式
在这里插入图片描述

1.4、align-items

align-items中的items指的就是flex子项们,因此align-items指的就是flex子项们相对于flex容器在侧轴方向上的对齐方式。(一行中每一个子项的排列)
在这里插入图片描述

 <style>
        #box{width: 300px;height: 300px;border: 1px black solid;margin: 20px auto;
        display: flex;justify-content: space-evenly;align-items: flex-start;}
        #box div{width: 50px;background: red; }/* 不加高度,会自适应 */
    </style>
</head>
<body>
    <div id="box">
        <div>测试文本</div>
        <div>测试文本测试文本</div>
        <div>测试文本测试文本</div>
        <div>测试文本测试文本测试文本</div>
        <div>测试文本</div>
    </div>
</body>
</html>

图片

1.4、align-content

align-content可以看成和justify-content是相似且对立的属性,如果所有flex子项只有一行,则align-content属性是没有任何效果的。(多行之间的对齐方式)
在这里插入图片描述

2、Flex子元素上的一些属性

图片
flex:0 1 auto(默认值 )(优先级会更高 )

<style>
        #box{width: 300px;height: 300px;border: 1px black solid;margin: 20px auto;display: flex;}
        #box div{width: 50px;height: 50px;color: white;line-height: 50px;text-align: center;background: red; }

        /* #box div:nth-child(2){order: 1;} */
        /* #box div:nth-child(3){order: -1;} *//* 排序处理 */

        #box div:nth-child(2){background: yellow;color: black;flex-grow: 1;}/* 会将剩余的空白全部占满,0.5会占一半 */
        #box div:nth-child(3){background: blue;color: black;flex-grow: 2;}
        #box div:nth-child(1){align-self: center;}
    </style>
</head>
<body>
    <div id="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
    </div>
</body>
</html>

二、Flex案例

1、骰子的点数

<style>
        #box1{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;display: flex ;
        justify-content:center;align-items: center;
        }
        #box1 div{width: 30px;height: 30px;background: black;border-radius: 50%;}

        #box2{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;display: flex ;
        justify-content:space-evenly;
        }
        #box2 div{width: 30px;height: 30px;background: black;border-radius: 50%;}
        #box2 div:last-child{align-self: flex-end;}

        #box3{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;display: flex ;
        justify-content:space-between;align-items: center;
        }
        #box3 div{width: 30%;height: 30%;background: black;border-radius: 50%;}
        #box3 div:first-child{align-self: flex-start;}
        #box3 div:last-child{align-self: flex-end;}


        #box4{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;display: flex ;
        flex-wrap: wrap;}
        #box4 div{width: 100%;display: flex;justify-content: space-between;}
        #box4 div:last-child{align-items: flex-end;}
        #box4 i{display: block; width:30%;height:60%;background: black;border-radius: 50%;}
       
        #box5{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;display: flex ;
        flex-wrap: wrap;}
        #box5 div{width: 100%;display: flex;justify-content: center;align-items: center;}
        #box5 div:first-child{align-items:flex-start;justify-content: space-between;}
        #box5 div:last-child{align-items: flex-end;justify-content: space-between;}
        #box5 i{display: block; width:30%;height:90%;background: black;border-radius: 50%;}
       
        #box6{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;display: flex ;
        flex-wrap: wrap;}
        #box6 div{width: 100%;display: flex;justify-content: space-between}
        #box6 div:first-child{align-items:flex-start;}
        #box6 div:last-child{align-items: flex-end;}
        #box6 i{display: block; width:30%;height:90%;background: black;border-radius: 50%;}
    
    </style>
</head>
<body>
    <div id="box1">
        <div></div>
    </div>

    <div id="box2">
        <div></div>
        <div></div>
    </div>

    <div id="box3">
        <div></div>
        <div></div>
        <div></div>
    </div>

    <div id="box4">
        <div>
            <i></i>
            <i></i>
        </div>
        <div>         
            <i></i>
            <i></i>
        </div>
    </div>

    <div id="box5">
        <div>
            <i></i>
            <i></i>
        </div>
        <div>
            <i></i>
        </div>
        <div>         
            <i></i>
            <i></i>
        </div>
    </div>

    <div id="box6">
        <div>
            <i></i>
            <i></i>
        </div>
        <div>
            <i></i>
            <i></i>
        </div>
        <div>         
            <i></i>
            <i></i>
        </div>
    </div>
</body>
</html>

在这里插入图片描述

2、两列固定,一列自适应

<style>
        *{margin: 0;padding: 0;}
       #main{display: flex;}
       #left{width: 200px;height: 200px;background: red;} 
       #center{flex: 1; height: 300px;background: yellow;} 
       #right{width: 150px;height: 200px;background: blue;} 
    </style>
</head>
<body>
    <div id="main">
        <div id="left"></div>
        <div id="center"></div>
        <div id="right"></div>
    </div>
</body>
</html>

在这里插入图片描述

3、百度弹性导航

三、Grid网格布局

Grid布局是一个二维的布局方法,纵横两个方向总是同时存在。
图片

1、grid父元素上属性

1.1、grid-template-coiumns、grid-template-rows

  • 对网格进行横纵划分,形成二维布局。单位可以是像素,百分比,自适应以及fr单位(网格剩余空间比例单位)。
  • 有时候,我们网格的划分是很规律的,如果需要添加多个横纵网格时,可以利用repeat()语法进行简化操作。
<style>
        .box{width: 500px;height: 500px;border: 1px gray dotted;display: grid;
        /* grid-template-rows: 100px auto 25%; *//* 3行 */
        /* grid-template-columns: 100px 100px 200px 100px; *//* 4列 */
            
        /* grid-template-rows: 1fr 1fr 1fr ; 
        grid-template-columns:1fr 1fr 1fr; */

        grid-template-rows: repeat(3,1fr) ; 
        grid-template-columns:repeat(3,1fr);}
        .box div{background: red;border: 1px black solid;}
    </style>
</head>
<body>
    <div class="box">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
       
    </div>
</body>
</html>

1.2、grid-template-areas、grid-template

  • area是区域的意思,grid-template-areas就是给我们的网格划分区域的。此时grid子项只要使用grid-area属性指定其隶属于那个区。
  • grid-template是grid-template-rows ,grid-template-columns和grid-template-areas属性的缩写。
<style>
        .box2{width: 300px;height: 300px;border: 1px gray dotted;display: grid;
        grid-template-rows: repeat(3,1fr) ; 
        grid-template-columns:repeat(3,1fr);
        grid-template-areas: 
        "a1 a1 a1"
        "a2 a2 a3" 
        "a2 a2 a3";      /* 只能形成矩形,不能形成特殊图形 */
        
         /* 复合写法 */
        /* grid-template: 
        "a1 a1 a1" 1fr
        "a2 a2 a3" 1fr
        "a2 a2 a3" 1fr
        / 1fr 1fr 1fr ; *//* 纵向  */  
    }
        .box2 div{background: red;border: 1px black solid;}
        .box2 div:nth-child(1){grid-area: a1;}
        .box2 div:nth-child(2){grid-area: a2;}
        .box2 div:nth-child(3){grid-area: a3;}

    </style>
</head>
<body>
    <div class="box2">
        <div>1</div>
        <div>2</div>
        <div>3</div>
       
    </div>
</body>
</html>

在这里插入图片描述

1.3、grid-column-gap、grid-row-gap

  • grid-column-gap和grid-row-gap属性用来定义网格中网格间隙的尺寸。
  • css grid-gap属性是grid-column-gap和grid-row-gap属性的缩写。
 grid-column-gap: 10px;
 grid-row-gap: 20px;
 /* 复合写法 */
 /*  grid-gap: 20px 10px; */

在这里插入图片描述

1.4、justify-items、align-items

  • justify-items指定了网格元素的水平呈现方式,是水平拉伸显示,还是左中右对齐。align-items指定了网格元素的垂直呈现方式,是垂直拉伸显示,还是上中下对齐。
  • place-items可以让align-items和justify-items属性写在单个声明中。
  • 针对网格中的一个一个元素的
    在这里插入图片描述
<style>
        .box3{width: 500px;height: 500px;border: 1px gray dotted;display: grid;
        grid-template-rows: repeat(3,1fr) ; 
        grid-template-columns:repeat(3,1fr);
        /* justify-items:center;
        align-items: center; */
        place-items: end end;
        /* 第一个对应的是纵向的(align-items),第二个是水平的(justify-items) */
    }
        .box3 div{background: red;border: 1px black solid;}
    </style>
</head>
<body>
    <div class="box3">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
    </div>
</body>
</html>

在这里插入图片描述

1.5 、justify-content、align-content

  • justify-content指定了网格元素的水平分布方式。
  • align-content指定了网格元素的水平分布方式。
  • place-content可以让align-content和justify-content属性写在一个CSS声明中。
  • 针对整个网格的
    在这里插入图片描述
 <style>
        .box3{width: 500px;height: 500px;border: 1px gray dotted;display: grid;
        grid-template-rows: repeat(3,auto) ; 
        grid-template-columns:repeat(3,auto);/* auto自适应 */
      
        justify-content: start;
        align-content:c;
        /* 复合写法 */
       /*  place-content: space-between  space-between ; */
    }
        .box3 div{background: red;border: 1px black solid;}


    </style>
</head>
<body>
    <div class="box3">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
       
    </div>
</body>
</html>

2、grid子元素上的属性

在这里插入图片描述

 <style>
         .box{width: 500px;height: 500px;border: 1px gray dotted;display: grid;
       
        grid-template-rows: repeat(3,1fr) ; 
        grid-template-columns:repeat(3,1fr);}

        .box div{background: red;border: 1px black solid;
        grid-column-start: 2;/* 纵向上启始的那条线是第二条线 */
        grid-column-end: 3;/* 纵向上结束的那条线是第三条线 */
        grid-row-start: 2;/* 横向上开始的那条线 */
        grid-row-end: 4;/* 横向上结束的那条线 ,四条线将区域锁定*/

        /*  grid-row-end: span 2; *//* 只有end有span属性,不再表示结束的位置,表示截取的网格个数 */

        /* 复合写法,通过斜线划开,不是空格 */
        /* grid-column: 2/3; */
       /*  grid-row: 2/span 2; *//* 开始/结束 */

        }

    </style>
</head>
<body>
    <div class="box">
        <div></div>
    </div>
</body>
</html>
 grid-area: 3/2/4/4;/* 横向起始/纵向起始/横向结束/纵向结束 */
 <style>
        .box2{width: 500px;height: 500px;border: 1px gray dotted;display: grid;
       grid-template-rows: repeat(3,1fr) ; 
       grid-template-columns:repeat(3,1fr);
       }
       .box2 div{background: red;border: 1px black solid; }
       .box2 div:nth-child(2){/* justify-self: start;align-self:end; */place-self: end start;}


    </style>
</head>
<body>
    <div class="box2">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
    </div>
    
</body>
</html>

四、Grid案例

1、骰子的点数

<style>
        /* 圆角边框 */
        .box{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;
            display: grid;
            grid-template-columns: repeat(3,1fr);
            grid-template-rows: repeat(3,1fr);
            place-items: center center;/* 每个网格居中处理 */
        }
        /* 点数 */
        .box div{width: 20px;height: 20px;background: black;border-radius: 50%;}
        .box div:nth-child(1){grid-area: 2/2/3/3;}


         /* 圆角边框 */
         .box2{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;
            display: grid;
            grid-template-columns: repeat(3,1fr);
            grid-template-rows: repeat(3,1fr);
            place-items: center center;/* 每个网格居中处理 */
        }
        /* 点数 */
        .box2 div{width: 20px;height: 20px;background: black;border-radius: 50%;}
        .box2 div:nth-child(2){grid-area: 3/3/4/4;}


        /* 圆角边框 */
         .box3{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;
            display: grid;
            grid-template-columns: repeat(3,1fr);
            grid-template-rows: repeat(3,1fr);
            place-items: center center;/* 每个网格居中处理 */
            grid-template-areas:
            "a1 a2 a3"
            "a4 a5 a6"
            "a7 a8 a9";
        }
        /* 点数 */
        .box3 div{width: 20px;height: 20px;background: black;border-radius: 50%;}
        .box3 div:nth-child(2){grid-area:a5;}
        .box3 div:nth-child(3){grid-area:a9;}

        /* 圆角边框 */
        .box4{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;
            display: grid;
            grid-template-columns: repeat(3,1fr);
            grid-template-rows: repeat(3,1fr);
            place-items: center center;/* 每个网格居中处理 */
            grid-template-areas:
            "a1 a2 a3"
            "a4 a5 a6"
            "a7 a8 a9";
        }
        /* 点数 */
        .box4 div{width: 20px;height: 20px;background: black;border-radius: 50%;}
        .box4 div:nth-child(2){grid-area:a3;}
        .box4 div:nth-child(3){grid-area:a7;}
        .box4 div:nth-child(4){grid-area:a9;}

        /* 圆角边框 */
        .box5{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;
            display: grid;
            grid-template-columns: repeat(3,1fr);
            grid-template-rows: repeat(3,1fr);
            place-items: center center;/* 每个网格居中处理 */
            grid-template-areas:
            "a1 a2 a3"
            "a4 a5 a6"
            "a7 a8 a9";
        }
        /* 点数 */
        .box5 div{width: 20px;height: 20px;background: black;border-radius: 50%;}
        .box5 div:nth-child(2){grid-area:a3;}
        .box5 div:nth-child(3){grid-area:a7;}
        .box5 div:nth-child(4){grid-area:a9;}
        .box5 div:nth-child(5){grid-area:a5;}

        /* 圆角边框 */
        .box6{width: 100px;height: 100px;border: 1px black solid;border-radius: 5px;
            display: grid;
            grid-template-columns: repeat(3,1fr);
            grid-template-rows: repeat(3,1fr);
            place-items: center center;/* 每个网格居中处理 */
            grid-template-areas:
            "a1 a2 a3"
            "a4 a5 a6"
            "a7 a8 a9";
        }
        /* 点数 */
        .box6 div{width: 20px;height: 20px;background: black;border-radius: 50%;}
        .box6 div:nth-child(2){grid-area:a3;}
        .box6 div:nth-child(3){grid-area:a7;}
        .box6 div:nth-child(4){grid-area:a9;}
        .box6 div:nth-child(5){grid-area:a4;}
        .box6 div:nth-child(6){grid-area:a6;}

    </style>
</head>
<body>
    <div class="box">
        <div></div>
    </div>

    <div class="box2">
        <div></div>
        <div></div>
    </div>

    <div class="box3">
        <div></div>
        <div></div>
        <div></div>
    </div>

    <div class="box4">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

    <div class="box5">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>

    <div class="box6">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

2、百度搜索风云榜

<style>
        .box{width: 280px;height: 352px;margin: 20px auto;display: grid;
            grid-template-columns: repeat(3,1fr);
            grid-template-rows: repeat(4,1fr);
            grid-template-areas:
            "a1 a2 a2"
            "a3 a2 a2"
            "a4 a4 a5"
            "a6 a7 a7";
            grid-gap: 6px 6px;/* 间距 */
        }
        .box div{background: red;}
        .box div:nth-child(1){grid-area: a1;}
        .box div:nth-child(2){grid-area: a2;}
        .box div:nth-child(3){grid-area: a3;}
        .box div:nth-child(4){grid-area: a4;}
        .box div:nth-child(5){grid-area: a5;}
        .box div:nth-child(6){grid-area: a6;}
        .box div:nth-child(7){grid-area: a7;}
    </style>
</head>
<body>
    <div class="box">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值