gpt4 book ai didi

html - 在移动 View 中折叠网格项时避免双重网格间隙

转载 作者:行者123 更新时间:2023-11-28 14:21:03 24 4
gpt4 key购买 nike

我正在使用 CSS 网格创建一个网站布局,我几乎完全按照我的意愿得到了它,但我遇到了一个问题:

我的桌面 View 是我想要的,但是当它折叠成移动大小的 View 时(所有元素都在单独的行中),我的 leftPadrightPad div 导致double grid-gap 如下所示:

double grid gap

代码可以在这里看到:https://codepen.io/nickmask/pen/BbxNoE

.container {
display: grid;

grid-template-areas:
"nav nav nav nav nav"
"header header header header header"
"leftPad leftSidebar content rightSidebar rightPad"
"footer footer footer footer footer";

grid-template-columns: auto 200px minmax(auto, 600px) 200px auto;
grid-template-rows: auto auto 1fr auto;
grid-gap: 10px;

height: 100vh;
}

body {
margin: 0;
}

@media (max-width: 768px) {
.container {
grid-template-areas:
"nav"
"header"
"leftPad"
"leftSidebar"
"content"
"rightSidebar"
"rightPad"
"footer";

grid-template-columns: 1fr;
grid-template-rows:
auto /* Nav */
auto /* Header */
auto /* Left Pad */
minmax(75px, auto) /* Left Sidebar */
1fr /* Content */
minmax(75px, auto) /* Right Sidebar */
auto /* Right Pad */
auto; /* Footer */
}

nav,
aside {
margin: 0;
}
}

header {
grid-area: header;
background-color: aqua;
}

nav {
grid-area: nav;
background-color: lightblue;
}

main {
grid-area: content;
}

.leftSidebar {
background-color: aliceblue;
grid-area: leftSidebar;
}

.rightSidebar {
background-color: aliceblue;
grid-area: rightSidebar;
}

footer {
grid-area: footer;
height: 100px;
background-color: lightcoral;
}
    <div class="container">
<nav>
<!-- Navigation -->
Nav
</nav>
<header>
Header
</header>
<div class="leftPad"></div>
<aside class="leftSidebar">
<!-- Sidebar / Ads -->
Side bar
</aside>
<main>
<!-- Main content -->
Main content
</main>
<aside class="rightSidebar">
<!-- Sidebar / Ads -->
Side bar
</aside>
<div class="rightPad"></div>
<footer>
Footer
<!-- Footer content -->
</footer>
</div>
>

是否有解决此问题的方法,或关于如何以不同方式构建此布局的建议?干杯!

最佳答案

问题是您的 leftPadrightPad - 您可以删除它们,然后更改您的 grid-template-areas:

grid-template-areas:
"nav nav nav nav nav"
"header header header header header"
". leftSidebar content rightSidebar ."
"footer footer footer footer footer";

注意上面定义中的。在移动 View 中,忽略它们即可。

However you can leave grid cells empty with this method of layout. To leave a cell empty use the full stop character, '.'.

MDN

请看下面的演示:

.container {
display: grid;

grid-template-areas:
"nav nav nav nav nav"
"header header header header header"
". leftSidebar content rightSidebar ."
"footer footer footer footer footer"; /* CHANGED */

grid-template-columns: auto 200px minmax(auto, 600px) 200px auto;
grid-template-rows: auto auto 1fr auto;
grid-gap: 10px;

height: 100vh;
}

body {
margin: 0;
}

@media (max-width: 768px) {
.container {
grid-template-areas:
"nav"
"header"
"leftSidebar"
"content"
"rightSidebar"
"footer"; /* CHANGED */

grid-template-columns: 1fr;
grid-template-rows:
auto /* Nav */
auto /* Header */
minmax(75px, auto) /* Left Sidebar */
1fr /* Content */
minmax(75px, auto) /* Right Sidebar */
auto; /* Footer */
} /* CHANGED */

nav,
aside {
margin: 0;
}
}

header {
grid-area: header;
background-color: aqua;
}

nav {
grid-area: nav;
background-color: lightblue;
}

main {
grid-area: content;
}

.leftSidebar {
background-color: aliceblue;
grid-area: leftSidebar;
}

.rightSidebar {
background-color: aliceblue;
grid-area: rightSidebar;
}

footer {
grid-area: footer;
height: 100px;
background-color: lightcoral;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<div class="container">
<nav>
<!-- Navigation -->
Nav
</nav>
<header>
Header
</header>

<aside class="leftSidebar">
<!-- Sidebar / Ads -->
Side bar
</aside>
<main>
<!-- Main content -->
Main content
</main>
<aside class="rightSidebar">
<!-- Sidebar / Ads -->
Side bar
</aside>

<footer>
Footer
<!-- Footer content -->
</footer>
</div>
</body>
</html>

关于html - 在移动 View 中折叠网格项时避免双重网格间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55193903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com