gpt4 book ai didi

html - CSS Grid - 底部的空白以及如何删除它

转载 作者:太空宇宙 更新时间:2023-11-03 21:07:04 24 4
gpt4 key购买 nike

我正在尝试掌握 CSS Grid,这是我以前使用 Bootstrap 的一种转变。

我创建了一个简单的布局(4 行和 6 列),但是底部有一个不需要的空白,导致可见滚动。

有没有办法在不为 .container 元素设置确切高度的情况下解决这个问题?当我将高度设置为 .container(高度:500 像素)时,问题就消失了。这是绕过它的方法吗?我不想使用可能会导致我在更小或更大视口(viewport)上出现问题的快速修复。

.grid{
display: grid;
position: relative;
margin: auto;
grid-template-areas:
"nav nav nav nav nav nav"
"logo logo logo logo logo logo"
"main main main main main side"
"footer footer footer footer footer footer";

grid-template-columns: repeat(6, 1fr);
grid-template-rows: 50px 50px 1fr 1fr;
}
.nav{
grid-area: nav;
background-color:green;
}
.logo{
grid-area: logo;
background-color:salmon;
}
.main{
grid-area: main;
background-color:cadetblue;
min-height: 800px;
height: auto;
}
.side{
grid-area: side;
background-color:lightpink;
min-height: 800px;
height: auto;
}
.footer{
grid-area: footer;
background-color:sandybrown;
height: 50px;
}

.overlap{
background-color: hotpink;
grid-area: 3/ 3/ 3/ 4;
z-index: 5;
}
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSSGrid</title>

</head>

<body>
<div class="grid">

<nav class="nav"></nav>
<div class="logo"></div>
<div class="overlap">
<h3>Overlap!</h3>
</div>
<section class="main"></section>
<aside class="side"></aside>
<footer class="footer"></footer>

</div>
</body>
</html>

最佳答案

由于您想要 4 行并且其中一个占用尽可能多的空间 (1fr),因此最后一行应该具有固定高度或设置到自动

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

::before,
::after {
box-sizing: inherit;
}

.grid {
display: grid;
position: relative;
margin: auto;
grid-template-areas: "nav nav nav nav nav nav" "logo logo logo logo logo logo" "main main main main main side" "footer footer footer footer footer footer";
grid-template-columns: repeat(6, 1fr);
grid-template-rows: 50px 50px 1fr auto;
}

.nav {
grid-area: nav;
background-color: green;
}

.logo {
grid-area: logo;
background-color: salmon;
}

.main {
grid-area: main;
background-color: cadetblue;
min-height: 800px;
height: auto;
}

.side {
grid-area: side;
background-color: lightpink;
min-height: 800px;
height: auto;
}

.footer {
grid-area: footer;
background-color: sandybrown;
height: 50px;
}

.overlap {
background-color: hotpink;
grid-area: 3/ 3/ 3/ 4;
z-index: 5;
}
<div class="grid">
<nav class="nav"></nav>
<div class="logo"></div>
<div class="overlap">
<h3>Overlap!</h3>
</div>
<section class="main"></section>
<aside class="side"></aside>
<footer class="footer"></footer>
</div>

关于html - CSS Grid - 底部的空白以及如何删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51616903/

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