gpt4 book ai didi

html - 嵌套 flexbox 和 CSS 网格布局中的空间分配问题

转载 作者:行者123 更新时间:2023-11-28 19:26:22 25 4
gpt4 key购买 nike

据我所知,aside 元素和 div.content 应该占用 div.container 中可用空间的一半。此外,div.content 中的网格应占用所有可用的垂直空间。

问题是网格项不能占据任何高度(如果不是硬编码的话)。此外,网格占用的所有垂直空间(由于 grid-gap)均来自 aside 元素和 div.content,这意味着div.content 窃取了 aside 的垂直空间,这两个元素的高度不再相同。

aside 的高度等于 div.content 的高度减去网格的高度。

综上所述,禁止使用flexbox为div.content设置的高度。添加到 div.content 的子元素表现得更像它的兄弟元素。

* {
box-sizing: border-box;
}

body {
min-height: 100%;
margin: 0;
display: flex;
flex-direction: column;
}

html {
height: 100%;
}

header,
footer {
background-color: #1A1C22;
height: 100px;
}

.container {
flex: 1 0 400px;
display: flex;
flex-direction: column;
padding-top: 12px;
}

aside {
background-color: #6C757D;
flex: 1 1 auto;
}

.content {
flex: 1 1 auto;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(6, 1fr);
grid-gap: 20px;
border: 2px solid blue;
}

section {
background-color: #343A40;
border: 1px solid red;
}
<body>
<header></header>
<div class="container">
<aside></aside>
<div class="content">
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
</div>
</div>
<footer></footer>
</body>

最佳答案

通过显式指定height而不是flex-basis并设置flex: 1 0 auto.container。对于 contentaside (设置 flex -basis: 0 而不是 auto) - 请参阅下面的演示和 fiddle :

* {
box-sizing: border-box;
}

body {
min-height: 100%;
margin: 0;
display: flex;
flex-direction: column;
}

html {
height: 100%;
}

header,
footer {
background-color: #1A1C22;
height: 100px;
}

.container {
/* flex: 1 0 400px; */
display: flex;
flex-direction: column;
padding-top: 12px;
height: 400px; /* added */
flex: 1 0 auto; /* added */
}

aside {
background-color: #6C757D;
flex: 1; /* changed */
}

.content {
flex: 1; /* changed */
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(6, 1fr);
grid-gap: 20px;
border: 2px solid blue;
}

section {
background-color: #343A40;
border: 1px solid red;
}
<header></header>
<div class="container">
<aside></aside>
<div class="content">
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
</div>
</div>
<footer></footer>


上面的解决方案有几个问题:

  • 列 flexboxes 指定高度以实现正确行为并不是那么直观,

  • 请注意,asidecontent 之间仍然存在 4px 的高度差异,这显然是内容的 2px 边框。


使用 CSS Grid 进行布局

网格布局可以轻松解决这些问题 - flex 盒作为一维解决方案表现良好,但在某些布局中达不到标准,例如这种布局。在这里,我将删除 container 包装器并使用 4 行布局 (grid-template-rows: 100px minmax(200px, 1fr) minmax(200px, 1fr) 100px;):

* {
box-sizing: border-box;
}

html {
height: 100%;
}

body {
min-height: 100%;
margin: 0;
display: grid;
grid-template-rows: 100px minmax(200px, 1fr) minmax(200px, 1fr) 100px;
}

aside {
background-color: #6C757D;
}

.content {
display: grid;
grid-auto-rows: 1fr;
grid-gap: 10px;
border: 2px solid blue;
}

.content section {
background-color: #343A40;
border: 1px solid red;
}

header,
footer {
background-color: #1A1C22;
}
<header></header>
<aside></aside>
<article class="content">
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
</article>
<footer></footer>

关于html - 嵌套 flexbox 和 CSS 网格布局中的空间分配问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089947/

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