gpt4 book ai didi

html - 当容器滚动时,如何让子 div 使用所有可用空间?

转载 作者:行者123 更新时间:2023-12-04 01:04:02 25 4
gpt4 key购买 nike

我有几个像这样的div:

.item {
white-space: nowrap;
}
.item:hover {
background-color: #DDDDDD;
}
.container {
max-width: 200px;
overflow: auto;
}
<div class="container">
<div class="item">somewhat short item 1</div>
<div class="item">somewhat short item 2</div>
<div class="item">somewhat short item 3</div>
<div class="item">very long item that makes the container scroll</div>
<div class="item">somewhat short item 4</div>
<div class="item">somewhat short item 5</div>
<div class="item">somewhat short item 6</div>
<div class="item">somewhat short item 7</div>
</div>

较长的 div 使容器滚动,因为它应该。然而,因为容器的宽度没有增加,悬停在滚动开始的地方被切断:

enter image description here

我希望所有的 div 都延伸到可滚动区域的末尾,这样悬停就不会像那样被切断。

我该怎么做?

最佳答案

容器上的display:grid就可以了

.item {
white-space: nowrap;
}
.item:hover {
background-color: #DDDDDD;
}
.container {
max-width: 200px;
overflow: auto;
display:grid;
}
<div class="container">
<div class="item">somewhat short item 1</div>
<div class="item">somewhat short item 2</div>
<div class="item">somewhat short item 3</div>
<div class="item">very long item that makes the container scroll</div>
<div class="item">somewhat short item 4</div>
<div class="item">somewhat short item 5</div>
<div class="item">somewhat short item 6</div>
<div class="item">somewhat short item 7</div>
</div>

关于html - 当容器滚动时,如何让子 div 使用所有可用空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67097473/

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