我想要实现的是,当视口比内容高时,内容应该垂直中心。当视口不够高且内容溢出时,父控件应该提供垂直滚动条。
当我将柔性盒内容与中间对齐,并将内容设置为滚动时,它不仅会忽略内容边距,而且会在顶部截断(给定的视口比内容短)。有办法解决这个问题吗?
html, body {
height: 100%;
}
body {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
overflow: hidden;
}
.container {
overflow-y: auto;
display: flex;
flex-wrap: nowrap;
flex-direction: row;
align-items: center;
}
.content {
border: 1px solid grey;
background-color: lightgrey;
padding: 10px;
margin: 10px;
border-radius: 10px;
}<div class="container">
<div class="content">
Start of the content
<br />
<br />
Middle of the content
<br />
<br />
End of the content
</div>
</div>
<div class="container">
<div class="content">
Start of the content :(((
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Middle of the content
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
End of the content
</div>
</div>
发布于 2021-11-24 10:30:09
align-items: safe center应该避免孩子们越界。
https://developer.mozilla.org/en-US/docs/Web/CSS/align-items
安全
与对齐关键字一起使用。如果选择的关键字意味着项溢出导致数据丢失的对齐容器,则项目将被对齐,就好像对齐模式已经启动一样。
html, body {
height: 100%;
}
body {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
overflow: hidden;
}
.container {
overflow-y: auto;
display: flex;
flex-wrap: nowrap;
flex-direction: row;
align-items: safe center;
}
.content {
border: 1px solid grey;
background-color: lightgrey;
padding: 10px;
margin: 10px;
border-radius: 10px;
}<div class="container">
<div class="content">
Start of the content
<br />
<br />
Middle of the content
<br />
<br />
End of the content
</div>
</div>
<div class="container">
<div class="content">
Start of the content :(((
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Middle of the content
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
End of the content
</div>
</div>
如果浏览器在对齐时不支持安全/不安全的关键字,则可以使用自动页边距:
html, body {
height: 100%;
}
body {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
overflow: hidden;
}
.container {
overflow-y: auto;
display: flex;
flex-wrap: nowrap;
flex-direction: row;
padding:10px 0;
}
.content {
border: 1px solid grey;
background-color: lightgrey;
padding: 10px;
margin:auto 10px;
border-radius: 10px;
}<div class="container">
<div class="content">
Start of the content
<br />
<br />
Middle of the content
<br />
<br />
End of the content
</div>
</div>
<div class="container">
<div class="content">
Start of the content :(((
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
Middle of the content
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
End of the content
</div>
</div>
https://stackoverflow.com/questions/70094298
复制相似问题