gpt4 book ai didi

html - 自动调整行大小的网格模板不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 08:39:05 25 4
gpt4 key购买 nike

我需要自动调整一行的大小,但它没有调整大小。如何让行自动调整高度?

我的代码:

body {
display: flex;
flex-direction: column;
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
box-shadow: inset 2px 2px red, inset -2px -2px red
}

.body {
display: grid;
grid-template-rows: 1fr 40px;
grid-template-columns: 100%;
}

.body div {
box-shadow: inset 2px 2px red, inset -2px -2px red
}

.page {
display: grid;
grid-template-rows: 40px auto 40px;
/* works if I but "calc(100vh - 120px)" in place of 'auto'*/
grid-template-columns: 100%;
}
<!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, user-scalable=no">
</head>

<body>
<div class="body">
<div class="page">
<div class="pageHead">Head</div>
<div class="pageContent">Content</div>
<div class="pageFoot">Foot</div>
</div>
<div class="ypnNav">
Nav
</div>
</div>
</body>

</html>

为什么自动不工作?我也尝试过用 1fr 代替 auto,但这也行不通。

在“网格模板区域”中处理这种情况的正确方法是什么?

最佳答案

你差不多好了,你只需要将 flex-grow:1 添加到 .body 来填充你在 中设置的 flex 容器的空间正文。您的代码工作正常,但问题出在上层容器上。

body {
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
padding: 0;
box-shadow: inset 2px 2px red, inset -2px -2px red
}

.body {
display: grid;
grid-template-rows: 1fr 40px;
flex-grow: 1; /*added*/
}

.body div {
box-shadow: inset 2px 2px red, inset -2px -2px red
}

.page {
display: grid;
grid-template-rows: 40px auto 40px;
}
<div class="body">
<div class="page">
<div class="pageHead">Head</div>
<div class="pageContent">Content</div>
<div class="pageFoot">Foot</div>
</div>
<div class="ypnNav">
Nav
</div>
</div>

你也可以像下面这样简化并避免额外的包装:

body {
display: grid;
grid-template-rows: 1fr 40px;
height: 100vh;
margin: 0;
padding: 0;
box-shadow: inset 2px 2px red, inset -2px -2px red
}

body div {
box-shadow: inset 2px 2px red, inset -2px -2px red
}

.page {
display: grid;
grid-template-rows: 40px auto 40px;
}
<div class="page">
<div class="pageHead">Head</div>
<div class="pageContent">Content</div>
<div class="pageFoot">Foot</div>
</div>
<div class="ypnNav">
Nav
</div>

使用 display:contents ( https://caniuse.com/#feat=css-display-contents ) 你仍然可以进一步简化:

body {
display: grid;
grid-template-rows: 40px 1fr 40px 40px;
height: 100vh;
margin: 0;
box-shadow: inset 2px 2px red, inset -2px -2px red
}

body div {
box-shadow: inset 2px 2px red, inset -2px -2px red
}

.page {
display: contents;
}
<div class="page">
<div class="pageHead">Head</div>
<div class="pageContent">Content</div>
<div class="pageFoot">Foot</div>
</div>
<div class="ypnNav">
Nav
</div>

关于html - 自动调整行大小的网格模板不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55438960/

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