gpt4 book ai didi

html - CSS 溢出-y :scroll not work with flex box

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

我有这个布局

enter image description here

        .main {
background-color: greenyellow;
height: 100vh;
display: flex;
flex-direction: column;
}

.header{
background-color: hotpink;
}
.filter{
background-color: indianred;
}
.sort{
background-color: lavender;
/* height: 30rem; */
}

.jobs{
background-color: blue;
display: flex;
flex: 1;

}

.joblist{
background-color: cornflowerblue;
display: flex;
flex-direction: column;
justify-content: space-between;



}



body,html {
margin: 0;
padding: 0;
}

.pagination{
background-color:chartreuse
}

.items {

background-color:yellow;
display: flex;

flex:1;
flex-direction: column;
overflow-y: scroll;
}

.item {
height: 100px;
border:1px solid black
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" href="style.css"> -->
<style>

</style>
<title>Document</title>
</head>
<body>
<div class="main">
<div class="header">
<span>Header</span>
</div>
<div class="filter">
<h1>Filter</h1>
</div>
<div class="sort">
<h1>Sort</h1>
</div>
<div class="jobs">
<div class="joblist">
<div class="items">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<!-- <div class="item">7</div>
<div class="item">8</div> -->


</div>
<div class="pagination">
<h1>Pagination</h1>
</div>

</div>
<div class="jobview">
content
</div>

</div>
</div>
</body>
</html>

这里的问题是,如果我取消注释 7,8 个元素并将它们添加到黄色部分,预期的行为是黄色部分将包含所有带有滚动条的元素。

即使我已将 overflow-y: scroll; 放入 .items 并在此处显示滚动条类的东西。第 7 和第 8 项将添加到布局中在没有滚动条的情况下将分页部分推到底部。如何使用 CSS 解决此问题?

最佳答案

因此,当 main 设置为 100vh 时,我看不到问题。我认为这与我们不同的视口(viewport)和显示器尺寸有关。所以我将您的 main 设置为 50vh 这样我就可以看到问题所在。然后你可以在 jobs 上设置一个静态高度,在这个例子中我设置了 height: 694px; 然后那个容器将只显示这么多(前 6 个元素)和有限的高度将允许 overflow-y: scroll; 根据需要。

.main {
background-color: greenyellow;
height: 100vh;
display: flex;
flex-direction: column;
}

.header{
background-color: hotpink;
}
.filter{
background-color: indianred;
}
.sort{
background-color: lavender;
/* height: 30rem; */
}

.jobs{
background-color: blue;
display: flex;
flex: 1;
height: 694px;

}

.joblist{
background-color: cornflowerblue;
display: flex;
flex-direction: column;
justify-content: space-between;



}



body,html {
margin: 0;
padding: 0;
}

.pagination{
background-color:chartreuse
}

.items {

background-color:yellow;
display: flex;

flex:1;
flex-direction: column;
overflow-y: scroll;
}

.item {
min-height: 100px;
height: 100px;
border:1px solid black
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" href="style.css"> -->
<style>

</style>
<title>Document</title>
</head>
<body>
<div class="main">
<div class="header">
<span>Header</span>
</div>
<div class="filter">
<h1>Filter</h1>
</div>
<div class="sort">
<h1>Sort</h1>
</div>
<div class="jobs">
<div class="joblist">
<div class="items">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
<div class="item">11</div>
<div class="item">12</div>
<div class="item">13</div>
<div class="item">14</div>
<div class="item">15</div>
<div class="item">16</div>


</div>
<div class="pagination">
<h1>Pagination</h1>
</div>

</div>
<div class="jobview">
content
</div>

</div>
</div>
</body>
</html>

关于html - CSS 溢出-y :scroll not work with flex box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70668184/

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