grid布局之-子项放置1

布局1

xml 复制代码
<style>
        .container{
            background-color:rgb(208, 164, 35);
            padding:50px;
            margin:50px;
            color:#fff;
            display:grid;
            height:80vh;
            box-sizing: border-box;
            grid-template-rows:repeat(2,50px) 1fr;
            grid-template-columns: 150px 1fr;
            gap:10px;
        }
        .item{
            border:1px solid red;
            background-color:orange;
            display:flex;
            justify-content: center;
            align-items: center;
        }
        .header{
            /* grid-column-start:1;
            grid-column-end:3;
            grid-row-start:1;
            grid-row-end:2; */

            /* 速写属性 */
            /* grid-column:1 / 3; */
            /* grid-row:1/2; */

            /* 该写法表示跨越两个格子 */
            /* grid-column:1 / span 2; */
            /* grid-row:1 / span 1; */
            /* grid-row:1; */

            /* 速写属性 grid-row和grid-column */
            /* 第1个数字是行的起始线,第2个数字是列的起始线,第3个数字是行的终止线,第4个数字是列的终止线, */
            /* grid-area:1/1 /2/3; */
            /* 第1个数字是行的起始线,第2个数字是列的起始线,第3个数字是行跨越几个格子,第4个数字是列跨越几个格子, */
            grid-area:1/1 /span 1/ span 2; 

        }
        .aside {
            grid-area:2/1/span 2/ span 1;
        }
        .tab{
            grid-area:2/2;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item main">main</div>
        <div class="item header">header</div>
        <div class="item aside">aside</div>
        <div class="item tab">tab</div>
    </div>
</body>

2.还有一种写法,

xml 复制代码
 <style>
        .item{
            border:1px solid red;
            background-color:orange;
            display:flex;
            justify-content: center;
            align-items: center;
        }
        .container{
            background-color:rgb(208, 164, 35);
            padding:50px;
            margin:50px;
            color:#fff;
            display:grid;
            height:80vh;
            box-sizing: border-box;
            grid-template-rows:repeat(2,50px) 1fr;
            grid-template-columns: 150px 1fr;
            gap:10px;
            /* 在父容器中定义 */
            grid-template-areas:
            'header header' 
            'aside tab' 
            'aside main';
        }
        .header{
            grid-area:header; 
        }
        .aside {
            grid-area:aside;
        }
        .tab{
            grid-area:tab;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="item main">main</div>
        <div class="item header">header</div>
        <div class="item aside">aside</div>
        <div class="item tab">tab</div>
    </div>
</body>

第二种的写法比较灵活,假如后面想把aside的列放到右边,只需要改这块

效果如下

grid-template速写属性

相关推荐
摘星编程1 天前
React Native + OpenHarmony:ImageSVG图片渲染
javascript·react native·react.js
会编程的土豆1 天前
新手前端小细节
前端·css·html·项目
摘星编程1 天前
OpenHarmony + RN:Text文本书写模式
javascript·react native·react.js
广州华水科技1 天前
单北斗GNSS在桥梁形变监测中的应用与技术进展分析
前端
我讲个笑话你可别哭啊1 天前
鸿蒙ArkTS快速入门
前端·ts·arkts·鸿蒙·方舟开发框架
CherryLee_12101 天前
基于poplar-annotation前端插件封装文本标注组件及使用
前端·文本标注
特立独行的猫a1 天前
C++轻量级Web框架介绍与对比:Crow与httplib
开发语言·前端·c++·crow·httplib
周航宇JoeZhou1 天前
JB2-7-HTML
java·前端·容器·html·h5·标签·表单
代码小库1 天前
【课程作业必备】Web开发技术HTML静态网站模板 - 校园动漫社团主题完整源码
前端·html
VT.馒头1 天前
【力扣】2722. 根据 ID 合并两个数组
javascript·算法·leetcode·职场和发展·typescript