gpt4 book ai didi

html - 你如何在css中用网格容器填充剩余空间?

转载 作者:行者123 更新时间:2023-12-05 03:38:08 25 4
gpt4 key购买 nike

我是新手。我正在尝试制作一个高度为 100vh 的网页,其中页眉和页脚将分别采用 80px 和 50px。中间剩下的任何空间都应该完全由网格容器接管。到目前为止我确实成功了,但是中间容器只占用了它的内容所需的量。请参阅下图以供引用:

enter image description here

此外,我的印象是页脚标签会自动将部分放在视口(viewport)底部。但这也没有发生。我必须使用绝对定位吗?

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

html {
font-size: 10px;
}

body {
height: 100vh;
overflow-x: hidden;
}

.container {
max-width: 1400px;
margin: 0 auto;
}

header {
padding-left: 40px;
padding-right: 40px;
}

nav {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
min-height: 80px;
}

.nav-menuList {
display: inline-flex;
justify-content: space-between;
align-items: center;
gap: 4rem;
}

.nav-list {
list-style-type: none;
font-family: sans-serif;
font-size: 1.4rem;
font-weight: 500;
line-height: 4.4rem;
color: #000;
}

.grid-container {
display: grid;
grid-template-rows: 1fr 1fr;
row-gap: 4rem;
grid-template-areas:
"h1"
"h-btn";
padding: 0 40px;
align-self: center;
}

h1 {
font-family: sans-serif;
font-size: 4.8rem;
font-weight: 300;
line-height: 6.4rem;
color: #000;
grid-area: h1;
width: 75%;
}

.button-group {
grid-area: h-btn;
}

footer {
padding: 0 40px;
}
<body>
<div class="container">
<header>
<nav>
<div class="logo">
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0 4C0 1.79086 1.79086 0 4 0H28C30.2091 0 32 1.79086 32 4V28C32 30.2091 30.2091 32 28 32H4C1.79086 32 0 30.2091 0 28V4Z" fill="black" />
</svg>
</div>
<div class="menu">
<ul class="nav-menuList">
<li class="nav-list">menu</li>
<li class="nav-list">menu</li>
<li class="nav-list">menu</li>
</ul>
</div>
</nav>
</header>
<main>
<div class="grid-container">
<h1>Lorem ipsum dolor sit amet</h1>
<div class="button-group">
<button class="primary-btn hero-btn">CTA</button>
</div>
</div>
</main>
<footer>This is footer</footer>
</div>

</body>

最佳答案

Flexbox 解决方案:

  1. 应用于 body 或 flexbox 容器:min-height: 100vh;
  2. 应用于 header:height: 80px;footer:height: 50px;
  3. 通过添加以下内容使 main 或内容框占用剩余空间:flex-grow: 1;

body {
display: flex;
flex-direction: column;
height: 100vh;
}

header {
height: 80px;
}

footer {
height: 50px;
}

main {
flex-grow: 1;
}




/* for demonstration purpose only */
body {
margin: 0;
}

header,
footer,
main {
display: flex;
justify-content: center;
align-items: center;
}

header {
background-color: pink;
}

main {
background-color: lightblue;
}

footer {
background-color: lightgreen;
}
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS 网格解决方案:

  1. 应用于body 或 grid-container:min-height: 100vh;
  2. 定义行:grid-template-rows: 80px auto 50px; 这意味着 auto 将占用所有剩余空间。

body {
height: 100vh;
display: grid;
grid-template-rows: 80px auto 50px;
}




/* for demonstration purpose only */
body {
margin: 0;
}

header,
footer,
main {
display: flex;
justify-content: center;
align-items: center;
}

header {
background-color: pink;
}

main {
background-color: lightblue;
}

footer {
background-color: lightgreen;
}
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

关于html - 你如何在css中用网格容器填充剩余空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69065484/

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