hooyantsing's Blog

P144_18_栅格区域布局真的太好用了

字数统计: 288阅读时长: 1 min
2020/04/20

P144 18 栅格区域布局真的太好用了

grid-template-areas

给区域命名。在同一行的区域名写在””内,不同行用不同””。

1
2
3
4
display: grid;
grid-template-rows: 60px 1fr 60px;
grid-template-columns: 60px 1fr;
grid-template-areas: "header header" "nav main" "footer footer";

grid-area

另一个特性是区域名可以作为参数。

例:

e66ad338f58cae9295c95e3f4863e422.png

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        article {
            width: 100vw;
            height: 100vh;
            display: grid;
            grid-template-rows: 60px 1fr 60px;
            grid-template-columns: 60px 1fr;
            grid-template-areas: "header header" "nav main" "footer footer";
        }

        header, nav, main, footer {
            background: blueviolet;
            background-clip: content-box;
            padding: 10px;
        }

        header {
            grid-area: header;
        }

        nav {
            grid-area: nav;
        }

        main {
            grid-area: main;
        }

        footer {
            grid-area: footer;
            display: grid;
            grid-template-columns: repeat(4, 1fr);
        }

        footer div {
            background: #ddd;
            background-clip: content-box;
            padding: 3px;
        }
    </style>
</head>
<body>
<article>
    <header></header>
    <nav></nav>
    <main></main>
    <footer>
        <div></div>
        <div></div>
        <div></div>
        <div></div>
    </footer>
</article>
</body>
</html>
CATALOG