gpt4 book ai didi

html - css grid children 越过网格边界

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

我有一个网格,其子项包含应该可滚动的内容。网格上方和下方是页眉和页脚。类似于以下内容:

<div class="app">
<div class="header">
Header
</div>
<div class="grid">
<div class="first card">
<div>
Card Header
</div>
<div class="content">
...
</div>
</div>
<div class="second card">
<div>
Card Header
</div>
<div class="content">
...
</div>
</div>
<div class="third card">
<div>
Card Header
</div>
<div class="content">
...
</div>
</div>
</div>
<div class="footer">
Footer
</div>
</div>

我想确保网格永远不会增长到足以溢出视口(viewport)。页脚应始终可见,视口(viewport)不应滚动(单个卡片内容应可滚动)。

我已将内容设置为在溢出时滚动:

.content {
overflow-y: auto;
}

但似乎让它们滚动的唯一方法是设置固定的 heightmax-height。我不希望它被修复。我希望它只占用尽可能多的空间,而不会导致主视口(viewport)滚动。我已经花了几个小时来弄乱 max-height 试图让它工作。下面是我的 fiddle 。任何帮助将不胜感激

https://jsfiddle.net/ja94t0m0/39/

如您所见,页脚出现在(几乎)正确的位置——实际上只是略低于它应该出现的位置。但是网格元素在它下面延伸得很好。我正在尝试解决这两个问题

最佳答案

您可以通过使用 CSS flexbox

来实现

The Flexible Box Module, usually referred to as flexbox, was designed as a one-dimensional layout model, and as a method that could offer space distribution between items in an interface and powerful alignment capabilities. This article gives an outline of the main features of flexbox, which we will be exploring in more detail in the rest of these guides.

因为我更喜欢你使用 Autoprefixer CSS

https://autoprefixer.github.io/

autoprefixer 使用有关浏览器流行度的数据以及浏览器对供应商前缀的支持。它根据这些信息排列和删除前缀。它可以帮助您获得前缀:动画、过渡、转换、网格、 flex 、 flex 盒等。

在使用 Autoprefixer CSS 之前

html,
body {
height: 100%;
margin: 0
}

.box {
display: flex;
flex-flow: column;
height: 100%;
}

.box .row {
border: 1px dotted grey;
}

.box .row.header {
flex: 0 1 auto;
}

.box .row.content {
display: grid;
grid-template-areas: "first second third";
grid-gap: .5em;
margin: .5em;
flex-flow: column;
flex: 1 1 auto;
overflow: hidden;
height: 100%;
}

.box .row.footer {
flex: 0 1 40px;
}

.content {
overflow-y: auto;
background-color: orange;
}
.card {
display: flex;
flex-direction: column;
background-color: lightblue;
padding: .5em;
box-sizing: border-box;
flex-grow: 0;
overflow-y: auto;
}
.first {
grid-area: first;
}

.second {
grid-area: second;
}

.third {
grid-area: third;
}
<div class="box">
<div class="row header">
<p>
HEADER FIXED HEIGHT
</p>
</div>
<div class="row content">

<div class="first card">
<div>
Card Header
</div>
<div class="content">
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
</div>
</div>
<div class="second card">
<div>
Card Header
</div>
<div class="content">
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
</div>
</div>
<div class="third card">
<div>
Card Header
</div>
<div class="content">
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
</div>
</div>
</div>
<div class="row footer">
<p>FOOTER FIXED HEIGHT</p>
</div>
</div>

使用 Autoprefixer CSS 后

html,
body {
height: 100%;
margin: 0
}

.box {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-flow: column;
flex-flow: column;
height: 100%;
}

.box .row {
border: 1px dotted grey;
}

.box .row.header {
-webkit-box-flex: 0;
-ms-flex: 0 1 auto;
flex: 0 1 auto;
}

.box .row.content {
display: -ms-grid;
display: grid;
grid-template-areas: "first second third";
grid-gap: .5em;
margin: .5em;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-flow: column;
flex-flow: column;
-webkit-box-flex: 1;
-ms-flex: 1 1 auto;
flex: 1 1 auto;
overflow: hidden;
height: 100%;
}

.box .row.footer {
-webkit-box-flex: 0;
-ms-flex: 0 1 40px;
flex: 0 1 40px;
}

.content {
overflow-y: auto;
background-color: orange;
}
.card {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
background-color: lightblue;
padding: .5em;
-webkit-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-flex: 0;
-ms-flex-positive: 0;
flex-grow: 0;
overflow-y: auto;
}
.first {
-ms-grid-row: 1;
-ms-grid-column: 1;
grid-area: first;
}

.second {
-ms-grid-row: 1;
-ms-grid-column: 2;
grid-area: second;
}

.third {
-ms-grid-row: 1;
-ms-grid-column: 3;
grid-area: third;
}
<div class="box">
<div class="row header">
<p>
HEADER FIXED HEIGHT
</p>
</div>
<div class="row content">

<div class="first card">
<div>
Card Header
</div>
<div class="content">
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
</div>
</div>
<div class="second card">
<div>
Card Header
</div>
<div class="content">
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
</div>
</div>
<div class="third card">
<div>
Card Header
</div>
<div class="content">
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
<div>Some content</div>
</div>
</div>
</div>
<div class="row footer">
<p>FOOTER FIXED HEIGHT</p>
</div>
</div>

关于html - css grid children 越过网格边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49185334/

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