下面是我尝试使用网格进行页面布局的尝试。但是,滚动条位于标题下方。
而不是占据页面的整个高度。
发生这种情况是因为我添加overflow:auto
了部分标签。由于我对使用网格布局不够熟悉,我不确定如何使用网格正确实现页面布局。有没有解决的办法?有没有更好的解决方案来实现我想要做的事情?
body {
font-size: 24px;
height: 100vh;
margin: 0;
display: grid;
grid-template-columns: minmax(150px, 200px) auto;
grid-template-rows: 54px auto;
grid-template-areas: "header header"
"nav section";
}
header {
grid-area: header;
background: red;
}
nav {
display:flex;
flex-direction: column;
justify-content: space-between;
grid-area: nav;
background: blue;
}
section {
grid-area: section;
background: yellow;
overflow:auto;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<body>
<header>
header
</header>
<nav>
<div>
navigation
<br> I'm at the top
</div>
<div>
I'm at the bottom
</div>
</nav>
<section>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
section
<br>
</section>
</body>
</html>
首先,滚动条在标题下,因为
body{
height:100vh;
}
放
body{
min-height:100vh;
}
现在,如果您设置 bodymin-height:100vh
这将使整个身体滚动而不是 section.This 会将 nav 拉伸到与 section 相同的高度because second row height in grid is auto
。
有没有解决的办法?有没有更好的解决方案来实现我想要做的事情?
我认为您不应该将 css grid 用于这种类型的布局,因为section
在大多数情况下高度会有所不同,这也会使导航高度发生变化。在您的示例中,您必须滚动到部分底部才能单击或访问导航栏(或侧边栏)中的最后一个元素,当部分高度超过100vh
.
我建议将导航栏position:fixed
放在左侧,并使用 css grid 将元素定位在剩余的右侧。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句